Docsity

Connected Components - Data Structures and Algorithms | CS 245, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Connected Components - Data Structures and Algorithms | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ CS245-2009S-21Connected Components^ David GallesDepartment of Computer ScienceUniversity of San Francisco

21-0:^ Strongly Connected Graph^ Directed Path from every node to every other node^12

453 Strongly Connected

21-2:^ Connected Components^ Subgraph (subset of the vertices) that is stronglyconnected.

7 13 24

5 6 8

21-3:^ Connected Components^ Subgraph (subset of the vertices) that is stronglyconnected.

7 13 24

5 6 8

21-5:^ Connected Components^ Subgraph (subset of the vertices) that is stronglyconnected.

7 13 24

5 6 8

21-6:^ Connected Components^ Connected components of the graph are the^ largest possible^ strongly connected subgraphs^ If we put each vertex in its own component – eachcomponent would be (trivially) strongly connected^ Those would not be the connected componentsof the graph – unless there were no largerconnected subgraphs

21-8:^ DFS Revisited^ We can keep track of the order in which we visitthe elements in a Depth-First Search^ For any vertex v in a DFS:^ d[v] =^ Discovery^ time – when the vertex is firstvisited^ f[v] =^ Finishing^ time – when we have finishedwith a vertex (and all of its children)

21-9:^ DFS Revisited class Edge {public int neighbor;public int next;} void DFS(Edge G[], int vertex, boolean Visited[], int d[], int f[]) {Edge tmp;Visited[vertex] = true;d[vertex] = time++;for (tmp = G[vertex]; tmp != null; tmp = tmp.next) {if (!Visited[tmp.neighbor])DFS(G, tmp.neighbor, Visited);} f[vertex] = time++;}

21-11:^ DFS Example

7 13 24

5 6 8

21-12:^ DFS Example

dd ff

dd ff dd ff

dd ff

21-14:^ DFS Example

d 1d ff

dd ff d 2d ff

dd ff

21-15:^ DFS Example

d 1d^3 ff

dd ff d 2d ff

dd ff

21-17:^ DFS Example

d 1d^3 ff

dd ff d 2d^4 ff

d^5 d ff

21-18:^ DFS Example

d 1d^3 ff

dd ff d 2d^4 ff

d^5 d f^6 f