{"id":2057,"date":"2018-03-10T23:38:07","date_gmt":"2018-03-11T07:38:07","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2057"},"modified":"2018-03-15T21:47:59","modified_gmt":"2018-03-16T04:47:59","slug":"leetcode-797-all-paths-from-source-to-target","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-797-all-paths-from-source-to-target\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 797. All Paths From Source to Target"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u65e0\u73af\u6709\u5411\u56fe\uff0c\u8fd4\u56de\u6240\u6709\u4ece\u8282\u70b90\u5230\u8282\u70b9n-1\u7684\u8def\u5f84\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/all-paths-from-source-to-target\/description\/\">https:\/\/leetcode.com\/problems\/all-paths-from-source-to-target\/description<\/a><\/p>\n<p>Given a directed, acyclic graph of\u00a0<code>N<\/code>\u00a0nodes.\u00a0 Find all possible paths from node\u00a0<code>0<\/code>\u00a0to node\u00a0<code>N-1<\/code>, and return them in any order.<\/p>\n<p>The graph is given as follows:\u00a0 the nodes are 0, 1, &#8230;, graph.length &#8211; 1.\u00a0 graph[i] is a list of all nodes j for which the edge (i, j) exists.<\/p>\n<pre class=\"decode-attributes:false lang:default decode:true \">Example:\r\nInput: [[1,2], [3], [3], []] \r\nOutput: [[0,1,3],[0,2,3]] \r\nExplanation: The graph looks like this:\r\n0---&gt;1\r\n|    |\r\nv    v\r\n2---&gt;3\r\nThere are two paths: 0 -&gt; 1 -&gt; 3 and 0 -&gt; 2 -&gt; 3.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The number of nodes in the graph will be in the range\u00a0<code>[2, 15]<\/code>.<\/li>\n<li>You can print different paths in any order, but you should keep the order of nodes inside one path.<\/li>\n<\/ul>\n<p><strong>Solution 1: DFS<\/strong><\/p>\n<p>Time complexity: O(n!)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 85 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; allPathsSourceTarget(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {    \r\n    vector&lt;vector&lt;int&gt;&gt; ans;\r\n    vector&lt;int&gt; path(1, 0);     \r\n    dfs(graph, 0, graph.size() - 1, path, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void dfs(const vector&lt;vector&lt;int&gt;&gt;&amp; graph, \r\n           int cur, int dest, vector&lt;int&gt;&amp; path, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {\r\n    if (cur == dest) {\r\n      ans.push_back(path);\r\n      return;\r\n    }\r\n    \r\n    for (int next : graph[cur]) {    \r\n      path.push_back(next);\r\n      dfs(graph, next, dest, path, ans);\r\n      path.pop_back();\r\n    }\r\n  }\r\n};<\/pre>\n<p>&#8220;Cleaner&#8221; Version<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; allPathsSourceTarget(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {    \r\n    vector&lt;vector&lt;int&gt;&gt; ans;\r\n    vector&lt;int&gt; path{0};     \r\n    dfs(graph, path, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void dfs(const vector&lt;vector&lt;int&gt;&gt;&amp; graph, \r\n           vector&lt;int&gt;&amp; path, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {\r\n    if (path.back() == graph.size() - 1) {\r\n      ans.push_back(path);\r\n      return;\r\n    }\r\n    \r\n    for (int next : graph[path.back()]) {\r\n      path.push_back(next);\r\n      dfs(graph, path, ans);\r\n      path.pop_back();\r\n    }\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u65e0\u73af\u6709\u5411\u56fe\uff0c\u8fd4\u56de\u6240\u6709\u4ece\u8282\u70b90\u5230\u8282\u70b9n-1\u7684\u8def\u5f84\u3002 Problem: https:\/\/leetcode.com\/problems\/all-paths-from-source-to-target\/description Given a directed, acyclic graph of\u00a0N\u00a0nodes.\u00a0 Find all possible paths from node\u00a00\u00a0to node\u00a0N-1, and return them in any order. The graph is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,177],"class_list":["post-2057","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2057"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2057\/revisions"}],"predecessor-version":[{"id":2080,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2057\/revisions\/2080"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}