{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-06T14:01:22.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-06T00: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":1217,"title":"create a circulant matrix","description":"create a circulant matrix","description_html":"\u003cp\u003ecreate a circulant matrix\u003c/p\u003e","function_template":"function t = circulant(c)\r\n%CIRCULANT -Circulant matrix.\r\n%  C = circulant(c)\r\n%\r\n% CIRCULANT.m output a Circulant matrix C with its first column equal to the vector c.%\r\n% input   c         VECTOR first column of the circulant matrix\r\n\r\n   m = length(c);\r\n   c = c(:);                               % build vector of user data\r\n\r\n   cidx = (1:m)';\r\n   ridx = 1:m;\r\n   t = cidx(:,ones(m,1)) - ridx(ones(m,1),:);    \r\n   t = mod(t,m)+1;                         % circulant subscripts\r\n   t(:) = c(t);                            % actual data\r\n","test_suite":"%%\r\nx = [0 0 1];\r\ny_correct = [0 1 0; 0 0 1; 1 0 0];\r\nassert(isequal(circulant(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":10103,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":135,"test_suite_updated_at":"2013-01-20T04:34:29.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-20T04:33:36.000Z","updated_at":"2026-03-02T14:36:36.000Z","published_at":"2013-01-20T04:34:29.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003ecreate a circulant matrix\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\"}]}"},{"id":45498,"title":"Trace the path of a harmful chemical in an ecological network","description":"An ecological network consists of the cycles of nature, such as the water cycle, the carbon cycle, the oxygen cycle, etc. Due to human activities, harmful chemicals (or persistent pollutants) are now entering these cycles and may stay there forever. Your job is to track a specific unknown chemical inside an ecological network.\r\n\r\nFor this problem, a network involves *N* _sites_ in nature, labelled as _Site_ 1, _Site_ 2, ..., _Site_ *N*. Researchers have identified an ecological network for you, which is given as a [1 x *N*] row vector called *P*. The network is read as follows: \"A chemical that enters _Site i_ always end up at _Site *P*(i)_\". Consider the following example:\r\n\r\n  If a chemical enters Site:      1 2 3 4 5\r\n     it will end up at Site: P = [3 1 5 2 4]\r\n  \r\nIf a harmful chemical enters the ecological network from _Site 2_, it will be traced to _Site 1_ (which is *P*(2)), then eventually at _Site 3_, then at _Site 5_, then at _Site 4_, then back to _Site 2_. Hence, the path of this chemical is [2 1 3 5 4].\r\n\r\nWrite a function that takes a vector *P* and a starting site *S*. Output the path of the chemical after it enters _Site *S*_ in the given ecological network. You are ensured that:\r\n\r\n* *P* is always a permutation of integers 1 to *N*.\r\n* 2 \u003c= *N* \u003c= 100\r\n* 1 \u003c= *S* \u003c= *N*\r\n\r\nSee sample test cases:\r\n\r\n  \u003e\u003e trace_chemical([3 1 5 2 4],2)\r\n     ans = \r\n         2 1 3 5 4\r\n\u003e\u003e trace_chemical([3 1 5 2 4],1)\r\n     ans =\r\n         1 3 5 4 2\r\n\u003e\u003e trace_chemical([4 1 6 5 2 3],1)\r\n     ans =\r\n         1 4 5 2","description_html":"\u003cp\u003eAn ecological network consists of the cycles of nature, such as the water cycle, the carbon cycle, the oxygen cycle, etc. Due to human activities, harmful chemicals (or persistent pollutants) are now entering these cycles and may stay there forever. Your job is to track a specific unknown chemical inside an ecological network.\u003c/p\u003e\u003cp\u003eFor this problem, a network involves \u003cb\u003eN\u003c/b\u003e \u003ci\u003esites\u003c/i\u003e in nature, labelled as \u003ci\u003eSite\u003c/i\u003e 1, \u003ci\u003eSite\u003c/i\u003e 2, ..., \u003ci\u003eSite\u003c/i\u003e \u003cb\u003eN\u003c/b\u003e. Researchers have identified an ecological network for you, which is given as a [1 x \u003cb\u003eN\u003c/b\u003e] row vector called \u003cb\u003eP\u003c/b\u003e. The network is read as follows: \"A chemical that enters \u003ci\u003eSite i\u003c/i\u003e always end up at \u003ci\u003eSite \u003cb\u003eP\u003c/b\u003e(i)\u003c/i\u003e\". Consider the following example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIf a chemical enters Site:      1 2 3 4 5\r\n   it will end up at Site: P = [3 1 5 2 4]\r\n\u003c/pre\u003e\u003cp\u003eIf a harmful chemical enters the ecological network from \u003ci\u003eSite 2\u003c/i\u003e, it will be traced to \u003ci\u003eSite 1\u003c/i\u003e (which is \u003cb\u003eP\u003c/b\u003e(2)), then eventually at \u003ci\u003eSite 3\u003c/i\u003e, then at \u003ci\u003eSite 5\u003c/i\u003e, then at \u003ci\u003eSite 4\u003c/i\u003e, then back to \u003ci\u003eSite 2\u003c/i\u003e. Hence, the path of this chemical is [2 1 3 5 4].\u003c/p\u003e\u003cp\u003eWrite a function that takes a vector \u003cb\u003eP\u003c/b\u003e and a starting site \u003cb\u003eS\u003c/b\u003e. Output the path of the chemical after it enters \u003ci\u003eSite \u003cb\u003eS\u003c/b\u003e\u003c/i\u003e in the given ecological network. You are ensured that:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003cb\u003eP\u003c/b\u003e is always a permutation of integers 1 to \u003cb\u003eN\u003c/b\u003e.\u003c/li\u003e\u003cli\u003e2 \u0026lt;= \u003cb\u003eN\u003c/b\u003e \u0026lt;= 100\u003c/li\u003e\u003cli\u003e1 \u0026lt;= \u003cb\u003eS\u003c/b\u003e \u0026lt;= \u003cb\u003eN\u003c/b\u003e\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eSee sample test cases:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; trace_chemical([3 1 5 2 4],2)\r\n   ans = \r\n       2 1 3 5 4\r\n\u0026gt;\u0026gt; trace_chemical([3 1 5 2 4],1)\r\n   ans =\r\n       1 3 5 4 2\r\n\u0026gt;\u0026gt; trace_chemical([4 1 6 5 2 3],1)\r\n   ans =\r\n       1 4 5 2\r\n\u003c/pre\u003e","function_template":"function y = trace_chemical(P,S)\r\n  y = P;\r\nend","test_suite":"%%\r\nfiletext = fileread('trace_chemical.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\nP = [3 1 5 2 4];\r\nans = [2 1 3 5 4];\r\nassert(isequal(trace_chemical(P,2),ans))\r\n%%\r\nP = [3 1 5 2 4];\r\nans = [1 3 5 4 2];\r\nassert(isequal(trace_chemical(P,1),ans))\r\n%%\r\nP = [2 5 8 6 10 9 3 4 7 1 ];\r\nans = [8 4 6 9 7 3 ];\r\nassert(isequal(trace_chemical(P,8),ans))\r\n%%\r\nP = [36 15 70 23 1 60 35 13 19 41 48 20 49 44 68 29 9 61 51 38 ...\r\n     32 2 40 8 72 39 26 28 67 69 42 76 66 34 47 46 11 77 71 64 ...\r\n     4 37 80 18 74 52 7 33 58 50 27 3 45 55 65 43 21 54 31 6 ...\r\n     24 63 57 30 12 78 22 75 14 10 16 17 81 53 59 25 56 5 73 62 ...\r\n     79 ];\r\nans = [60 6 ];\r\nassert(isequal(trace_chemical(P,60),ans))\r\n%%\r\nP = [8 45 12 53 33 15 29 39 40 21 9 26 32 58 20 43 54 17 48 55 ...\r\n     5 49 37 57 16 46 36 10 34 6 38 50 11 27 22 42 19 4 13 47 ...\r\n     52 2 23 25 35 24 30 56 44 41 28 14 51 31 7 3 1 18 ];\r\nans = [35 22 49 44 25 16 43 23 37 19 48 56 3 12 26 46 24 57 1 8 ...\r\n     39 13 32 50 41 52 14 58 18 17 54 31 38 4 53 51 28 10 21 5 ...\r\n     33 11 9 40 47 30 6 15 20 55 7 29 34 27 36 42 2 45 ];\r\nassert(isequal(trace_chemical(P,35),ans))\r\n%%\r\nP = [9 28 7 42 18 16 30 17 24 20 41 29 13 15 44 8 27 23 12 19 ...\r\n     21 32 40 49 11 47 14 25 35 36 46 38 33 45 34 4 43 48 31 5 ...\r\n     3 10 26 39 37 1 22 6 2 ];\r\nans = [24 49 2 28 25 11 41 3 7 30 36 4 42 10 20 19 12 29 35 34 ...\r\n     45 37 43 26 47 22 32 38 48 6 16 8 17 27 14 15 44 39 31 46 ...\r\n     1 9 ];\r\nassert(isequal(trace_chemical(P,24),ans))\r\n%%\r\nP = [39 27 32 17 22 3 21 8 4 16 45 37 40 2 19 11 51 36 50 43 ...\r\n     13 44 12 30 48 28 42 35 10 14 5 38 15 9 20 18 6 26 31 24 ...\r\n     23 41 34 1 46 7 47 33 49 29 25 ];\r\nans = [11 45 46 7 21 13 40 24 30 14 2 27 42 41 23 12 37 6 3 32 ...\r\n     38 26 28 35 20 43 34 9 4 17 51 25 48 33 15 19 50 29 10 16 ];\r\nassert(isequal(trace_chemical(P,11),ans))\r\n%%\r\nP = [19 10 17 9 18 7 13 14 20 21 5 3 6 16 8 12 15 11 2 4 1];\r\nans = [4 9 20 ];\r\nassert(isequal(trace_chemical(P,4),ans))\r\n%%\r\nP = [12 1 5 66 26 29 64 68 2 33 38 41 55 8 18 49 27 47 22 50 ...\r\n     35 24 16 13 60 34 46 36 6 56 67 30 42 48 19 37 63 57 11 17 ...\r\n     40 59 15 23 45 32 61 44 53 31 28 10 62 9 21 7 52 14 39 51 ...\r\n     58 65 54 43 3 4 20 25 ];\r\nans = [26 34 48 44 23 16 49 53 62 65 3 5 ];\r\nassert(isequal(trace_chemical(P,26),ans))\r\n%%\r\nP = [65 8 29 66 49 72 61 38 18 33 58 62 67 40 20 27 46 1 5 6 ...\r\n     14 75 82 74 23 37 54 22 78 41 4 53 13 47 57 51 17 69 77 71 ...\r\n     64 35 25 44 21 70 19 76 36 10 81 42 60 79 28 31 9 48 3 56 ...\r\n     24 59 11 50 7 26 30 16 52 39 15 2 68 73 34 80 32 12 63 55 ...\r\n     45 43 ];\r\nans = [17 46 70 39 77 32 53 60 56 31 4 66 26 37 ];\r\nassert(isequal(trace_chemical(P,17),ans))\r\n%%\r\nP = [1 17 15 6 13 33 28 36 4 22 44 23 32 40 26 12 41 30 8 34 ...\r\n     37 14 21 5 9 10 29 3 35 38 11 43 31 16 19 27 24 45 39 7 ...\r\n     2 42 20 18 25 ];\r\nans = [14 40 7 28 3 15 26 10 22 ];\r\nassert(isequal(trace_chemical(P,14),ans))\r\n%%\r\nP = [1 17 21 15 6 24 13 5 4 18 2 9 3 29 10 28 12 23 11 25 ...\r\n     20 19 8 16 27 26 7 22 14 ];\r\nans = [10 18 23 8 5 6 24 16 28 22 19 11 2 17 12 9 4 15 ];\r\nassert(isequal(trace_chemical(P,10),ans))\r\n%%\r\nP = [6 2 3 4 1 7 5 ];\r\nans = [3 ];\r\nassert(isequal(trace_chemical(P,3),ans))\r\n%%\r\nP = [11 19 15 23 14 10 3 4 25 7 24 1 18 26 6 16 17 20 12 2 ...\r\n     13 22 9 21 8 5 ];\r\nans = [16 ];\r\nassert(isequal(trace_chemical(P,16),ans))\r\n%%\r\nP = [20 16 5 9 30 28 8 24 14 15 23 4 29 11 22 19 26 17 25 6 ...\r\n     27 18 3 2 1 13 31 12 10 21 7 ];\r\nans = [11 23 3 5 30 21 27 31 7 8 24 2 16 19 25 1 20 6 28 12 ...\r\n     4 9 14 ];\r\nassert(isequal(trace_chemical(P,11),ans))\r\n%%\r\nP = [1 18 16 8 15 21 27 22 23 17 26 19 3 4 9 11 7 6 29 2 ...\r\n     12 25 24 5 14 28 20 10 13 ];\r\nans = [22 25 14 4 8 ];\r\nassert(isequal(trace_chemical(P,22),ans))\r\n%%\r\nP = [38 48 42 87 57 89 92 12 20 62 59 51 26 29 45 55 10 71 44 69 ...\r\n     34 60 30 77 53 11 54 14 23 15 22 43 49 13 41 5 47 91 68 37 ...\r\n     9 3 76 31 85 33 40 1 63 70 18 8 17 35 90 36 24 83 94 21 ...\r\n     73 27 61 78 39 82 64 93 66 6 67 84 56 80 19 50 95 2 75 46 ...\r\n     74 16 81 72 4 25 86 32 28 52 65 58 88 7 79 ];\r\nans = [54 35 41 9 20 69 66 82 16 55 90 52 8 12 51 18 71 67 64 78 ...\r\n     2 48 1 38 91 65 39 68 93 88 32 43 76 50 70 6 89 28 14 29 ...\r\n     23 30 15 45 85 4 87 86 25 53 17 10 62 27 ];\r\nassert(isequal(trace_chemical(P,54),ans))\r\n%%\r\nP = [69 48 11 21 80 50 75 64 41 54 23 82 61 45 25 10 74 63 72 8 ...\r\n     15 81 42 60 59 65 35 37 70 33 76 24 36 49 56 18 38 6 44 39 ...\r\n     4 17 52 51 32 43 1 46 55 73 34 28 58 31 68 29 67 22 66 12 ...\r\n     53 5 16 77 19 7 13 26 57 79 3 47 71 40 14 30 2 20 62 9 ...\r\n     78 27 ];\r\nans = [27 35 56 29 70 79 62 5 80 9 41 4 21 15 25 59 66 7 75 14 ...\r\n     45 32 24 60 12 82 ];\r\nassert(isequal(trace_chemical(P,27),ans))\r\n%%\r\nP = [78 59 84 70 19 82 34 69 29 92 6 51 52 28 10 32 31 33 4 73 ...\r\n     24 89 99 68 64 47 46 95 94 21 53 44 62 26 93 91 58 55 98 79 ...\r\n     11 35 48 40 22 66 87 80 63 43 12 97 13 17 67 20 1 85 60 81 ...\r\n     25 50 88 49 96 90 76 83 36 15 75 23 41 86 39 9 8 54 7 61 ...\r\n     2 72 45 38 16 71 56 37 3 14 27 74 5 57 65 18 42 30 77 ];\r\nans = [85 16 32 44 40 79 7 34 26 47 87 56 20 73 41 11 6 82 72 23 ...\r\n     99 77 8 69 36 91 27 46 66 90 14 28 95 65 96 18 33 62 50 43 ...\r\n     48 80 61 25 64 49 63 88 37 58 ];\r\nassert(isequal(trace_chemical(P,85),ans))\r\n%%\r\nP = [86 17 25 63 38 72 9 64 56 10 7 26 43 28 36 40 24 71 41 22 ...\r\n     27 80 21 1 54 84 42 11 60 73 6 46 78 50 67 66 20 23 77 74 ...\r\n     57 44 85 75 16 13 47 14 29 48 19 58 2 39 81 83 59 33 49 61 ...\r\n     69 53 3 35 8 55 32 18 31 30 12 51 34 65 87 62 5 52 15 45 ...\r\n     4 68 82 76 70 37 79 ];\r\nans = [36 66 55 81 4 63 3 25 54 39 77 5 38 23 21 27 42 44 75 87 ...\r\n     79 15 ];\r\nassert(isequal(trace_chemical(P,36),ans))\r\n%%\r\nP = [25 7 4 6 16 30 24 28 9 3 31 13 10 23 2 26 29 8 5 20 ...\r\n     18 27 21 11 22 17 12 19 1 15 14 ];\r\nans = [21 18 8 28 19 5 16 26 17 29 1 25 22 27 12 13 10 3 4 6 ...\r\n     30 15 2 7 24 11 31 14 23 ];\r\nassert(isequal(trace_chemical(P,21),ans))\r\n%%\r\nP = [30 31 13 37 59 49 28 25 65 61 22 8 43 80 64 18 2 74 46 14 ...\r\n     85 12 62 5 55 67 48 42 78 83 47 15 79 89 34 68 54 90 3 44 ...\r\n     72 40 21 24 60 82 35 50 66 11 41 77 75 7 16 27 73 10 76 71 ...\r\n     33 56 39 53 38 19 36 84 69 81 23 87 9 51 70 86 88 1 26 57 ...\r\n     63 20 4 52 29 45 58 6 17 32 ];\r\nans = [78 1 30 83 4 37 54 7 28 42 40 44 24 5 59 76 86 45 60 71 ...\r\n     23 62 56 27 48 50 11 22 12 8 25 55 16 18 74 51 41 72 87 58 ...\r\n     10 61 33 79 26 67 36 68 84 52 77 88 6 49 66 19 46 82 20 14 ...\r\n     80 57 73 9 65 38 90 32 15 64 53 75 70 81 63 39 3 13 43 21 ...\r\n     85 29 ];\r\nassert(isequal(trace_chemical(P,78),ans))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":32,"test_suite_updated_at":"2020-05-07T02:09:17.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-05-06T19:35:57.000Z","updated_at":"2025-12-07T16:54:53.000Z","published_at":"2020-05-06T19:53:21.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\u003eAn ecological network consists of the cycles of nature, such as the water cycle, the carbon cycle, the oxygen cycle, etc. Due to human activities, harmful chemicals (or persistent pollutants) are now entering these cycles and may stay there forever. Your job is to track a specific unknown chemical inside an ecological network.\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\u003eFor this problem, a network involves\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 \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003esites\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e in nature, labelled as\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\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSite\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. Researchers have identified an ecological network for you, which is given as a [1 x\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] row vector 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. The network is read as follows: \\\"A chemical that enters\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\u003eSite i\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e always end up at\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\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e(i)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e\\\". Consider 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[If a chemical enters Site:      1 2 3 4 5\\n   it will end up at Site: P = [3 1 5 2 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\u003eIf a harmful chemical enters the ecological network from\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\u003eSite 2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, it will be traced to\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\u003eSite 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (which is\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(2)), then eventually at\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\u003eSite 3\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then at\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\u003eSite 5\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then at\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\u003eSite 4\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then back to\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\u003eSite 2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Hence, the path of this chemical is [2 1 3 5 4].\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 a vector\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 and a starting site\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\u003eS\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Output the path of the chemical after it enters\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\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eS\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e in the given ecological network. You are ensured that:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\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 is always a permutation of integers 1 to\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.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e2 \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\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 \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\u003eS\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \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\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\u003eSee sample test cases:\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[\u003e\u003e trace_chemical([3 1 5 2 4],2)\\n   ans = \\n       2 1 3 5 4\\n\u003e\u003e trace_chemical([3 1 5 2 4],1)\\n   ans =\\n       1 3 5 4 2\\n\u003e\u003e trace_chemical([4 1 6 5 2 3],1)\\n   ans =\\n       1 4 5 2]]\u003e\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\"}]}"},{"id":45382,"title":"Find a Hamiltonian Cycle in a Graph","description":"You are given a graph g and asked to find a Hamiltonian cycle of g.\r\n\r\nSee \u003chttps://www.mathworks.com/help/matlab/ref/graph.html MATLAB graph documentation\u003e for details of the graph data structure.\r\n\r\nA cycle of g is a sequence of vertices of g such that each adjacent pair of vertices in the sequence share an edge in g and the first and last vertices in the sequence share an edge in g. A Hamiltonian cycle of g is a cycle of g that visits each vertex of g exactly once.\r\n\r\nFor example, consider the adjacency matrix below.\r\n\r\n  A = [\r\n     0     0     0     1     1     0     1     1\r\n     0     1     0     1     0     0     1     0\r\n     0     0     0     1     1     0     0     1\r\n     1     1     1     1     0     1     0     1\r\n     1     0     1     0     0     1     0     1\r\n     0     0     0     1     1     0     0     1\r\n     1     1     0     0     0     0     0     0\r\n     1     0     1     1     1     1     0     0];\r\n\r\nThis corresponds to the graph with vertices labeled 1 through 8 and an edge between two vertices i and j if and only if A(i, j) == 1. This graph has cycles of vertices [3 4 8], [3 8 1 4], and [5 1 4 3 8], among others. Try the commands below to visualize this.\r\n\r\n  g  = graph(A);\r\n  gh = plot(g);\r\n\r\nA Hamiltonian cycle for this graph g is [1 5 6 8 3 4 2 7].\r\n\r\nFor another fun challenge, see: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/45252-restricted-addition-v1 Restricted Addition\u003e","description_html":"\u003cp\u003eYou are given a graph g and asked to find a Hamiltonian cycle of g.\u003c/p\u003e\u003cp\u003eSee \u003ca href = \"https://www.mathworks.com/help/matlab/ref/graph.html\"\u003eMATLAB graph documentation\u003c/a\u003e for details of the graph data structure.\u003c/p\u003e\u003cp\u003eA cycle of g is a sequence of vertices of g such that each adjacent pair of vertices in the sequence share an edge in g and the first and last vertices in the sequence share an edge in g. A Hamiltonian cycle of g is a cycle of g that visits each vertex of g exactly once.\u003c/p\u003e\u003cp\u003eFor example, consider the adjacency matrix below.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = [\r\n   0     0     0     1     1     0     1     1\r\n   0     1     0     1     0     0     1     0\r\n   0     0     0     1     1     0     0     1\r\n   1     1     1     1     0     1     0     1\r\n   1     0     1     0     0     1     0     1\r\n   0     0     0     1     1     0     0     1\r\n   1     1     0     0     0     0     0     0\r\n   1     0     1     1     1     1     0     0];\r\n\u003c/pre\u003e\u003cp\u003eThis corresponds to the graph with vertices labeled 1 through 8 and an edge between two vertices i and j if and only if A(i, j) == 1. This graph has cycles of vertices [3 4 8], [3 8 1 4], and [5 1 4 3 8], among others. Try the commands below to visualize this.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eg  = graph(A);\r\ngh = plot(g);\r\n\u003c/pre\u003e\u003cp\u003eA Hamiltonian cycle for this graph g is [1 5 6 8 3 4 2 7].\u003c/p\u003e\u003cp\u003eFor another fun challenge, see: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/45252-restricted-addition-v1\"\u003eRestricted Addition\u003c/a\u003e\u003c/p\u003e","function_template":"function c = findCycle(g)\r\n    c = 1 : g.numnodes;\r\nend","test_suite":"%%\r\nA = [\r\n   0     0     0     1     1     0     1     1\r\n   0     1     0     1     0     0     1     0\r\n   0     0     0     1     1     0     0     1\r\n   1     1     1     1     0     1     0     1\r\n   1     0     1     0     0     1     0     1\r\n   0     0     0     1     1     0     0     1\r\n   1     1     0     0     0     0     0     0\r\n   1     0     1     1     1     1     0     0];\r\ng = graph(A);\r\nc = findCycle(g);\r\nn = 8;\r\nfor vI = 1 : n\r\n\tv = c(vI);\r\n\tu = c(mod(vI + 1, n) + n * (vI == n - 1));\r\n\t\r\n\tassert(findedge(g, v, u) \u003e 0);\r\nend\r\n\r\n%%\r\nn = 30;\r\np = 0.3;\r\nfor pI = 1 : 5\r\n\tn = n * 2;\r\n\tp = p / 2;\r\n\t\r\n\tx       = rand(n) \u003c p;\r\n\tx       = x + diag(ones(1, n - 1), -1);\r\n\tx(n, 1) = 1;\r\n\tx       = x + x.';\r\n\tr       = randperm(n);\r\n\tx(r, r) = x \u003e 0;\r\n\tg       = graph(x);\r\n\t\r\n\tc = findCycle(g);\r\n\tfor vI = 1 : n\r\n\t\tv = c(vI);\r\n\t\tu = c(mod(vI + 1, n) + n * (vI == n - 1));\r\n\t\t\r\n\t\tassert(findedge(g, v, u) \u003e 0);\r\n\tend\r\nend","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":692,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2020-03-24T02:36:00.000Z","updated_at":"2020-03-24T02:37:44.000Z","published_at":"2020-03-24T02:37:44.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\u003eYou are given a graph g and asked to find a Hamiltonian cycle of g.\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\u003eSee\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/help/matlab/ref/graph.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMATLAB graph documentation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for details of the graph data structure.\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\u003eA cycle of g is a sequence of vertices of g such that each adjacent pair of vertices in the sequence share an edge in g and the first and last vertices in the sequence share an edge in g. A Hamiltonian cycle of g is a cycle of g that visits each vertex of g exactly once.\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\u003eFor example, consider the adjacency matrix below.\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[A = [\\n   0     0     0     1     1     0     1     1\\n   0     1     0     1     0     0     1     0\\n   0     0     0     1     1     0     0     1\\n   1     1     1     1     0     1     0     1\\n   1     0     1     0     0     1     0     1\\n   0     0     0     1     1     0     0     1\\n   1     1     0     0     0     0     0     0\\n   1     0     1     1     1     1     0     0];]]\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\u003eThis corresponds to the graph with vertices labeled 1 through 8 and an edge between two vertices i and j if and only if A(i, j) == 1. This graph has cycles of vertices [3 4 8], [3 8 1 4], and [5 1 4 3 8], among others. Try the commands below to visualize this.\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[g  = graph(A);\\ngh = plot(g);]]\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\u003eA Hamiltonian cycle for this graph g is [1 5 6 8 3 4 2 7].\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\u003eFor another fun challenge, see:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/45252-restricted-addition-v1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eRestricted Addition\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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":1217,"title":"create a circulant matrix","description":"create a circulant matrix","description_html":"\u003cp\u003ecreate a circulant matrix\u003c/p\u003e","function_template":"function t = circulant(c)\r\n%CIRCULANT -Circulant matrix.\r\n%  C = circulant(c)\r\n%\r\n% CIRCULANT.m output a Circulant matrix C with its first column equal to the vector c.%\r\n% input   c         VECTOR first column of the circulant matrix\r\n\r\n   m = length(c);\r\n   c = c(:);                               % build vector of user data\r\n\r\n   cidx = (1:m)';\r\n   ridx = 1:m;\r\n   t = cidx(:,ones(m,1)) - ridx(ones(m,1),:);    \r\n   t = mod(t,m)+1;                         % circulant subscripts\r\n   t(:) = c(t);                            % actual data\r\n","test_suite":"%%\r\nx = [0 0 1];\r\ny_correct = [0 1 0; 0 0 1; 1 0 0];\r\nassert(isequal(circulant(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":1,"created_by":10103,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":135,"test_suite_updated_at":"2013-01-20T04:34:29.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-01-20T04:33:36.000Z","updated_at":"2026-03-02T14:36:36.000Z","published_at":"2013-01-20T04:34:29.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\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"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\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\u003ecreate a circulant matrix\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\"}]}"},{"id":45498,"title":"Trace the path of a harmful chemical in an ecological network","description":"An ecological network consists of the cycles of nature, such as the water cycle, the carbon cycle, the oxygen cycle, etc. Due to human activities, harmful chemicals (or persistent pollutants) are now entering these cycles and may stay there forever. Your job is to track a specific unknown chemical inside an ecological network.\r\n\r\nFor this problem, a network involves *N* _sites_ in nature, labelled as _Site_ 1, _Site_ 2, ..., _Site_ *N*. Researchers have identified an ecological network for you, which is given as a [1 x *N*] row vector called *P*. The network is read as follows: \"A chemical that enters _Site i_ always end up at _Site *P*(i)_\". Consider the following example:\r\n\r\n  If a chemical enters Site:      1 2 3 4 5\r\n     it will end up at Site: P = [3 1 5 2 4]\r\n  \r\nIf a harmful chemical enters the ecological network from _Site 2_, it will be traced to _Site 1_ (which is *P*(2)), then eventually at _Site 3_, then at _Site 5_, then at _Site 4_, then back to _Site 2_. Hence, the path of this chemical is [2 1 3 5 4].\r\n\r\nWrite a function that takes a vector *P* and a starting site *S*. Output the path of the chemical after it enters _Site *S*_ in the given ecological network. You are ensured that:\r\n\r\n* *P* is always a permutation of integers 1 to *N*.\r\n* 2 \u003c= *N* \u003c= 100\r\n* 1 \u003c= *S* \u003c= *N*\r\n\r\nSee sample test cases:\r\n\r\n  \u003e\u003e trace_chemical([3 1 5 2 4],2)\r\n     ans = \r\n         2 1 3 5 4\r\n\u003e\u003e trace_chemical([3 1 5 2 4],1)\r\n     ans =\r\n         1 3 5 4 2\r\n\u003e\u003e trace_chemical([4 1 6 5 2 3],1)\r\n     ans =\r\n         1 4 5 2","description_html":"\u003cp\u003eAn ecological network consists of the cycles of nature, such as the water cycle, the carbon cycle, the oxygen cycle, etc. Due to human activities, harmful chemicals (or persistent pollutants) are now entering these cycles and may stay there forever. Your job is to track a specific unknown chemical inside an ecological network.\u003c/p\u003e\u003cp\u003eFor this problem, a network involves \u003cb\u003eN\u003c/b\u003e \u003ci\u003esites\u003c/i\u003e in nature, labelled as \u003ci\u003eSite\u003c/i\u003e 1, \u003ci\u003eSite\u003c/i\u003e 2, ..., \u003ci\u003eSite\u003c/i\u003e \u003cb\u003eN\u003c/b\u003e. Researchers have identified an ecological network for you, which is given as a [1 x \u003cb\u003eN\u003c/b\u003e] row vector called \u003cb\u003eP\u003c/b\u003e. The network is read as follows: \"A chemical that enters \u003ci\u003eSite i\u003c/i\u003e always end up at \u003ci\u003eSite \u003cb\u003eP\u003c/b\u003e(i)\u003c/i\u003e\". Consider the following example:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eIf a chemical enters Site:      1 2 3 4 5\r\n   it will end up at Site: P = [3 1 5 2 4]\r\n\u003c/pre\u003e\u003cp\u003eIf a harmful chemical enters the ecological network from \u003ci\u003eSite 2\u003c/i\u003e, it will be traced to \u003ci\u003eSite 1\u003c/i\u003e (which is \u003cb\u003eP\u003c/b\u003e(2)), then eventually at \u003ci\u003eSite 3\u003c/i\u003e, then at \u003ci\u003eSite 5\u003c/i\u003e, then at \u003ci\u003eSite 4\u003c/i\u003e, then back to \u003ci\u003eSite 2\u003c/i\u003e. Hence, the path of this chemical is [2 1 3 5 4].\u003c/p\u003e\u003cp\u003eWrite a function that takes a vector \u003cb\u003eP\u003c/b\u003e and a starting site \u003cb\u003eS\u003c/b\u003e. Output the path of the chemical after it enters \u003ci\u003eSite \u003cb\u003eS\u003c/b\u003e\u003c/i\u003e in the given ecological network. You are ensured that:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003cb\u003eP\u003c/b\u003e is always a permutation of integers 1 to \u003cb\u003eN\u003c/b\u003e.\u003c/li\u003e\u003cli\u003e2 \u0026lt;= \u003cb\u003eN\u003c/b\u003e \u0026lt;= 100\u003c/li\u003e\u003cli\u003e1 \u0026lt;= \u003cb\u003eS\u003c/b\u003e \u0026lt;= \u003cb\u003eN\u003c/b\u003e\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eSee sample test cases:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; trace_chemical([3 1 5 2 4],2)\r\n   ans = \r\n       2 1 3 5 4\r\n\u0026gt;\u0026gt; trace_chemical([3 1 5 2 4],1)\r\n   ans =\r\n       1 3 5 4 2\r\n\u0026gt;\u0026gt; trace_chemical([4 1 6 5 2 3],1)\r\n   ans =\r\n       1 4 5 2\r\n\u003c/pre\u003e","function_template":"function y = trace_chemical(P,S)\r\n  y = P;\r\nend","test_suite":"%%\r\nfiletext = fileread('trace_chemical.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\nP = [3 1 5 2 4];\r\nans = [2 1 3 5 4];\r\nassert(isequal(trace_chemical(P,2),ans))\r\n%%\r\nP = [3 1 5 2 4];\r\nans = [1 3 5 4 2];\r\nassert(isequal(trace_chemical(P,1),ans))\r\n%%\r\nP = [2 5 8 6 10 9 3 4 7 1 ];\r\nans = [8 4 6 9 7 3 ];\r\nassert(isequal(trace_chemical(P,8),ans))\r\n%%\r\nP = [36 15 70 23 1 60 35 13 19 41 48 20 49 44 68 29 9 61 51 38 ...\r\n     32 2 40 8 72 39 26 28 67 69 42 76 66 34 47 46 11 77 71 64 ...\r\n     4 37 80 18 74 52 7 33 58 50 27 3 45 55 65 43 21 54 31 6 ...\r\n     24 63 57 30 12 78 22 75 14 10 16 17 81 53 59 25 56 5 73 62 ...\r\n     79 ];\r\nans = [60 6 ];\r\nassert(isequal(trace_chemical(P,60),ans))\r\n%%\r\nP = [8 45 12 53 33 15 29 39 40 21 9 26 32 58 20 43 54 17 48 55 ...\r\n     5 49 37 57 16 46 36 10 34 6 38 50 11 27 22 42 19 4 13 47 ...\r\n     52 2 23 25 35 24 30 56 44 41 28 14 51 31 7 3 1 18 ];\r\nans = [35 22 49 44 25 16 43 23 37 19 48 56 3 12 26 46 24 57 1 8 ...\r\n     39 13 32 50 41 52 14 58 18 17 54 31 38 4 53 51 28 10 21 5 ...\r\n     33 11 9 40 47 30 6 15 20 55 7 29 34 27 36 42 2 45 ];\r\nassert(isequal(trace_chemical(P,35),ans))\r\n%%\r\nP = [9 28 7 42 18 16 30 17 24 20 41 29 13 15 44 8 27 23 12 19 ...\r\n     21 32 40 49 11 47 14 25 35 36 46 38 33 45 34 4 43 48 31 5 ...\r\n     3 10 26 39 37 1 22 6 2 ];\r\nans = [24 49 2 28 25 11 41 3 7 30 36 4 42 10 20 19 12 29 35 34 ...\r\n     45 37 43 26 47 22 32 38 48 6 16 8 17 27 14 15 44 39 31 46 ...\r\n     1 9 ];\r\nassert(isequal(trace_chemical(P,24),ans))\r\n%%\r\nP = [39 27 32 17 22 3 21 8 4 16 45 37 40 2 19 11 51 36 50 43 ...\r\n     13 44 12 30 48 28 42 35 10 14 5 38 15 9 20 18 6 26 31 24 ...\r\n     23 41 34 1 46 7 47 33 49 29 25 ];\r\nans = [11 45 46 7 21 13 40 24 30 14 2 27 42 41 23 12 37 6 3 32 ...\r\n     38 26 28 35 20 43 34 9 4 17 51 25 48 33 15 19 50 29 10 16 ];\r\nassert(isequal(trace_chemical(P,11),ans))\r\n%%\r\nP = [19 10 17 9 18 7 13 14 20 21 5 3 6 16 8 12 15 11 2 4 1];\r\nans = [4 9 20 ];\r\nassert(isequal(trace_chemical(P,4),ans))\r\n%%\r\nP = [12 1 5 66 26 29 64 68 2 33 38 41 55 8 18 49 27 47 22 50 ...\r\n     35 24 16 13 60 34 46 36 6 56 67 30 42 48 19 37 63 57 11 17 ...\r\n     40 59 15 23 45 32 61 44 53 31 28 10 62 9 21 7 52 14 39 51 ...\r\n     58 65 54 43 3 4 20 25 ];\r\nans = [26 34 48 44 23 16 49 53 62 65 3 5 ];\r\nassert(isequal(trace_chemical(P,26),ans))\r\n%%\r\nP = [65 8 29 66 49 72 61 38 18 33 58 62 67 40 20 27 46 1 5 6 ...\r\n     14 75 82 74 23 37 54 22 78 41 4 53 13 47 57 51 17 69 77 71 ...\r\n     64 35 25 44 21 70 19 76 36 10 81 42 60 79 28 31 9 48 3 56 ...\r\n     24 59 11 50 7 26 30 16 52 39 15 2 68 73 34 80 32 12 63 55 ...\r\n     45 43 ];\r\nans = [17 46 70 39 77 32 53 60 56 31 4 66 26 37 ];\r\nassert(isequal(trace_chemical(P,17),ans))\r\n%%\r\nP = [1 17 15 6 13 33 28 36 4 22 44 23 32 40 26 12 41 30 8 34 ...\r\n     37 14 21 5 9 10 29 3 35 38 11 43 31 16 19 27 24 45 39 7 ...\r\n     2 42 20 18 25 ];\r\nans = [14 40 7 28 3 15 26 10 22 ];\r\nassert(isequal(trace_chemical(P,14),ans))\r\n%%\r\nP = [1 17 21 15 6 24 13 5 4 18 2 9 3 29 10 28 12 23 11 25 ...\r\n     20 19 8 16 27 26 7 22 14 ];\r\nans = [10 18 23 8 5 6 24 16 28 22 19 11 2 17 12 9 4 15 ];\r\nassert(isequal(trace_chemical(P,10),ans))\r\n%%\r\nP = [6 2 3 4 1 7 5 ];\r\nans = [3 ];\r\nassert(isequal(trace_chemical(P,3),ans))\r\n%%\r\nP = [11 19 15 23 14 10 3 4 25 7 24 1 18 26 6 16 17 20 12 2 ...\r\n     13 22 9 21 8 5 ];\r\nans = [16 ];\r\nassert(isequal(trace_chemical(P,16),ans))\r\n%%\r\nP = [20 16 5 9 30 28 8 24 14 15 23 4 29 11 22 19 26 17 25 6 ...\r\n     27 18 3 2 1 13 31 12 10 21 7 ];\r\nans = [11 23 3 5 30 21 27 31 7 8 24 2 16 19 25 1 20 6 28 12 ...\r\n     4 9 14 ];\r\nassert(isequal(trace_chemical(P,11),ans))\r\n%%\r\nP = [1 18 16 8 15 21 27 22 23 17 26 19 3 4 9 11 7 6 29 2 ...\r\n     12 25 24 5 14 28 20 10 13 ];\r\nans = [22 25 14 4 8 ];\r\nassert(isequal(trace_chemical(P,22),ans))\r\n%%\r\nP = [38 48 42 87 57 89 92 12 20 62 59 51 26 29 45 55 10 71 44 69 ...\r\n     34 60 30 77 53 11 54 14 23 15 22 43 49 13 41 5 47 91 68 37 ...\r\n     9 3 76 31 85 33 40 1 63 70 18 8 17 35 90 36 24 83 94 21 ...\r\n     73 27 61 78 39 82 64 93 66 6 67 84 56 80 19 50 95 2 75 46 ...\r\n     74 16 81 72 4 25 86 32 28 52 65 58 88 7 79 ];\r\nans = [54 35 41 9 20 69 66 82 16 55 90 52 8 12 51 18 71 67 64 78 ...\r\n     2 48 1 38 91 65 39 68 93 88 32 43 76 50 70 6 89 28 14 29 ...\r\n     23 30 15 45 85 4 87 86 25 53 17 10 62 27 ];\r\nassert(isequal(trace_chemical(P,54),ans))\r\n%%\r\nP = [69 48 11 21 80 50 75 64 41 54 23 82 61 45 25 10 74 63 72 8 ...\r\n     15 81 42 60 59 65 35 37 70 33 76 24 36 49 56 18 38 6 44 39 ...\r\n     4 17 52 51 32 43 1 46 55 73 34 28 58 31 68 29 67 22 66 12 ...\r\n     53 5 16 77 19 7 13 26 57 79 3 47 71 40 14 30 2 20 62 9 ...\r\n     78 27 ];\r\nans = [27 35 56 29 70 79 62 5 80 9 41 4 21 15 25 59 66 7 75 14 ...\r\n     45 32 24 60 12 82 ];\r\nassert(isequal(trace_chemical(P,27),ans))\r\n%%\r\nP = [78 59 84 70 19 82 34 69 29 92 6 51 52 28 10 32 31 33 4 73 ...\r\n     24 89 99 68 64 47 46 95 94 21 53 44 62 26 93 91 58 55 98 79 ...\r\n     11 35 48 40 22 66 87 80 63 43 12 97 13 17 67 20 1 85 60 81 ...\r\n     25 50 88 49 96 90 76 83 36 15 75 23 41 86 39 9 8 54 7 61 ...\r\n     2 72 45 38 16 71 56 37 3 14 27 74 5 57 65 18 42 30 77 ];\r\nans = [85 16 32 44 40 79 7 34 26 47 87 56 20 73 41 11 6 82 72 23 ...\r\n     99 77 8 69 36 91 27 46 66 90 14 28 95 65 96 18 33 62 50 43 ...\r\n     48 80 61 25 64 49 63 88 37 58 ];\r\nassert(isequal(trace_chemical(P,85),ans))\r\n%%\r\nP = [86 17 25 63 38 72 9 64 56 10 7 26 43 28 36 40 24 71 41 22 ...\r\n     27 80 21 1 54 84 42 11 60 73 6 46 78 50 67 66 20 23 77 74 ...\r\n     57 44 85 75 16 13 47 14 29 48 19 58 2 39 81 83 59 33 49 61 ...\r\n     69 53 3 35 8 55 32 18 31 30 12 51 34 65 87 62 5 52 15 45 ...\r\n     4 68 82 76 70 37 79 ];\r\nans = [36 66 55 81 4 63 3 25 54 39 77 5 38 23 21 27 42 44 75 87 ...\r\n     79 15 ];\r\nassert(isequal(trace_chemical(P,36),ans))\r\n%%\r\nP = [25 7 4 6 16 30 24 28 9 3 31 13 10 23 2 26 29 8 5 20 ...\r\n     18 27 21 11 22 17 12 19 1 15 14 ];\r\nans = [21 18 8 28 19 5 16 26 17 29 1 25 22 27 12 13 10 3 4 6 ...\r\n     30 15 2 7 24 11 31 14 23 ];\r\nassert(isequal(trace_chemical(P,21),ans))\r\n%%\r\nP = [30 31 13 37 59 49 28 25 65 61 22 8 43 80 64 18 2 74 46 14 ...\r\n     85 12 62 5 55 67 48 42 78 83 47 15 79 89 34 68 54 90 3 44 ...\r\n     72 40 21 24 60 82 35 50 66 11 41 77 75 7 16 27 73 10 76 71 ...\r\n     33 56 39 53 38 19 36 84 69 81 23 87 9 51 70 86 88 1 26 57 ...\r\n     63 20 4 52 29 45 58 6 17 32 ];\r\nans = [78 1 30 83 4 37 54 7 28 42 40 44 24 5 59 76 86 45 60 71 ...\r\n     23 62 56 27 48 50 11 22 12 8 25 55 16 18 74 51 41 72 87 58 ...\r\n     10 61 33 79 26 67 36 68 84 52 77 88 6 49 66 19 46 82 20 14 ...\r\n     80 57 73 9 65 38 90 32 15 64 53 75 70 81 63 39 3 13 43 21 ...\r\n     85 29 ];\r\nassert(isequal(trace_chemical(P,78),ans))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":32,"test_suite_updated_at":"2020-05-07T02:09:17.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-05-06T19:35:57.000Z","updated_at":"2025-12-07T16:54:53.000Z","published_at":"2020-05-06T19:53:21.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\u003eAn ecological network consists of the cycles of nature, such as the water cycle, the carbon cycle, the oxygen cycle, etc. Due to human activities, harmful chemicals (or persistent pollutants) are now entering these cycles and may stay there forever. Your job is to track a specific unknown chemical inside an ecological network.\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\u003eFor this problem, a network involves\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 \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003esites\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e in nature, labelled as\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\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 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:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSite\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. Researchers have identified an ecological network for you, which is given as a [1 x\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] row vector 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. The network is read as follows: \\\"A chemical that enters\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\u003eSite i\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e always end up at\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\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eP\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e(i)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e\\\". Consider 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[If a chemical enters Site:      1 2 3 4 5\\n   it will end up at Site: P = [3 1 5 2 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\u003eIf a harmful chemical enters the ecological network from\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\u003eSite 2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, it will be traced to\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\u003eSite 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (which is\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(2)), then eventually at\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\u003eSite 3\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then at\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\u003eSite 5\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then at\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\u003eSite 4\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, then back to\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\u003eSite 2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Hence, the path of this chemical is [2 1 3 5 4].\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 a vector\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 and a starting site\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\u003eS\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Output the path of the chemical after it enters\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\u003eSite\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eS\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e in the given ecological network. You are ensured that:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\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 is always a permutation of integers 1 to\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.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e2 \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\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 \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\u003eS\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \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\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\u003eSee sample test cases:\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[\u003e\u003e trace_chemical([3 1 5 2 4],2)\\n   ans = \\n       2 1 3 5 4\\n\u003e\u003e trace_chemical([3 1 5 2 4],1)\\n   ans =\\n       1 3 5 4 2\\n\u003e\u003e trace_chemical([4 1 6 5 2 3],1)\\n   ans =\\n       1 4 5 2]]\u003e\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\"}]}"},{"id":45382,"title":"Find a Hamiltonian Cycle in a Graph","description":"You are given a graph g and asked to find a Hamiltonian cycle of g.\r\n\r\nSee \u003chttps://www.mathworks.com/help/matlab/ref/graph.html MATLAB graph documentation\u003e for details of the graph data structure.\r\n\r\nA cycle of g is a sequence of vertices of g such that each adjacent pair of vertices in the sequence share an edge in g and the first and last vertices in the sequence share an edge in g. A Hamiltonian cycle of g is a cycle of g that visits each vertex of g exactly once.\r\n\r\nFor example, consider the adjacency matrix below.\r\n\r\n  A = [\r\n     0     0     0     1     1     0     1     1\r\n     0     1     0     1     0     0     1     0\r\n     0     0     0     1     1     0     0     1\r\n     1     1     1     1     0     1     0     1\r\n     1     0     1     0     0     1     0     1\r\n     0     0     0     1     1     0     0     1\r\n     1     1     0     0     0     0     0     0\r\n     1     0     1     1     1     1     0     0];\r\n\r\nThis corresponds to the graph with vertices labeled 1 through 8 and an edge between two vertices i and j if and only if A(i, j) == 1. This graph has cycles of vertices [3 4 8], [3 8 1 4], and [5 1 4 3 8], among others. Try the commands below to visualize this.\r\n\r\n  g  = graph(A);\r\n  gh = plot(g);\r\n\r\nA Hamiltonian cycle for this graph g is [1 5 6 8 3 4 2 7].\r\n\r\nFor another fun challenge, see: \u003chttps://www.mathworks.com/matlabcentral/cody/problems/45252-restricted-addition-v1 Restricted Addition\u003e","description_html":"\u003cp\u003eYou are given a graph g and asked to find a Hamiltonian cycle of g.\u003c/p\u003e\u003cp\u003eSee \u003ca href = \"https://www.mathworks.com/help/matlab/ref/graph.html\"\u003eMATLAB graph documentation\u003c/a\u003e for details of the graph data structure.\u003c/p\u003e\u003cp\u003eA cycle of g is a sequence of vertices of g such that each adjacent pair of vertices in the sequence share an edge in g and the first and last vertices in the sequence share an edge in g. A Hamiltonian cycle of g is a cycle of g that visits each vertex of g exactly once.\u003c/p\u003e\u003cp\u003eFor example, consider the adjacency matrix below.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eA = [\r\n   0     0     0     1     1     0     1     1\r\n   0     1     0     1     0     0     1     0\r\n   0     0     0     1     1     0     0     1\r\n   1     1     1     1     0     1     0     1\r\n   1     0     1     0     0     1     0     1\r\n   0     0     0     1     1     0     0     1\r\n   1     1     0     0     0     0     0     0\r\n   1     0     1     1     1     1     0     0];\r\n\u003c/pre\u003e\u003cp\u003eThis corresponds to the graph with vertices labeled 1 through 8 and an edge between two vertices i and j if and only if A(i, j) == 1. This graph has cycles of vertices [3 4 8], [3 8 1 4], and [5 1 4 3 8], among others. Try the commands below to visualize this.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eg  = graph(A);\r\ngh = plot(g);\r\n\u003c/pre\u003e\u003cp\u003eA Hamiltonian cycle for this graph g is [1 5 6 8 3 4 2 7].\u003c/p\u003e\u003cp\u003eFor another fun challenge, see: \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/45252-restricted-addition-v1\"\u003eRestricted Addition\u003c/a\u003e\u003c/p\u003e","function_template":"function c = findCycle(g)\r\n    c = 1 : g.numnodes;\r\nend","test_suite":"%%\r\nA = [\r\n   0     0     0     1     1     0     1     1\r\n   0     1     0     1     0     0     1     0\r\n   0     0     0     1     1     0     0     1\r\n   1     1     1     1     0     1     0     1\r\n   1     0     1     0     0     1     0     1\r\n   0     0     0     1     1     0     0     1\r\n   1     1     0     0     0     0     0     0\r\n   1     0     1     1     1     1     0     0];\r\ng = graph(A);\r\nc = findCycle(g);\r\nn = 8;\r\nfor vI = 1 : n\r\n\tv = c(vI);\r\n\tu = c(mod(vI + 1, n) + n * (vI == n - 1));\r\n\t\r\n\tassert(findedge(g, v, u) \u003e 0);\r\nend\r\n\r\n%%\r\nn = 30;\r\np = 0.3;\r\nfor pI = 1 : 5\r\n\tn = n * 2;\r\n\tp = p / 2;\r\n\t\r\n\tx       = rand(n) \u003c p;\r\n\tx       = x + diag(ones(1, n - 1), -1);\r\n\tx(n, 1) = 1;\r\n\tx       = x + x.';\r\n\tr       = randperm(n);\r\n\tx(r, r) = x \u003e 0;\r\n\tg       = graph(x);\r\n\t\r\n\tc = findCycle(g);\r\n\tfor vI = 1 : n\r\n\t\tv = c(vI);\r\n\t\tu = c(mod(vI + 1, n) + n * (vI == n - 1));\r\n\t\t\r\n\t\tassert(findedge(g, v, u) \u003e 0);\r\n\tend\r\nend","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":692,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2020-03-24T02:36:00.000Z","updated_at":"2020-03-24T02:37:44.000Z","published_at":"2020-03-24T02:37:44.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\u003eYou are given a graph g and asked to find a Hamiltonian cycle of g.\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\u003eSee\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/help/matlab/ref/graph.html\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eMATLAB graph documentation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e for details of the graph data structure.\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\u003eA cycle of g is a sequence of vertices of g such that each adjacent pair of vertices in the sequence share an edge in g and the first and last vertices in the sequence share an edge in g. A Hamiltonian cycle of g is a cycle of g that visits each vertex of g exactly once.\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\u003eFor example, consider the adjacency matrix below.\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[A = [\\n   0     0     0     1     1     0     1     1\\n   0     1     0     1     0     0     1     0\\n   0     0     0     1     1     0     0     1\\n   1     1     1     1     0     1     0     1\\n   1     0     1     0     0     1     0     1\\n   0     0     0     1     1     0     0     1\\n   1     1     0     0     0     0     0     0\\n   1     0     1     1     1     1     0     0];]]\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\u003eThis corresponds to the graph with vertices labeled 1 through 8 and an edge between two vertices i and j if and only if A(i, j) == 1. This graph has cycles of vertices [3 4 8], [3 8 1 4], and [5 1 4 3 8], among others. Try the commands below to visualize this.\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[g  = graph(A);\\ngh = plot(g);]]\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\u003eA Hamiltonian cycle for this graph g is [1 5 6 8 3 4 2 7].\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\u003eFor another fun challenge, see:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/45252-restricted-addition-v1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eRestricted Addition\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\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:\"cycle\"","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:\"cycle\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"cycle\"","","\"","cycle","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f636eb9b378\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f636eb9b2d8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f636eb9a978\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f636eb9b878\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f636eb9b5f8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f636eb9b558\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f636eb9b4b8\u003e":"tag:\"cycle\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f636eb9b4b8\u003e":"tag:\"cycle\""},"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:\"cycle\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"cycle\"","","\"","cycle","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f636eb9b378\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f636eb9b2d8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f636eb9a978\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f636eb9b878\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f636eb9b5f8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f636eb9b558\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f636eb9b4b8\u003e":"tag:\"cycle\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f636eb9b4b8\u003e":"tag:\"cycle\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":1217,"difficulty_rating":"easy"},{"id":45498,"difficulty_rating":"easy-medium"},{"id":45382,"difficulty_rating":"hard"}]}}