













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Depth-first search (dfs) algorithm on an undirected graph and how every edge is classified as a tree edge or a cross edge. The implementation of dfs, the concept of forward, tree, back, and cross edges, and their significance in finding cycles and articulation vertices. It also discusses the application of dfs in topological sorting.
Typology: Slides
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Prove that in a breadth-first search on a undirected graph G, every edge in G is either a tree edge or a cross edge, where a cross edge (x, y) is an edge where x is neither is an ancestor or descendent of y.
if (discovered[y] == FALSE) { parent[y] = v; process edge(v,y); dfs(g,y); } else if ((!processed[y]) || (g− >directed)) process edge(v,y); if (finished) return; p = p− >next; } process vertex late(v); time = time + 1; exit time[v] = time; processed[v] = TRUE; }
A depth-first search of a graph organizes the edges of the graph in a precise way. In a DFS of an undirected graph, we assign a direction to each edge, from the vertex which discover it:
1 2 6 3 4 5
1
(^23)
4 5 6
int edge classification(int x, int y) { if (parent[y] == x) return(TREE); if (discovered[y] && !processed[y]) return(BACK); if (processed[y] && (entry time[y]¿entry time[x])) return(FORWARD); if (processed[y] && (entry time[y]¡entry time[x])) return(CROSS); printf(”Warning: unclassified edge (%d,%d)”,x,y); }
The reason DFS is so important is that it defines a very nice ordering to the edges of the graph. In a DFS of an undirected graph, every edge is either a tree edge or a back edge. Why? Suppose we have a forward edge. We would have encountered (4, 1) when expanding 4, so this is a back edge.
1
2
3 4
Back edges are the key to finding a cycle in an undirected graph. Any back edge going from x to an ancestor y creates a cycle with the path in the tree from y to x.
process edge(int x, int y) { if (parent[x]! = y) { (* found back edge! *) printf(”Cycle from %d to %d:”,y,x); find path(y,x,parent); finished = TRUE; } }
Suppose you are a terrorist, seeking to disrupt the telephone network. Which station do you blow up?
An articulation vertex is a vertex of a connected graph whose deletion disconnects the graph. Clearly connectivity is an important concern in the design of any network. Articulation vertices can be found in O(n(m+n)) – just delete each vertex to do a DFS on the remaining graph to see if it is connected.
A directed, acyclic graph has no directed cycles.
D A
G F
C E
B
A topological sort of a graph is an ordering on the vertices so that all edges go from left to right. DAGs (and only DAGs) has at least one topological sort (here G, A, B, C, F, E, D).
Topological sorting is often useful in scheduling jobs in their proper sequence. In general, we can use it to order things given precidence constraints. Example: Dressing schedule from CLR.
A directed graph is a DAG if and only if no back edges are encountered during a depth-first search. Labeling each of the vertices in the reverse order that they are marked processed finds a topological sort of a DAG. Why? Consider what happens to each directed edge {x, y} as we encounter it during the exploration of vertex x:
A directed graph is strongly connected iff there is a directed path between any two vertices. The strongly connected components of a graph is a partition of the vertices into subsets (maximal) such that each subset is strongly connected. a b
c d g h
e f
Observe that no vertex can be in two maximal components, so it is a partition.
There is an elegant, linear time algorithm to find the strongly connected components of a directed graph using DFS which is similar to the algorithm for biconnected components.