{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-16T00:12:35.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-16T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":45503,"title":"Place wastewater treatment processes in the correct order","description":"There are many technologies for treating wastewater. For example, grit chambers are used to remove heavy solids, filtration is used to remove smaller solids, chemical oxidation disinfects the water from bacteria, and so on. By performing these steps one after the other, it is possible to recycle sewage water back to nature.\r\n\r\nHowever, the steps must be performed in the correct order, i.e. some technologies must be performed before others. In this problem, we will label these technologies as 1, 2, ... *N*. You are then given a [ *K* x 2] matrix called *P*, which is a list of constraints to be read as follows:\r\n\r\n\"Technology *P*( _i_, 1) must be performed _before_ *P*( _i_, 2),\" for all rows _i_.\r\n\r\nYour task is to list all *N* technologies from left to right in the order that they should be performed, that is, without violating any constraint. If there are multiple possible orderings, choose the one in which the technologies with the smaller labels come earlier. Your output will be essential for designing the sequence of steps in a wastewater treatment plant. \r\n\r\nConsider the following example:\r\n\r\n  N = 4; P = [1 2;\r\n              3 4];\r\nPossible orderings: [1 2 3 4], [1 3 2 4], [1 3 4 2],\r\n                      [3 1 2 4], [3 1 4 2], [3 4 1 2].\r\nDesired ordering:   [1 2 3 4]\r\n\r\n\r\nAnother example:\r\n\r\n  N = 4; P = [2 1;\r\n              2 4];\r\nPossible orderings: [2 1 3 4], [2 1 4 3], [2 3 1 4], [2 3 4 1],\r\n                      [2 4 1 3], [2 4 3 1], [3 2 1 4], [3 2 4 1].\r\nDesired ordering:   [2 1 3 4]\r\n\r\n\r\nWrite a function that takes *N* and *P*, then output the desired ordering. \r\n\r\nYou are ensured that 1 \u003c= *N* \u003c= 100 and 1 \u003c= *K* \u003c= 100. All elements of *P* are within [1, *N*]. Furthermore, none of the constraints will be conflicting.","description_html":"\u003cp\u003eThere are many technologies for treating wastewater. For example, grit chambers are used to remove heavy solids, filtration is used to remove smaller solids, chemical oxidation disinfects the water from bacteria, and so on. By performing these steps one after the other, it is possible to recycle sewage water back to nature.\u003c/p\u003e\u003cp\u003eHowever, the steps must be performed in the correct order, i.e. some technologies must be performed before others. In this problem, we will label these technologies as 1, 2, ... \u003cb\u003eN\u003c/b\u003e. You are then given a [ \u003cb\u003eK\u003c/b\u003e x 2] matrix called \u003cb\u003eP\u003c/b\u003e, which is a list of constraints to be read as follows:\u003c/p\u003e\u003cp\u003e\"Technology \u003cb\u003eP\u003c/b\u003e( \u003ci\u003ei\u003c/i\u003e, 1) must be performed \u003ci\u003ebefore\u003c/i\u003e \u003cb\u003eP\u003c/b\u003e( \u003ci\u003ei\u003c/i\u003e, 2),\" for all rows \u003ci\u003ei\u003c/i\u003e.\u003c/p\u003e\u003cp\u003eYour task is to list all \u003cb\u003eN\u003c/b\u003e technologies from left to right in the order that they should be performed, that is, without violating any constraint. If there are multiple possible orderings, choose the one in which the technologies with the smaller labels come earlier. Your output will be essential for designing the sequence of steps in a wastewater treatment plant.\u003c/p\u003e\u003cp\u003eConsider the following example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eN = 4; P = [1 2;\r\n            3 4];\r\nPossible orderings: [1 2 3 4], [1 3 2 4], [1 3 4 2],\r\n                    [3 1 2 4], [3 1 4 2], [3 4 1 2].\r\nDesired ordering:   [1 2 3 4]\r\n\u003c/pre\u003e\u003cp\u003eAnother example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eN = 4; P = [2 1;\r\n            2 4];\r\nPossible orderings: [2 1 3 4], [2 1 4 3], [2 3 1 4], [2 3 4 1],\r\n                    [2 4 1 3], [2 4 3 1], [3 2 1 4], [3 2 4 1].\r\nDesired ordering:   [2 1 3 4]\r\n\u003c/pre\u003e\u003cp\u003eWrite a function that takes \u003cb\u003eN\u003c/b\u003e and \u003cb\u003eP\u003c/b\u003e, then output the desired ordering.\u003c/p\u003e\u003cp\u003eYou are ensured that 1 \u0026lt;= \u003cb\u003eN\u003c/b\u003e \u0026lt;= 100 and 1 \u0026lt;= \u003cb\u003eK\u003c/b\u003e \u0026lt;= 100. All elements of \u003cb\u003eP\u003c/b\u003e are within [1, \u003cb\u003eN\u003c/b\u003e]. Furthermore, none of the constraints will be conflicting.\u003c/p\u003e","function_template":"function y = order_processes(N,P)\r\n  y = P(:)';\r\nend","test_suite":"%%\r\nfiletext = fileread('order_processes.m')\r\nassert(isempty(strfind(filetext, 'rand')))\r\nassert(isempty(strfind(filetext, 'fileread')))\r\nassert(isempty(strfind(filetext, 'assert')))\r\nassert(isempty(strfind(filetext, 'echo')))\r\n%%\r\nN = 4; P = [1 2; 3 4];\r\ny_correct = [1 2 3 4];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 4; P = [2 1; 2 4];\r\ny_correct = [2 1 3 4];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 4; P = [3 2; 3 1];\r\ny_correct = [3 1 2 4];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 8; P = [3 5; 8 2; 5 1; 4 2; 4 5; 5 2; 6 2; 7 6; 4 6];\r\ny_correct = [3 4 5 1 7 6 8 2];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 77; P = [42 37; 27 29; 59 56; 24 72; 50 40; 18 54; 14 49; 75 70; ...\r\n12 34; 5 29; 56 68; 26 49; 22 2; 40 53; 54 49; 77 14; ...\r\n40 1; 3 1; 33 68; 25 47; 73 63; 33 47; 62 68; 35 49; ...\r\n60 52; 61 49; 74 44; 3 10; 4 2; 38 61; 30 18; 41 54; ...\r\n57 39; 29 14; 58 37; 23 17; 21 39; 18 49; 7 72; 72 44; ...\r\n42 66; 39 68; 51 14; 68 26; 53 41; 74 42; 30 49; 18 53; ...\r\n71 9; 50 54; 67 70; 57 30; 39 66; 36 20; 55 25; 59 44; ...\r\n25 49; 64 67; 56 49; 43 20; 28 74; 50 30; 70 68; 4 21; ...\r\n29 40; 17 40; 55 4; 75 37; 65 69; 12 54; 14 18; 65 40; ...\r\n6 63; 59 52; 63 54; 15 30; 11 66; 5 39; 61 54; 51 18; ...\r\n68 34; 15 62; 61 35; 5 14; 15 63; 37 26; 13 29; 21 29];\r\ny_correct = [3 5 6 7 8 10 11 12 13 15 16 19 22 23 17 24 27 28 31 32  ...\r\n33 36 38 43 20 45 46 48 50 51 55 4 2 21 25 29 47 57 30 39  ...\r\n58 59 56 60 52 61 35 62 64 65 40 1 67 69 71 9 72 73 63 74  ...\r\n42 44 66 75 37 70 68 26 34 76 77 14 18 53 41 54 49 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 96; P = [24 33; 83 43; 34 7; 68 88; 29 61; 6 16; 81 71; 29 2; ...\r\n74 5; 35 52; 52 76; 33 92; 49 46; 79 12; 7 59; 57 73; ...\r\n35 58; 88 13; 67 19; 28 53; 43 70; 53 36; 93 7; 86 36; ...\r\n72 62; 23 31; 57 42; 6 35; 11 4; 33 70; 54 10; 75 76; ...\r\n78 59; 42 90; 44 41; 20 87; 40 93; 23 52; 16 15; 63 84; ...\r\n10 59; 38 61; 5 62; 87 71; 31 21; 21 18; 52 93; 1 41; ...\r\n19 41; 67 81; 21 7; 36 74; 58 34; 35 78; 61 75; 84 92; ...\r\n8 90; 33 52; 81 96; 54 21; 71 21; 84 50; 41 93; 86 95; ...\r\n88 26; 74 93; 8 22; 96 7; 82 76; 24 67; 18 34; 30 33; ...\r\n29 16; 50 18; 75 62; 77 17; 56 65; 54 17; 54 48; 53 96; ...\r\n20 18; 18 41; 79 39; 53 34; 11 52; 49 24; 37 84; 63 58; ...\r\n95 41; 28 66; 61 53; 4 19; 49 66; 35 74; 48 76; 41 7; ...\r\n14 54; 1 92; 91 55; 92 15; ];\r\ny_correct = [1 3 6 8 9 11 4 14 20 22 23 25 27 28 29 2 16 30 31 32  ...\r\n35 37 38 40 44 45 47 49 24 33 46 51 52 54 10 48 56 57 42 60  ...\r\n61 53 63 58 64 65 66 67 19 68 69 72 73 75 77 17 78 79 12 39  ...\r\n80 81 82 76 83 43 70 84 50 85 86 36 74 5 62 87 71 21 18 34  ...\r\n88 13 26 89 90 91 55 92 15 94 95 41 93 96 7 59 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 49; P = [18 8; 8 42; 42 5; 3 32; 26 36; 16 40; 23 32; 10 13; ...\r\n37 15; 22 11; 23 14; 7 42; 34 28; 20 12; 5 13; 14 26; ...\r\n25 33; 32 28; 12 31; 12 36; 37 31; 19 13; 16 24; 9 32; ...\r\n22 12; 4 17; 18 14; 2 14; 2 23; 23 9; 23 3; ];\r\ny_correct = [1 2 4 6 7 10 16 17 18 8 19 20 21 22 11 12 23 3 9 14  ...\r\n24 25 26 27 29 30 32 33 34 28 35 36 37 15 31 38 39 40 41 42  ...\r\n5 13 43 44 45 46 47 48 49 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 33; P = [14 22; 8 1; 27 1; 8 11; 10 22; ];\r\ny_correct = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  ...\r\n22 23 24 25 26 27 1 28 29 30 31 32 33 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 92; P = [78 42; 69 14; 45 91; 4 46; 57 2; 62 14; 45 8; 15 6; ...\r\n23 21; 67 91; 83 72; 10 4; 34 76; 1 53; 1 30; 15 46; ...\r\n17 19; 42 57; 38 36; 82 92; 3 73; 27 33; 28 79; 54 6; ...\r\n92 2; 7 6; 7 69; 31 16; 84 76; ];\r\ny_correct = [1 3 5 7 9 10 4 11 12 13 15 17 18 19 20 22 23 21 24 25  ...\r\n26 27 28 29 30 31 16 32 33 34 35 37 38 36 39 40 41 43 44 45  ...\r\n8 46 47 48 49 50 51 52 53 54 6 55 56 58 59 60 61 62 63 64  ...\r\n65 66 67 68 69 14 70 71 73 74 75 77 78 42 57 79 80 81 82 83  ...\r\n72 84 76 85 86 87 88 89 90 91 92 2 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 100; P = [94 96; 34 13; 33 30; 4 11; 18 66; 19 70; 98 100; 46 4; ...\r\n83 77; 59 41; 17 97; 4 68; 90 28; 31 53; 53 37; 74 20; ...\r\n80 12; 91 30; 2 71; 48 60; 19 7; 78 60; 37 92; 73 30; ...\r\n58 59; 35 78; 51 68; 94 60; 59 94; 2 12; 76 14; 66 16; ...\r\n91 68; 5 57; 32 68; 43 60; 72 85; 75 89; 21 96; 93 41; ...\r\n40 50; 63 10; 44 56; 12 60; 14 29; 43 72; 85 51; 64 86; ...\r\n54 7; 94 30; 11 41; 24 16; 59 60; 20 13; 45 88; 77 79; ...\r\n67 54; 41 1; 18 33; 17 48; 31 60; 59 54; 81 19; 82 97; ...\r\n3 79; 70 1; 42 58; 10 19; 52 98; 74 90; 86 94; 57 55; ...\r\n47 87; 6 43; 90 15; 75 79; 3 51; 12 50; 56 39; 13 1; ...\r\n86 22; 57 73; 23 48; 100 32; 19 85; 32 1; 14 66; 86 87; ...\r\n10 89; 20 12; 59 22; 18 67; 20 32; 41 16; 15 48; 27 59; ...\r\n16 92; 12 66; 62 47; 32 51; ];\r\ny_correct = [2 3 5 6 8 9 17 18 21 23 24 25 26 27 31 33 34 35 36 38  ...\r\n40 42 43 44 45 46 4 11 49 52 53 37 56 39 57 55 58 59 61 62  ...\r\n47 63 10 64 65 67 54 69 71 72 73 74 20 13 75 76 14 29 78 80  ...\r\n12 50 66 81 19 7 70 82 83 77 79 84 85 86 22 87 88 89 90 15  ...\r\n28 48 91 93 41 16 92 94 30 60 95 96 97 98 99 100 32 1 51 68];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 65; P = [33 42; 43 32; 2 37; 10 37; 59 32; 32 49; 17 34; 14 33; ...\r\n7 10; 12 54; 5 18; 1 20; 65 19; 24 36; 20 36; 56 32; ...\r\n42 38; 48 36; 39 32; 24 65; 16 54; 9 17; 27 19; 17 16; ...\r\n26 46; 64 52; 38 34; 59 26; 52 38; 63 50; 28 13; 5 25; ...\r\n10 27; 36 16; 19 64; 23 52; 48 41; 22 38; 54 38; 63 46; ...\r\n44 2; 8 13; 47 65; 36 52; 46 42; 46 38; 51 27; 41 38; ...\r\n2 62; 42 54; 51 3; 1 36; 28 14; 45 19; 6 34; 30 39; ...\r\n20 26; 49 38; 17 14; 2 26; 20 7; 21 33; 20 41; 21 9; ...\r\n37 27; 44 46; 5 34; 53 44; 62 17; 51 62; 50 62; 32 38; ...\r\n41 42; 37 25; 22 65; 3 38; 45 37; 44 13; 3 61; 21 19];\r\ny_correct = [1 4 5 6 8 11 12 15 18 20 7 10 21 9 22 23 24 28 29 30  ...\r\n31 35 39 40 43 45 47 48 36 41 51 3 53 44 2 13 37 25 27 55  ...\r\n56 57 58 59 26 32 49 60 61 63 46 50 62 17 14 16 33 42 54 65  ...\r\n19 64 52 38 34 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 26; P = [26 20];\r\ny_correct = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21  ...\r\n22 23 24 25 26 20];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 12; P = [9 4; 9 7; 6 9; 5 3; 6 11; 2 9; 6 4; 12 4; ...\r\n7 4; 12 6; 2 7; 2 6; 6 7; 1 7; 1 5; 1 10; ...\r\n1 6; 3 6; 10 7; 5 4; 8 6; 5 10; 11 9; 8 7];\r\ny_correct = [1 2 5 3 8 10 12 6 11 9 7 4 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 23; P = [19 6; 16 23; 7 14; 21 16; 9 2; 1 6; 7 6; 4 6; ...\r\n15 17; 22 21; 6 17; 18 17; 21 23; 14 17; 10 18; 10 23; ...\r\n20 21; 15 19; 20 5; 22 23; 10 12; 8 17; 2 22; 11 10; ...\r\n4 8; 8 9; 8 5; 7 19; 20 11; 9 17; 13 9; 17 21; ...\r\n8 21; 4 18; 2 18; 13 6; 3 2; 6 21; 4 10; 14 23; ...\r\n19 21; 17 16; 15 18; 10 6; 12 17; 1 18; 12 6; 3 21; ...\r\n20 6; 10 16; 7 18; 8 23; 14 22; 17 23; 7 23; 13 21];\r\ny_correct = [1 3 4 7 8 13 9 2 14 15 19 20 5 11 10 12 6 18 17 22  ...\r\n21 16 23 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 86; P = [74 53; 17 66; 84 7; 51 7; 24 57; 58 57; 10 20; 79 65; ...\r\n64 57; 58 39; 43 34; 45 63; 36 7; 4 77; 33 14; 75 19; ...\r\n86 4; 17 76; 54 6; 85 39; 74 39; 75 68; 28 20; 71 74; ...\r\n30 62; 85 65; 24 77; 69 42; 25 66; 40 11; 67 53; 84 6; ...\r\n85 62; 49 35; 54 5; 54 35; 82 64; 49 25; 42 39; 80 57; ...\r\n77 63; 16 46; 8 78; 30 63; 61 73; 75 66; 81 52; 25 63; ...\r\n33 26; 45 12; 9 40; 31 78; 55 30; 34 22; 7 25; 65 63; ...\r\n22 32; 34 52; 9 68; 48 73; 78 65; 73 40; 71 40; 14 68; ...\r\n3 65; 42 40; 15 61; 42 83; 56 74; 53 63; 59 49; 83 72; ...\r\n46 77; 3 10; 6 74; ];\r\ny_correct = [1 2 3 8 9 10 13 15 16 17 18 21 23 24 27 28 20 29 31 33  ...\r\n14 26 36 37 38 41 43 34 22 32 44 45 12 46 47 48 50 51 54 5  ...\r\n55 30 56 58 59 49 35 60 61 67 69 42 70 71 73 40 11 75 19 68  ...\r\n76 78 79 80 81 52 82 64 57 83 72 84 6 7 25 66 74 53 85 39  ...\r\n62 65 86 4 77 63 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":4,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":17,"test_suite_updated_at":"2020-05-07T23:35:01.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-05-07T22:19:51.000Z","updated_at":"2025-12-05T00:18:30.000Z","published_at":"2020-05-07T23:35:01.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThere are many technologies for treating wastewater. For example, grit chambers are used to remove heavy solids, filtration is used to remove smaller solids, chemical oxidation disinfects the water from bacteria, and so on. By performing these steps one after the other, it is possible to recycle sewage water back to nature.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHowever, the steps must be performed in the correct order, i.e. some technologies must be performed before others. In this problem, we will label these technologies as 1, 2, ...\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. You are then given a [\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eK\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x 2] matrix called\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, which is a list of constraints to be read as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\\\"Technology\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, 1) must be performed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ebefore\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, 2),\\\" for all rows\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour task is to list all\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e technologies from left to right in the order that they should be performed, that is, without violating any constraint. If there are multiple possible orderings, choose the one in which the technologies with the smaller labels come earlier. Your output will be essential for designing the sequence of steps in a wastewater treatment plant.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eConsider the following example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[N = 4; P = [1 2;\\n            3 4];\\nPossible orderings: [1 2 3 4], [1 3 2 4], [1 3 4 2],\\n                    [3 1 2 4], [3 1 4 2], [3 4 1 2].\\nDesired ordering:   [1 2 3 4]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAnother example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[N = 4; P = [2 1;\\n            2 4];\\nPossible orderings: [2 1 3 4], [2 1 4 3], [2 3 1 4], [2 3 4 1],\\n                    [2 4 1 3], [2 4 3 1], [3 2 1 4], [3 2 4 1].\\nDesired ordering:   [2 1 3 4]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that takes\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then output the desired ordering.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are ensured that 1 \u0026lt;=\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u0026lt;= 100 and 1 \u0026lt;=\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eK\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u0026lt;= 100. All elements of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are within [1,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e]. Furthermore, none of the constraints will be conflicting.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":45503,"title":"Place wastewater treatment processes in the correct order","description":"There are many technologies for treating wastewater. For example, grit chambers are used to remove heavy solids, filtration is used to remove smaller solids, chemical oxidation disinfects the water from bacteria, and so on. By performing these steps one after the other, it is possible to recycle sewage water back to nature.\r\n\r\nHowever, the steps must be performed in the correct order, i.e. some technologies must be performed before others. In this problem, we will label these technologies as 1, 2, ... *N*. You are then given a [ *K* x 2] matrix called *P*, which is a list of constraints to be read as follows:\r\n\r\n\"Technology *P*( _i_, 1) must be performed _before_ *P*( _i_, 2),\" for all rows _i_.\r\n\r\nYour task is to list all *N* technologies from left to right in the order that they should be performed, that is, without violating any constraint. If there are multiple possible orderings, choose the one in which the technologies with the smaller labels come earlier. Your output will be essential for designing the sequence of steps in a wastewater treatment plant. \r\n\r\nConsider the following example:\r\n\r\n  N = 4; P = [1 2;\r\n              3 4];\r\nPossible orderings: [1 2 3 4], [1 3 2 4], [1 3 4 2],\r\n                      [3 1 2 4], [3 1 4 2], [3 4 1 2].\r\nDesired ordering:   [1 2 3 4]\r\n\r\n\r\nAnother example:\r\n\r\n  N = 4; P = [2 1;\r\n              2 4];\r\nPossible orderings: [2 1 3 4], [2 1 4 3], [2 3 1 4], [2 3 4 1],\r\n                      [2 4 1 3], [2 4 3 1], [3 2 1 4], [3 2 4 1].\r\nDesired ordering:   [2 1 3 4]\r\n\r\n\r\nWrite a function that takes *N* and *P*, then output the desired ordering. \r\n\r\nYou are ensured that 1 \u003c= *N* \u003c= 100 and 1 \u003c= *K* \u003c= 100. All elements of *P* are within [1, *N*]. Furthermore, none of the constraints will be conflicting.","description_html":"\u003cp\u003eThere are many technologies for treating wastewater. For example, grit chambers are used to remove heavy solids, filtration is used to remove smaller solids, chemical oxidation disinfects the water from bacteria, and so on. By performing these steps one after the other, it is possible to recycle sewage water back to nature.\u003c/p\u003e\u003cp\u003eHowever, the steps must be performed in the correct order, i.e. some technologies must be performed before others. In this problem, we will label these technologies as 1, 2, ... \u003cb\u003eN\u003c/b\u003e. You are then given a [ \u003cb\u003eK\u003c/b\u003e x 2] matrix called \u003cb\u003eP\u003c/b\u003e, which is a list of constraints to be read as follows:\u003c/p\u003e\u003cp\u003e\"Technology \u003cb\u003eP\u003c/b\u003e( \u003ci\u003ei\u003c/i\u003e, 1) must be performed \u003ci\u003ebefore\u003c/i\u003e \u003cb\u003eP\u003c/b\u003e( \u003ci\u003ei\u003c/i\u003e, 2),\" for all rows \u003ci\u003ei\u003c/i\u003e.\u003c/p\u003e\u003cp\u003eYour task is to list all \u003cb\u003eN\u003c/b\u003e technologies from left to right in the order that they should be performed, that is, without violating any constraint. If there are multiple possible orderings, choose the one in which the technologies with the smaller labels come earlier. Your output will be essential for designing the sequence of steps in a wastewater treatment plant.\u003c/p\u003e\u003cp\u003eConsider the following example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eN = 4; P = [1 2;\r\n            3 4];\r\nPossible orderings: [1 2 3 4], [1 3 2 4], [1 3 4 2],\r\n                    [3 1 2 4], [3 1 4 2], [3 4 1 2].\r\nDesired ordering:   [1 2 3 4]\r\n\u003c/pre\u003e\u003cp\u003eAnother example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eN = 4; P = [2 1;\r\n            2 4];\r\nPossible orderings: [2 1 3 4], [2 1 4 3], [2 3 1 4], [2 3 4 1],\r\n                    [2 4 1 3], [2 4 3 1], [3 2 1 4], [3 2 4 1].\r\nDesired ordering:   [2 1 3 4]\r\n\u003c/pre\u003e\u003cp\u003eWrite a function that takes \u003cb\u003eN\u003c/b\u003e and \u003cb\u003eP\u003c/b\u003e, then output the desired ordering.\u003c/p\u003e\u003cp\u003eYou are ensured that 1 \u0026lt;= \u003cb\u003eN\u003c/b\u003e \u0026lt;= 100 and 1 \u0026lt;= \u003cb\u003eK\u003c/b\u003e \u0026lt;= 100. All elements of \u003cb\u003eP\u003c/b\u003e are within [1, \u003cb\u003eN\u003c/b\u003e]. Furthermore, none of the constraints will be conflicting.\u003c/p\u003e","function_template":"function y = order_processes(N,P)\r\n  y = P(:)';\r\nend","test_suite":"%%\r\nfiletext = fileread('order_processes.m')\r\nassert(isempty(strfind(filetext, 'rand')))\r\nassert(isempty(strfind(filetext, 'fileread')))\r\nassert(isempty(strfind(filetext, 'assert')))\r\nassert(isempty(strfind(filetext, 'echo')))\r\n%%\r\nN = 4; P = [1 2; 3 4];\r\ny_correct = [1 2 3 4];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 4; P = [2 1; 2 4];\r\ny_correct = [2 1 3 4];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 4; P = [3 2; 3 1];\r\ny_correct = [3 1 2 4];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 8; P = [3 5; 8 2; 5 1; 4 2; 4 5; 5 2; 6 2; 7 6; 4 6];\r\ny_correct = [3 4 5 1 7 6 8 2];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 77; P = [42 37; 27 29; 59 56; 24 72; 50 40; 18 54; 14 49; 75 70; ...\r\n12 34; 5 29; 56 68; 26 49; 22 2; 40 53; 54 49; 77 14; ...\r\n40 1; 3 1; 33 68; 25 47; 73 63; 33 47; 62 68; 35 49; ...\r\n60 52; 61 49; 74 44; 3 10; 4 2; 38 61; 30 18; 41 54; ...\r\n57 39; 29 14; 58 37; 23 17; 21 39; 18 49; 7 72; 72 44; ...\r\n42 66; 39 68; 51 14; 68 26; 53 41; 74 42; 30 49; 18 53; ...\r\n71 9; 50 54; 67 70; 57 30; 39 66; 36 20; 55 25; 59 44; ...\r\n25 49; 64 67; 56 49; 43 20; 28 74; 50 30; 70 68; 4 21; ...\r\n29 40; 17 40; 55 4; 75 37; 65 69; 12 54; 14 18; 65 40; ...\r\n6 63; 59 52; 63 54; 15 30; 11 66; 5 39; 61 54; 51 18; ...\r\n68 34; 15 62; 61 35; 5 14; 15 63; 37 26; 13 29; 21 29];\r\ny_correct = [3 5 6 7 8 10 11 12 13 15 16 19 22 23 17 24 27 28 31 32  ...\r\n33 36 38 43 20 45 46 48 50 51 55 4 2 21 25 29 47 57 30 39  ...\r\n58 59 56 60 52 61 35 62 64 65 40 1 67 69 71 9 72 73 63 74  ...\r\n42 44 66 75 37 70 68 26 34 76 77 14 18 53 41 54 49 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 96; P = [24 33; 83 43; 34 7; 68 88; 29 61; 6 16; 81 71; 29 2; ...\r\n74 5; 35 52; 52 76; 33 92; 49 46; 79 12; 7 59; 57 73; ...\r\n35 58; 88 13; 67 19; 28 53; 43 70; 53 36; 93 7; 86 36; ...\r\n72 62; 23 31; 57 42; 6 35; 11 4; 33 70; 54 10; 75 76; ...\r\n78 59; 42 90; 44 41; 20 87; 40 93; 23 52; 16 15; 63 84; ...\r\n10 59; 38 61; 5 62; 87 71; 31 21; 21 18; 52 93; 1 41; ...\r\n19 41; 67 81; 21 7; 36 74; 58 34; 35 78; 61 75; 84 92; ...\r\n8 90; 33 52; 81 96; 54 21; 71 21; 84 50; 41 93; 86 95; ...\r\n88 26; 74 93; 8 22; 96 7; 82 76; 24 67; 18 34; 30 33; ...\r\n29 16; 50 18; 75 62; 77 17; 56 65; 54 17; 54 48; 53 96; ...\r\n20 18; 18 41; 79 39; 53 34; 11 52; 49 24; 37 84; 63 58; ...\r\n95 41; 28 66; 61 53; 4 19; 49 66; 35 74; 48 76; 41 7; ...\r\n14 54; 1 92; 91 55; 92 15; ];\r\ny_correct = [1 3 6 8 9 11 4 14 20 22 23 25 27 28 29 2 16 30 31 32  ...\r\n35 37 38 40 44 45 47 49 24 33 46 51 52 54 10 48 56 57 42 60  ...\r\n61 53 63 58 64 65 66 67 19 68 69 72 73 75 77 17 78 79 12 39  ...\r\n80 81 82 76 83 43 70 84 50 85 86 36 74 5 62 87 71 21 18 34  ...\r\n88 13 26 89 90 91 55 92 15 94 95 41 93 96 7 59 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 49; P = [18 8; 8 42; 42 5; 3 32; 26 36; 16 40; 23 32; 10 13; ...\r\n37 15; 22 11; 23 14; 7 42; 34 28; 20 12; 5 13; 14 26; ...\r\n25 33; 32 28; 12 31; 12 36; 37 31; 19 13; 16 24; 9 32; ...\r\n22 12; 4 17; 18 14; 2 14; 2 23; 23 9; 23 3; ];\r\ny_correct = [1 2 4 6 7 10 16 17 18 8 19 20 21 22 11 12 23 3 9 14  ...\r\n24 25 26 27 29 30 32 33 34 28 35 36 37 15 31 38 39 40 41 42  ...\r\n5 13 43 44 45 46 47 48 49 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 33; P = [14 22; 8 1; 27 1; 8 11; 10 22; ];\r\ny_correct = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  ...\r\n22 23 24 25 26 27 1 28 29 30 31 32 33 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 92; P = [78 42; 69 14; 45 91; 4 46; 57 2; 62 14; 45 8; 15 6; ...\r\n23 21; 67 91; 83 72; 10 4; 34 76; 1 53; 1 30; 15 46; ...\r\n17 19; 42 57; 38 36; 82 92; 3 73; 27 33; 28 79; 54 6; ...\r\n92 2; 7 6; 7 69; 31 16; 84 76; ];\r\ny_correct = [1 3 5 7 9 10 4 11 12 13 15 17 18 19 20 22 23 21 24 25  ...\r\n26 27 28 29 30 31 16 32 33 34 35 37 38 36 39 40 41 43 44 45  ...\r\n8 46 47 48 49 50 51 52 53 54 6 55 56 58 59 60 61 62 63 64  ...\r\n65 66 67 68 69 14 70 71 73 74 75 77 78 42 57 79 80 81 82 83  ...\r\n72 84 76 85 86 87 88 89 90 91 92 2 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 100; P = [94 96; 34 13; 33 30; 4 11; 18 66; 19 70; 98 100; 46 4; ...\r\n83 77; 59 41; 17 97; 4 68; 90 28; 31 53; 53 37; 74 20; ...\r\n80 12; 91 30; 2 71; 48 60; 19 7; 78 60; 37 92; 73 30; ...\r\n58 59; 35 78; 51 68; 94 60; 59 94; 2 12; 76 14; 66 16; ...\r\n91 68; 5 57; 32 68; 43 60; 72 85; 75 89; 21 96; 93 41; ...\r\n40 50; 63 10; 44 56; 12 60; 14 29; 43 72; 85 51; 64 86; ...\r\n54 7; 94 30; 11 41; 24 16; 59 60; 20 13; 45 88; 77 79; ...\r\n67 54; 41 1; 18 33; 17 48; 31 60; 59 54; 81 19; 82 97; ...\r\n3 79; 70 1; 42 58; 10 19; 52 98; 74 90; 86 94; 57 55; ...\r\n47 87; 6 43; 90 15; 75 79; 3 51; 12 50; 56 39; 13 1; ...\r\n86 22; 57 73; 23 48; 100 32; 19 85; 32 1; 14 66; 86 87; ...\r\n10 89; 20 12; 59 22; 18 67; 20 32; 41 16; 15 48; 27 59; ...\r\n16 92; 12 66; 62 47; 32 51; ];\r\ny_correct = [2 3 5 6 8 9 17 18 21 23 24 25 26 27 31 33 34 35 36 38  ...\r\n40 42 43 44 45 46 4 11 49 52 53 37 56 39 57 55 58 59 61 62  ...\r\n47 63 10 64 65 67 54 69 71 72 73 74 20 13 75 76 14 29 78 80  ...\r\n12 50 66 81 19 7 70 82 83 77 79 84 85 86 22 87 88 89 90 15  ...\r\n28 48 91 93 41 16 92 94 30 60 95 96 97 98 99 100 32 1 51 68];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 65; P = [33 42; 43 32; 2 37; 10 37; 59 32; 32 49; 17 34; 14 33; ...\r\n7 10; 12 54; 5 18; 1 20; 65 19; 24 36; 20 36; 56 32; ...\r\n42 38; 48 36; 39 32; 24 65; 16 54; 9 17; 27 19; 17 16; ...\r\n26 46; 64 52; 38 34; 59 26; 52 38; 63 50; 28 13; 5 25; ...\r\n10 27; 36 16; 19 64; 23 52; 48 41; 22 38; 54 38; 63 46; ...\r\n44 2; 8 13; 47 65; 36 52; 46 42; 46 38; 51 27; 41 38; ...\r\n2 62; 42 54; 51 3; 1 36; 28 14; 45 19; 6 34; 30 39; ...\r\n20 26; 49 38; 17 14; 2 26; 20 7; 21 33; 20 41; 21 9; ...\r\n37 27; 44 46; 5 34; 53 44; 62 17; 51 62; 50 62; 32 38; ...\r\n41 42; 37 25; 22 65; 3 38; 45 37; 44 13; 3 61; 21 19];\r\ny_correct = [1 4 5 6 8 11 12 15 18 20 7 10 21 9 22 23 24 28 29 30  ...\r\n31 35 39 40 43 45 47 48 36 41 51 3 53 44 2 13 37 25 27 55  ...\r\n56 57 58 59 26 32 49 60 61 63 46 50 62 17 14 16 33 42 54 65  ...\r\n19 64 52 38 34 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 26; P = [26 20];\r\ny_correct = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21  ...\r\n22 23 24 25 26 20];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 12; P = [9 4; 9 7; 6 9; 5 3; 6 11; 2 9; 6 4; 12 4; ...\r\n7 4; 12 6; 2 7; 2 6; 6 7; 1 7; 1 5; 1 10; ...\r\n1 6; 3 6; 10 7; 5 4; 8 6; 5 10; 11 9; 8 7];\r\ny_correct = [1 2 5 3 8 10 12 6 11 9 7 4 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 23; P = [19 6; 16 23; 7 14; 21 16; 9 2; 1 6; 7 6; 4 6; ...\r\n15 17; 22 21; 6 17; 18 17; 21 23; 14 17; 10 18; 10 23; ...\r\n20 21; 15 19; 20 5; 22 23; 10 12; 8 17; 2 22; 11 10; ...\r\n4 8; 8 9; 8 5; 7 19; 20 11; 9 17; 13 9; 17 21; ...\r\n8 21; 4 18; 2 18; 13 6; 3 2; 6 21; 4 10; 14 23; ...\r\n19 21; 17 16; 15 18; 10 6; 12 17; 1 18; 12 6; 3 21; ...\r\n20 6; 10 16; 7 18; 8 23; 14 22; 17 23; 7 23; 13 21];\r\ny_correct = [1 3 4 7 8 13 9 2 14 15 19 20 5 11 10 12 6 18 17 22  ...\r\n21 16 23 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n%%\r\nN = 86; P = [74 53; 17 66; 84 7; 51 7; 24 57; 58 57; 10 20; 79 65; ...\r\n64 57; 58 39; 43 34; 45 63; 36 7; 4 77; 33 14; 75 19; ...\r\n86 4; 17 76; 54 6; 85 39; 74 39; 75 68; 28 20; 71 74; ...\r\n30 62; 85 65; 24 77; 69 42; 25 66; 40 11; 67 53; 84 6; ...\r\n85 62; 49 35; 54 5; 54 35; 82 64; 49 25; 42 39; 80 57; ...\r\n77 63; 16 46; 8 78; 30 63; 61 73; 75 66; 81 52; 25 63; ...\r\n33 26; 45 12; 9 40; 31 78; 55 30; 34 22; 7 25; 65 63; ...\r\n22 32; 34 52; 9 68; 48 73; 78 65; 73 40; 71 40; 14 68; ...\r\n3 65; 42 40; 15 61; 42 83; 56 74; 53 63; 59 49; 83 72; ...\r\n46 77; 3 10; 6 74; ];\r\ny_correct = [1 2 3 8 9 10 13 15 16 17 18 21 23 24 27 28 20 29 31 33  ...\r\n14 26 36 37 38 41 43 34 22 32 44 45 12 46 47 48 50 51 54 5  ...\r\n55 30 56 58 59 49 35 60 61 67 69 42 70 71 73 40 11 75 19 68  ...\r\n76 78 79 80 81 52 82 64 57 83 72 84 6 7 25 66 74 53 85 39  ...\r\n62 65 86 4 77 63 ];\r\nassert(isequal(order_processes(N,P),y_correct))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":4,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":17,"test_suite_updated_at":"2020-05-07T23:35:01.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-05-07T22:19:51.000Z","updated_at":"2025-12-05T00:18:30.000Z","published_at":"2020-05-07T23:35:01.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThere are many technologies for treating wastewater. For example, grit chambers are used to remove heavy solids, filtration is used to remove smaller solids, chemical oxidation disinfects the water from bacteria, and so on. By performing these steps one after the other, it is possible to recycle sewage water back to nature.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHowever, the steps must be performed in the correct order, i.e. some technologies must be performed before others. In this problem, we will label these technologies as 1, 2, ...\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. You are then given a [\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eK\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e x 2] matrix called\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, which is a list of constraints to be read as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\\\"Technology\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, 1) must be performed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ebefore\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, 2),\\\" for all rows\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ei\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYour task is to list all\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e technologies from left to right in the order that they should be performed, that is, without violating any constraint. If there are multiple possible orderings, choose the one in which the technologies with the smaller labels come earlier. Your output will be essential for designing the sequence of steps in a wastewater treatment plant.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eConsider the following example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[N = 4; P = [1 2;\\n            3 4];\\nPossible orderings: [1 2 3 4], [1 3 2 4], [1 3 4 2],\\n                    [3 1 2 4], [3 1 4 2], [3 4 1 2].\\nDesired ordering:   [1 2 3 4]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAnother example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[N = 4; P = [2 1;\\n            2 4];\\nPossible orderings: [2 1 3 4], [2 1 4 3], [2 3 1 4], [2 3 4 1],\\n                    [2 4 1 3], [2 4 3 1], [3 2 1 4], [3 2 4 1].\\nDesired ordering:   [2 1 3 4]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that takes\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then output the desired ordering.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are ensured that 1 \u0026lt;=\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u0026lt;= 100 and 1 \u0026lt;=\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eK\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u0026lt;= 100. All elements of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are within [1,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eN\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e]. Furthermore, none of the constraints will be conflicting.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"toposort\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"toposort\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"toposort\"","","\"","toposort","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f001c0d3360\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f001c0d32c0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f001c0d2960\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f001c0d35e0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f001c0d3540\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f001c0d34a0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f001c0d3400\u003e":"tag:\"toposort\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f001c0d3400\u003e":"tag:\"toposort\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"cody-search","password":"78X075ddcV44","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"toposort\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"toposort\"","","\"","toposort","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f001c0d3360\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f001c0d32c0\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f001c0d2960\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f001c0d35e0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f001c0d3540\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f001c0d34a0\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f001c0d3400\u003e":"tag:\"toposort\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f001c0d3400\u003e":"tag:\"toposort\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":45503,"difficulty_rating":"medium"}]}}