1. 程式人生 > 實用技巧 >[LeetCode] 1059. All Paths from Source Lead to Destination

[LeetCode] 1059. All Paths from Source Lead to Destination

Given theedgesof a directed graph, and two nodessourceanddestinationof this graph, determine whether or not all paths starting fromsourceeventually end atdestination, that is:

  • At least one path exists from thesourcenode to thedestinationnode
  • If a path exists from thesourcenode to a node with no outgoing edges, then that node is equal todestination
    .
  • The number of possible paths fromsourcetodestinationis a finite number.

Returntrueif and only if all roads fromsourcelead todestination.

Example 1:

Input: n = 3, edges = [[0,1],[0,2]], source = 0, destination = 2
Output: false
Explanation: It is possible to reach and get stuck on both node 1 and node 2.

Example 2:

Input: n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3
Output: false
Explanation: We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely.

Example 3:

Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3
Output: true

Example 4:

Input: n = 3, edges = [[0,1],[1,1],[1,2]], source = 0, destination = 2
Output: false
Explanation: All paths from the source node end at the destination node, but there are an infinite number of paths, 
such as 0-1-2, 0-1-1-2, 0-1-1-1-2, 0-1-1-1-1-2, and so on.

Example 5:

Input: n = 2, edges = [[0,1],[1,1]], source = 0, destination = 1
Output: false
Explanation: There is infinite self-loop at destination node.

Note:

  1. The given graph may have self loops and parallel edges.
  2. The number of nodesnin the graph is between1and10000
  3. The number of edges in the graph is between0and10000
  4. 0 <= edges.length <= 10000
  5. edges[i].length == 2
  6. 0 <= source <= n - 1
  7. 0 <= destination <= n - 1

從始點到終點的所有路徑。

給定有向圖的邊edges,以及該圖的始點source和目標終點destination,確定從始點source出發的所有路徑是否最終結束於目標終點destination,即:

  • 從始點source 到目標終點destination 存在至少一條路徑
  • 如果存在從始點source 到沒有出邊的節點的路徑,則該節點就是路徑終點。
  • 從始點source到目標終點destination 可能路徑數是有限數字
  • 當從始點source 出發的所有路徑都可以到達目標終點destination 時返回true,否則返回 false。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/all-paths-from-source-lead-to-destination
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這是一道有向圖的題。題目的意思解釋的很清楚,給了起點,終點,所有的邊,請你判斷是不是所有從起點開始的邊都能帶你去到終點。

遇到圖的題,絕大部分還是用DFS去遍歷,得到答案。這個題需要注意的點是

  • 當遇到一個終點(這個點沒有next節點),判斷這個點是不是destination
  • 判斷圖中是否有環 - 染色法

思路不難想,程式碼的實現需要多練,面試才會寫的6。

時間O(V + E)

空間O(n)

Java實現 - hashmap建圖

 1 class Solution {
 2     public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
 3         // build the graph
 4         HashMap<Integer, List<Integer>> graph = new HashMap<>();
 5         for (int[] edge : edges) {
 6             graph.putIfAbsent(edge[0], new ArrayList<>());
 7             graph.get(edge[0]).add(edge[1]);
 8         }
 9         return helper(graph, new HashSet<>(), source, destination);
10     }
11 
12     private boolean helper(Map<Integer, List<Integer>> graph, Set<Integer> visited, int cur, int end) {
13         // base case
14         if (!graph.containsKey(cur)) {
15             return cur == end;
16         }
17         visited.add(cur);
18         for (int neighbor : graph.get(cur)) {
19             if (visited.contains(neighbor) || !helper(graph, visited, neighbor, end)) {
20                 return false;
21             }
22         }
23         visited.remove(cur);
24         return true;
25     }
26 }
View Code

Java實現 - list建圖

 1 class Solution {
 2     public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
 3         // corner case
 4         if (edges == null || edges.length == 0) {
 5             return true;
 6         }
 7 
 8         // normal case
 9         List<Integer>[] g = new List[n];
10         int[] colors = new int[n];
11         buildGraph(g, edges);
12         return dfs(g, source, destination, colors);
13     }
14 
15     // s = source, d = destination
16     private boolean dfs(List<Integer>[] g, int s, int d, int[] colors) {
17         // base case
18         if (g[s] == null || g[s].size() == 0) {
19             return s == d;
20         }
21         colors[s] = 1;
22         for (int next : g[s]) {
23             if (colors[next] == 1) {
24                 return false;
25             }
26             if (colors[next] == 0 && !dfs(g, next, d, colors)) {
27                 return false;
28             }
29             colors[s] = 2;
30         }
31         return true;
32     }
33 
34     private void buildGraph(List<Integer>[] g, int[][] edges) {
35         for (int[] e : edges) {
36             int from = e[0];
37             int to = e[1];
38             if (g[from] == null) {
39                 g[from] = new LinkedList<>();
40             }
41             g[from].add(to);
42         }
43     }
44 }
View Code

LeetCode 題目總結