Data Structures an Algorithms CS-0S-9 Connecte Components Davi Galles Department o Computer Science University o San Francisco
9-0: Strongly Connecte Graph Directe Path rom every noe to every other noe Strongly Connecte
9-: Strongly Connecte Graph Directe Path rom every noe to every other noe Strongly Connecte
9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.
9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.
9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.
9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.
9-6: Connecte Components Connecte components o the graph are the largest possible strongly connecte subgraphs I we put each vertex in its own component each component woul be (trivially) strongly connecte Those woul not be the connecte components o the graph unless there were no larger connecte subgraphs
9-: Connecte Components Calculating Connecte Components Two vertices v an v are in the same connecte component i an only i: Directe path rom v to v Directe path rom v to v To in connecte components in irecte paths Use DFS
9-8: DFS Revisite We can keep track o the orer in which we visit the elements in a Depth-First Search For any vertex v in a DFS: [v] = Discovery time when the vertex is irst visite [v] = Finishing time when we have inishe with a vertex (an all o its chilren)
9-9: DFS Revisite class Ege { public int neighbor; public int next; } voi DFS(Ege G[], int vertex, boolean Visite[], int [], int []) { Ege tmp; Visite[vertex] = true; [vertex] = time++; or (tmp = G[vertex]; tmp!= null; tmp = tmp.next) { i (!Visite[tmp.neighbor]) DFS(G, tmp.neighbor, Visite); } [vertex] = time++; }
9-0: DFS Revisite To visit every noe in the graph: TraverseDFS(Ege G[]) { int i; boolean Visite = new boolean[g.length]; int = new int[g.length]; int v = new int[g.length]; time = ; or (i=0; i<g.length; i++) Visite[i] = alse; or (i=0; i<g.length; i++) i (!Visite[i]) DFS(G, i, Visite,, ); }
9-: DFS Example
9-: DFS Example
9-: DFS Example
9-: DFS Example
9-: DFS Example
9-6: DFS Example
9-: DFS Example
9-8: DFS Example 6
9-9: DFS Example 6
9-0: DFS Example 8 6
9-: DFS Example 8 9 6
9-: DFS Example 0 8 9 6
9-: DFS Example 0 8 9 6
9-: DFS Example 0 8 9 6
9-: DFS Example 0 8 9 6
9-6: DFS Example 0 8 9 6
9-: DFS Example 0 8 9 6
9-8: DFS Example 0 8 6 9 6
9-9: DFS Example
9-0: DFS Example
9-: DFS Example
9-: DFS Example
9-: DFS Example
9-: DFS Example
9-: DFS Example 6
9-6: DFS Example 6
9-: DFS Example 8 6
9-8: DFS Example 9 8 6
9-9: DFS Example 0 9 8 6
9-0: DFS Example 0 9 8 6
9-: DFS Example 0 9 8 6
9-: DFS Example 0 9 8 6
9-: DFS Example 0 9 8 6
9-: DFS Example 0 9 8 6
9-: DFS Example 0 6 9 8 6
9-6: Using [] & [] Given two vertices v an v, what o we know i [v ] < [v ]?
9-: Using [] & [] Given two vertices v an v, what o we know i [v ] < [v ]? Either: Path rom v to v Start rom v Eventually visit v Finish v Finish v
9-8: Using [] & [] Given two vertices v an v, what o we know i [v ] < [v ]? Either: Path rom v to v No path rom v to v Start rom v Eventually inish v Start rom v Eventually inish v
9-9: Using [] & [] I [v ] < [v ]: Either a path rom v to v, or no path rom v to v I there is a path rom v to v, then there must be a path rom v to v [v ] < [v ] an a path rom v to v v an v are in the same connecte component
9-0: Calculating paths Path rom v to v in G i an only i there is a path rom v to v in G T G T is the transpose o G G with all eges reverse I ater DFS, [v ] < [v ] Run secon DFS on G T, starting rom v, an v an v are in the same DFS spanning tree v an v must be in the same connecte component
9-: Connecte Components Run DFS on G, calculating [] times Compute G T Run DFS on G T examining noes in inverse orer o inishing times rom irst DFS Any noes that are in the same DFS search tree in G T must be in the same connecte component
9-: Connecte Components Eg.
9-: Connecte Components Eg. 0 8 6 9 6
9-: Connecte Components Eg. 0 8 6 9 6
9-: Connecte Components Eg. 0 8 6 9 6
9-6: Connecte Components Eg.
9-: Connecte Components Eg. 0 6 9 8 6
9-8: Connecte Components Eg. 0 6 9 8 6
9-9: Connecte Components Eg. 0 6 9 8 6
9-60: Topological Sort How coul we use DFS to o a Topological Sort? (Hint Use iscover an/or inish times)
9-6: Topological Sort How coul we use DFS to o a Topological Sort? (Hint Use iscover an/or inish times) (What oes it mean i noe x inishe beore noe y?)
9-6: Topological Sort How coul we use DFS to o a Topological Sort? Do DFS, computing inishing times or each vertex As each vertex is inishe, a to ront o a linke list This list is a vali topological sort