Skiena algorithm 2007 lecture12 topological sort connectivity

21
Lecture 12: Depth-First Search Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/skiena

Transcript of Skiena algorithm 2007 lecture12 topological sort connectivity

Page 1: Skiena algorithm 2007 lecture12 topological sort connectivity

Lecture 12:Depth-First Search

Steven Skiena

Department of Computer ScienceState University of New YorkStony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena

Page 2: Skiena algorithm 2007 lecture12 topological sort connectivity

Problem of the Day

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 across edge (x, y) is an edge where x is neither is an ancestoror descendent of y.

Page 3: Skiena algorithm 2007 lecture12 topological sort connectivity

Depth-First Search

DFS has a neat recursive implementation which eliminatesthe need to explicitly use a stack.Discovery and final times are a convenience to maintain.dfs(graph *g, int v){

edgenode *p; (* temporary pointer *)int y; (* successor vertex *)

if (finished) return; (* allow for search termination *)

discovered[v] = TRUE;time = time + 1;entry time[v] = time;

process vertex early(v);

p = g− >edges[v];while (p ! = NULL) {

y = p− >y;

Page 4: Skiena algorithm 2007 lecture12 topological sort connectivity

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;}

Page 5: Skiena algorithm 2007 lecture12 topological sort connectivity

The Key Idea with DFS

A depth-first search of a graph organizes the edges of thegraph in a precise way.In a DFS of an undirected graph, we assign a direction to eachedge, from the vertex which discover it:

1

2 6

3

4

5

1

23

4

5

6

Page 6: Skiena algorithm 2007 lecture12 topological sort connectivity

Edge Classification for DFS

Every edge is either:3. A Forward Edge

4. A Cross Edge

to a different node

to a decendant1. A Tree Edge

2. A Back Edge

to an ancestor

On any particular DFS or BFS of a directed or undirectedgraph, each edge gets classified as one of the above.

Page 7: Skiena algorithm 2007 lecture12 topological sort connectivity

Edge Classification Implementation

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);}

Page 8: Skiena algorithm 2007 lecture12 topological sort connectivity

DFS: Tree Edges and Back Edges Only

The reason DFS is so important is that it defines a very niceordering to the edges of the graph.In a DFS of an undirected graph, every edge is either a treeedge or a back edge.Why? Suppose we have a forward edge. We would haveencountered (4, 1) when expanding 4, so this is a back edge.

1

2

3 4

Page 9: Skiena algorithm 2007 lecture12 topological sort connectivity

No Cross Edges in DFS

Suppose we have a cross-edge1

2

3 4 6

5 When expanding 2, we would discover

5, so the tree would look like:

1

2

34 5

6

Page 10: Skiena algorithm 2007 lecture12 topological sort connectivity

DFS Application: Finding Cycles

Back edges are the key to finding a cycle in an undirectedgraph.Any back edge going from x to an ancestor y creates a cyclewith 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;

}}

Page 11: Skiena algorithm 2007 lecture12 topological sort connectivity

Articulation Vertices

Suppose you are a terrorist, seeking to disrupt the telephonenetwork. Which station do you blow up?

An articulation vertex is a vertex of a connected graph whosedeletion disconnects the graph.Clearly connectivity is an important concern in the design ofany network.Articulation vertices can be found in O(n(m+n)) – just deleteeach vertex to do a DFS on the remaining graph to see if it isconnected.

Page 12: Skiena algorithm 2007 lecture12 topological sort connectivity

A Faster O(n + m) DFS Algorithm

In a DFS tree, a vertex v (other than the root) is an articulationvertex iff v is not a leaf and some subtree of v has no backedge incident until a proper ancestor of v.

X

The root is a special case since it has no ancestors.

X is an articulation vertex sincethe right subtree does not have a back edge to a proper ancestor.

Leaves cannot bearticulation vertices

Page 13: Skiena algorithm 2007 lecture12 topological sort connectivity

Topological Sorting

A directed, acyclic graph has no directed cycles.D

A

G F

EC

B

A topological sort of a graph is an ordering on the vertices sothat all edges go from left to right.DAGs (and only DAGs) has at least one topological sort (hereG, A, B, C, F, E, D).

Page 14: Skiena algorithm 2007 lecture12 topological sort connectivity

Applications of Topological Sorting

Topological sorting is often useful in scheduling jobs in theirproper sequence. In general, we can use it to order thingsgiven precidence constraints.Example: Dressing schedule from CLR.

Page 15: Skiena algorithm 2007 lecture12 topological sort connectivity

Example: Identifying errors in DNA fragmentassembly

Certain fragments are constrained to be to the left or right ofother fragments, unless there are errors.

A B R A CA C A D AA D A B RD A B R AR A C A D

A B R A C

R A C A D

A C A D A

A D A B R

D A B R A

A B R A C A D A B R A

Solution – build a DAG representing all the left-rightconstraints. Any topological sort of this DAG is a consistantordering. If there are cycles, there must be errors.

Page 16: Skiena algorithm 2007 lecture12 topological sort connectivity

Topological Sorting via DFS

A directed graph is a DAG if and only if no back edges areencountered during a depth-first search.Labeling each of the vertices in the reverse order that they aremarked processed finds a topological sort of a DAG.Why? Consider what happens to each directed edge {x, y} aswe encounter it during the exploration of vertex x:

Page 17: Skiena algorithm 2007 lecture12 topological sort connectivity

Case Analysis

• If y is currently undiscovered, then we then start a DFSof y before we can continue with x. Thus y is markedcompleted before x is, and x appears before y in thetopological order, as it must.

• If y is discovered but not completed, then {x, y} is a backedge, which is forbidden in a DAG.

• If y is completed, then it will have been so labeled beforex. Therefore, x appears before y in the topological order,as it must.

Page 18: Skiena algorithm 2007 lecture12 topological sort connectivity

Topological Sorting Implementation

process vertex late(int v){

push(&sorted,v);}

process edge(int x, int y){

int class;

class = edge classification(x,y);

if (class == BACK)printf(”Warning: directed cycle found, not a DAG”);

}

We push each vertex on a stack soon as we have evaluatedall outgoing edges. The top vertex on the stack always hasno incoming edges from any vertex on the stack, repeatedlypopping them off yields a topological ordering.

Page 19: Skiena algorithm 2007 lecture12 topological sort connectivity

Strongly Connected Components

A directed graph is strongly connected iff there is a directedpath between any two vertices.The strongly connected components of a graph is a partitionof the vertices into subsets (maximal) such that each subset isstrongly connected.

a b

c d g h

e f

Observe that no vertex can be in two maximal components,so it is a partition.

Page 20: Skiena algorithm 2007 lecture12 topological sort connectivity

There is an elegant, linear time algorithm to find the stronglyconnected components of a directed graph using DFS whichis similar to the algorithm for biconnected components.

Page 21: Skiena algorithm 2007 lecture12 topological sort connectivity

Backtracking and Depth-First Search

Depth-first search uses essentially the same idea as backtrack-ing.Both involve exhaustively searching all possibilities byadvancing if it is possible, and backing up as soon as thereis no unexplored possibility for further advancement. Bothare most easily understood as recursive algorithms.