Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.

Similar presentations


Presentation on theme: "Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first."— Presentation transcript:

1 Main algorithm with recursion: We’ll have a function DFS that initializes, and then
calls DFS-Visit, which is a recursive function and does the depth first search. // start time // we discovered u we are finished going through all of u’s neighbors // finish time CSC317

2 CSC317

3 Run time DFS: O(n + m) with n number vertices and m number edges
Run time DFS: O(n + m) with n number vertices and m number edges. Procedure DFS-Visit is called exactly once for each vertex (only if white and then changed). During the DFS-Visit all adjacent nodes of each vertex are used, so overall includes all edges. Some properties of DFS: 1.) Every vertex v that gets discovered between start of u and end of u, is a child of u in the DFS tree. The v will also finish before u does (see path from u to v to y to x) This can be seen in recursion: DFS-Visit(u) called first; and then calls DFSvisit( v), which finishes first (last in first out, as in a stack) 2.) There is never an edge from a black to a white node (because if a node is black, we already visited all its neighbors). 3.) White path theorem: vertex v is a descendant of vertex u if and only if when the search discovers u, there is a path from u to v consisting entirely of white vertices. CSC317

4 CSC317 4.) Disjoint trees, contained trees, and parenthesis theorem:
Consider vertices u and v in a graph G(V,E). Then one of the following conditions hold: intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and neither u nor v is a descendant of the other in the depth-first forest, interval [u.d, u.f] is contained entirely within [v.d, v.f], and u is a descendant of v (which means u was discovered after v), or interval [v.d, v.f] is contained entirely within [u.d, u.f], and v is a descendant of u (which means v was discovered after u) (vice versa) CSC317

5 CSC317 Application DFS: topological sort:
A directed edge from u to v, means that v happens after u. Topological order: All directed edges only go forward (needs to be acyclic graph DAG). Useful for sorting problems with tasks where one needs to be done before another. Again linear time in vertices and edges O(n+m), since doing DFS. Definitions: A topological sort of a dag G = (V,E) is a linear ordering of all its vertices such that if G contains an edge (u,v) then u appears before v in the ordering. (If the graph contains a cycle, then no linear ordering is possible.) CSC317

6 CSC317

7 How can we do that? We can perform topological sort in time Θ (V+E) since depth-first search takes Θ (V+E) time and it takes O(1) time to insert each of the |V| vertices onto the front of the linked list. CSC317

8 CSC317 Application DFS: strongly connected components:
Definition: Maximal set of vertices for a directed graph C V, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. Usefulness: Groups of people or populations that are connected amongst themselves; brain regions that are strongly connected; figuring out if a network such as telephone lines are strongly connected (you can dial anyone in the network); Web connectivity. We will use Depth First Search (DFS), but not just by itself. CSC317

9 Definition: Maximal set of vertices for a directed graph, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. done ???? CSC317

10 CSC317 Each cluster of strongly connected components is in gray.
For example, there is a path from node c to d, and from node d to c. There is also a path from any node in a,b,e to other nodes in that cluster. If we started DFS from node c, we could discover strongly connected component cluster c and d. But if we started the DFS search from node a, we will actually discover all the nodes of this graph, and not an individual strongly connected component cluster (because b has an arrow out of the cluster going to c, so c would be discovered too, although it is not strongly connected with a,b,e). CSC317

11 Solution: We need to call BFS from the right places to find the strongly connected components!
How are we going to do that? Call DFS on graph G to compute finish times u.f for each vertex u in graph G (this is step a in the figure above) CSC317

12 Definition: Maximal set of vertices for a directed graph, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. done ???? Make a transpose graph CSC317

13 Call DFS on graph G to compute finish times u
Call DFS on graph G to compute finish times u.f for each vertex u in graph G (this is step a in the figure above) Compute the transpose of graph G: GT. This is the graph G, but with all edge directions reversed. Note that G and its transpose have the same strongly connected component clusters. CSC317

14 Definition: Maximal set of vertices for a directed graph, so that for each pair of vertices, u and v, there is a path both from u to v and from v to u. done ???? Make a transpose graph (and now …?) CSC317

15 Call DFS on graph G to compute finish times u
Call DFS on graph G to compute finish times u.f for each vertex u in graph G (this is step a in the figure above) Compute the transpose of graph G: GT. This is the graph G, but with all edge directions reversed. Note that G and its transpose have the same strongly connected component clusters. Call DFS on the transpose graph, but in the DFS loop consider vertices in decreasing order of finish time from the DFS we ran on G (starting from highest finish time). CSC317

16 Since we are considering vertices in decreasing order of finish times from the DFS in panel (a), we first consider vertex b and perform DFS in panel (b) on the transposed graph. We find strongly connected components b,a,e in this way. From this cluster of 3 vertices, there are no are no more vertices going out in the transpose graph. So as in DFS, we choose a new starting node, and again take the largest remaining finish time from panel (a) and therefore start in panel (b) from node c. We find nodes c and d in this way. Again, there are no more nodes going out of the cluster. We start from g, and find strongly connected component cluster g,f. Finally, we also start from vertex h and find only the vertex itself as the last strongly connected component (with itself). CSC317

17 This also shows the idea of strongly connected components
This also shows the idea of strongly connected components. Each cluster has a path between all nodes in the cluster, but between clusters there is only a single arrow (otherwise if there were arrows going both ways, both clusters would form a larger strongly connected component). You could also see that the strongly connected components of graph G and its transpose are the same (just the arrow between strongly connected components is reversed in the transpose graph). CSC317

18 Why it works: The main intuition is that the finish times in the original DFS on graph G, give us the proper starting points for our second DFS, such that we discover a strongly connected component cluster and there are no arrows to other undiscovered clusters each time. For example, the first cluster b,a,e in panel (b), had no out going arrows from these vertices. The second cluster c,d only had out going arrows to the cluster that was already discovered. And so on. There is a lemma and proof in the book, which we did not go over. To get an idea why strongly connected components are interesting, read the paper about web connectivity CSC317


Download ppt "Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first."

Similar presentations


Ads by Google