Network Science Depth-First Search Joao Meidanis University of Campinas, Brazil March 24, 2020
Summary Depth-First Search (DFS) Algorithm 1 Example 2 Applications 3 Meidanis (Unicamp) Network Science March 24, 2020 2 / 24
Depth-First Search (DFS) Algorithm Meidanis (Unicamp) Network Science March 24, 2020 3 / 24
Depth-First Search Needs: adjacency lists Provides: edge classification cycle detection topological sort Meidanis (Unicamp) Network Science March 24, 2020 4 / 24
Algorithm — Core function DFS-Visit ( u , Adj ) for v in Adj[u] do if v not in parent then parent[v] ← u DFS-Visit ( v , Adj ) end if end for end function Meidanis (Unicamp) Network Science March 24, 2020 5 / 24
Algorithm — Main function DFS ( V , Adj ) parent ← {} for v in V do if v not in parent then parent[v] ← None DFS-Visit ( v , Adj ) end if end for end function Meidanis (Unicamp) Network Science March 24, 2020 6 / 24
Example Meidanis (Unicamp) Network Science March 24, 2020 7 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 8 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 9 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 10 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 11 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 12 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 13 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 14 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 15 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 16 / 24
Example: directed graph a b c d e f g Meidanis (Unicamp) Network Science March 24, 2020 17 / 24
Example: directed graph a b c d e f Running time O ( V + E ) g linear time Meidanis (Unicamp) Network Science March 24, 2020 18 / 24
Applications Meidanis (Unicamp) Network Science March 24, 2020 19 / 24
Edge classification Depends on DFS itself, not just graph Types of edges: tree edges forward edges backward edges cross edges Meidanis (Unicamp) Network Science March 24, 2020 20 / 24
Algorithm additions Starting and ending times useful to classify edges u forward edges: u → v with v u backward edges: u → v with v u cross edges: u → v with v impossible: Meidanis (Unicamp) Network Science March 24, 2020 21 / 24
Undirected graph no forward edges no cross edges Meidanis (Unicamp) Network Science March 24, 2020 22 / 24
Cycle detection Graph has a cycle ⇐ ⇒ DFS has a backward edge Meidanis (Unicamp) Network Science March 24, 2020 23 / 24
Topological sorting Premises: Acyclic graphs Tasks that depend on one another Results: Topological sort: Safe order for the tasks DFS: reverse order of finishing times Meidanis (Unicamp) Network Science March 24, 2020 24 / 24
Cycle detection DFS has a backward edge = ⇒ Graph has a cycle u backward edge: u → v with v a c b u starts while v active = ⇒ there is a path from v to u f e d g Meidanis (Unicamp) Network Science March 24, 2020 25 / 24
Cycle detection Graph has a cycle = ⇒ DFS has a backward edge v k v 0 : first visited vertex in cycle v 0 v 1 , v 2 , v 3 , . . . , v k : start after v 0 v 1 , v 2 , v 3 , . . . , v k : start before v 0 finishes v 1 v 0 Therefore, v k v 2 and v k → v 0 is a backward edge v 3 Meidanis (Unicamp) Network Science March 24, 2020 26 / 24
Topological sorting Example: getting dressed socks socks → shoes shoes underwear → trousers trousers shirt → trousers belt trousers → belt shirt trousers → shoes underwear socks shoes shirt underwear trousers belt Meidanis (Unicamp) Network Science March 24, 2020 27 / 24
Topological sorting Example: getting dressed socks shoes shirt underwear trousers belt shoes, socks, belt, trousers, underwear, shirt ✛ belt, shoes, trousers, shirt, socks, underwear ✛ Meidanis (Unicamp) Network Science March 24, 2020 28 / 24
Recommend
More recommend