You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can use it to know which node comes after which or detect cycles.
36
+
The algorithm is easy to understand.
37
+
First, we build the adjacent list (`graph`) and count all the inbound of the node.
38
+
Then we start from the node whose inbound count is 0, adding it in to the `q`.
39
+
For every node we pop out from `q`
40
+
* We remove the node's outbound by decrease 1 on all its neighbor's inbound.
41
+
* Put the node's neighbor to `q` if it has no inbound
42
+
* Put the node into the `order`
43
+
Repeat the process until there is no more node.
44
+
The order in the `order` is the order we are going to encounter when we run through the directed graph.
45
+
If we cannot sort all the nodes in the graph, it means that there are some nodes we couldn't find its starting point, in other words, there are cycles in the graph.
46
+
47
+
Time: O(E+2V) ~= O(E+V)
48
+
we used O(E) to build the graph #[1], O(V) to find the starting point #[2], then traverse all the nodes again #[3].
49
+
Space: O(E+3V) ~= O(E+V), O(E+V) for the adjacent list. O(V) for the `q`, O(V) for the `q_next`.
We can use it to know which node comes after which or detect cycles.
43
-
The algorithm is easy to understand.
44
-
First, we build the adjacent list and count all the inbound of the node.
45
-
Then we start from the node whose inbound count is 0, adding it in to the `q_next`.
46
-
For every node we pop out from q_next
47
-
* We remove the node's outbound by decrease 1 on all its neighbor's inbound.
48
-
* Put the node's neighbor to `q_next` if it has no inbound
49
-
* Put the node into the `q`
50
-
Repeat the process until there is no more node.
51
-
The order in the `q` is the order we are going to encounter when we run through the directed graph.
52
-
If we cannot sort all the nodes in the graph, it means that there are some nodes we couldn't find its starting point, in other words, there are cycles in the graph.
20
+
First, we build a graph of adjacency list #[0]
21
+
0->[2,4,5]
22
+
1->[3,4]
23
+
Meaning before taking 2,4,5 we need to take 0, before taking 3,4 we need to take 1
24
+
if we find a loop back to itself then it is impossible, for example
25
+
0->[2,4,5]
26
+
1->[3,4]
27
+
2->[0,3]
28
+
0->2->0, which is imposible.
53
29
54
-
Time: O(E+2V) ~= O(E+V)
55
-
we used O(E) to build the graph #[1], O(V) to find the starting point #[2], then traverse all the nodes again #[3].
56
-
Space: O(E+3V) ~= O(E+V), O(E+V) for the adjacent list. O(V) for the `q`, O(V) for the `q_next`.
30
+
Now we iterate every course to see if it can loop back to itself in anyway #[1]
31
+
we do this by dfs and search for it self
32
+
if we find itself we find loop
33
+
34
+
The time efficiency is O(V^2+VE), because each dfs in adjacency list is O(V+E) and we do it V times
We can use it to know which node comes after which or detect cycles.
67
+
The algorithm is easy to understand.
68
+
First, we build the adjacent list and count all the inbound of the node.
69
+
Then we start from the node whose inbound count is 0, adding it in to the `q_next`.
70
+
For every node we pop out from q_next
71
+
* We remove the node's outbound by decrease 1 on all its neighbor's inbound.
72
+
* Put the node's neighbor to `q_next` if it has no inbound
73
+
* Put the node into the `q`
74
+
Repeat the process until there is no more node.
75
+
The order in the `q` is the order we are going to encounter when we run through the directed graph.
76
+
If we cannot sort all the nodes in the graph, it means that there are some nodes we couldn't find its starting point, in other words, there are cycles in the graph.
81
77
82
-
78
+
Time: O(E+2V) ~= O(E+V)
79
+
we used O(E) to build the graph #[1], O(V) to find the starting point #[2], then traverse all the nodes again #[3].
80
+
Space: O(E+3V) ~= O(E+V), O(E+V) for the adjacent list. O(V) for the `q`, O(V) for the `q_next`.
0 commit comments