{"id":9570,"date":"2022-03-11T23:14:40","date_gmt":"2022-03-12T07:14:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9570"},"modified":"2022-03-11T23:19:35","modified_gmt":"2022-03-12T07:19:35","slug":"leetcode-2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2192. All Ancestors of a Node in a Directed Acyclic Graph"},"content":{"rendered":"\n<p>You are given a positive integer&nbsp;<code>n<\/code>&nbsp;representing the number of nodes of a&nbsp;<strong>Directed Acyclic Graph<\/strong>&nbsp;(DAG). The nodes are numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;(<strong>inclusive<\/strong>).<\/p>\n\n\n\n<p>You are also given a 2D integer array&nbsp;<code>edges<\/code>, where&nbsp;<code>edges[i] = [from<sub>i<\/sub>, to<sub>i<\/sub>]<\/code>&nbsp;denotes that there is a&nbsp;<strong>unidirectional<\/strong>&nbsp;edge from&nbsp;<code>from<sub>i<\/sub><\/code>&nbsp;to&nbsp;<code>to<sub>i<\/sub><\/code>&nbsp;in the graph.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list<\/em>&nbsp;<code>answer<\/code><em>, where&nbsp;<\/em><code>answer[i]<\/code><em>&nbsp;is the&nbsp;<strong>list of ancestors<\/strong>&nbsp;of the<\/em>&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;<em>node, sorted in&nbsp;<strong>ascending order<\/strong><\/em>.<\/p>\n\n\n\n<p>A node&nbsp;<code>u<\/code>&nbsp;is an&nbsp;<strong>ancestor<\/strong>&nbsp;of another node&nbsp;<code>v<\/code>&nbsp;if&nbsp;<code>u<\/code>&nbsp;can reach&nbsp;<code>v<\/code>&nbsp;via a set of edges.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/12\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 8, edgeList = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]\n<strong>Output:<\/strong> [[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]\n<strong>Explanation:<\/strong>\nThe above diagram represents the input graph.\n- Nodes 0, 1, and 2 do not have any ancestors.\n- Node 3 has two ancestors 0 and 1.\n- Node 4 has two ancestors 0 and 2.\n- Node 5 has three ancestors 0, 1, and 3.\n- Node 6 has five ancestors 0, 1, 2, 3, and 4.\n- Node 7 has four ancestors 0, 1, 2, and 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/12\/e2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, edgeList = [[0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]\n<strong>Output:<\/strong> [[],[0],[0,1],[0,1,2],[0,1,2,3]]\n<strong>Explanation:<\/strong>\nThe above diagram represents the input graph.\n- Node 0 does not have any ancestor.\n- Node 1 has one ancestor 0.\n- Node 2 has two ancestors 0 and 1.\n- Node 3 has three ancestors 0, 1, and 2.\n- Node 4 has four ancestors 0, 1, 2, and 3.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>0 &lt;= edges.length &lt;= min(2000, n * (n - 1) \/ 2)<\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>0 &lt;= from<sub>i<\/sub>, to<sub>i<\/sub>&nbsp;&lt;= n - 1<\/code><\/li><li><code>from<sub>i<\/sub>&nbsp;!= to<sub>i<\/sub><\/code><\/li><li>There are no duplicate edges.<\/li><li>The graph is&nbsp;<strong>directed<\/strong>&nbsp;and&nbsp;<strong>acyclic<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>For each source node S, add it to all its reachable nodes by traversing the entire graph.<br>In one pass, only traverse each child node at most once.<\/p>\n\n\n\n<p>Time complexity: O(VE)<br>Space complexity: (V+E)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n    vector<vector<int>> ans(n);\n    vector<vector<int>> g(n);\n    for (const auto& e : edges)\n      g[e[0]].push_back(e[1]);    \n    \n    function<void(int, int)> dfs = [&](int s, int u) -> void {\n      for (int v : g[u]) {\n        if (ans[v].empty() || ans[v].back() != s) {\n          ans[v].push_back(s);\n          dfs(s, v);\n        }\n      }\n    };\n    \n    for (int i = 0; i < n; ++i)\n      dfs(i, i);  \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a positive integer&nbsp;n&nbsp;representing the number of nodes of a&nbsp;Directed Acyclic Graph&nbsp;(DAG). The nodes are numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;(inclusive). You are also given&#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":[33,77,177],"class_list":["post-9570","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9570","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=9570"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9570\/revisions"}],"predecessor-version":[{"id":9575,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9570\/revisions\/9575"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}