CS344: Lecture 16

8
CS344: Lecture 16 S. Muthu Muthukrishnan

description

CS344: Lecture 16. S. Muthu Muthukrishnan. E. D. C. B. A. Graph Navigation. BFS: DFS: DFS numbering by start time or finish time. tree, back, forward and cross edges. How to do a DFS and identify what each edge type is? - PowerPoint PPT Presentation

Transcript of CS344: Lecture 16

Page 1: CS344: Lecture 16

CS344: Lecture 16

S. Muthu Muthukrishnan

Page 2: CS344: Lecture 16

Graph Navigation• BFS:• DFS: DFS numbering by start time or finish time.

– tree, back, forward and cross edges.

– How to do a DFS and identify what each edge type is?

– Prove that a directed graph G is acyclic if and only if DFS of G has no back edges.

A

C

E

B

D

A

C

E

B

D

Page 3: CS344: Lecture 16

Details

• Iterative algorithm for BFS uses a queue, that for DFS uses a stack.

• How to use BFS or DFS to test whether a undirected graph is connected?

• Both BFS and DFS take time O(|V|+|E|) given graph in the adjacency list format.

Page 4: CS344: Lecture 16

DAGs

• DAG

• Topological ordering of a DAG G is a linear ordering of the vertices of G such that if (u,v) is an edge in G than v appears before u in the linear ordering. This can be thought of as numbering vertices in the topological order, and we will refer to it as topological numbering.

• Use DFS to do topological numbering.

Page 5: CS344: Lecture 16

Another application• A directed graph G is strongly connected if there

is a path from u to v and v to u for all pairs of vertices u,v.

• Cute trick to check if G is strongly connected:– Pick some v.– Do DFS(v). If some vertex w is not reachable, print

NOT strongly connected. – Reverse edges of G to get G’.– Do DFS(v) in G’. If some vertex w is not reachable,

print NOT strongly connected. Else, print YES. – Time: O(|V|+|E|)

Page 6: CS344: Lecture 16

Strongly connected graph? Example

G:

G’:

a

d

c

b

e

f

g

a

d

c

b

e

f

g

Page 7: CS344: Lecture 16

SC components

• How to partition a graph into strongly connected components (SCC)?

• SCC: Maximal subgraphs such that each vertex can reach all other vertices in the subgraph

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Page 8: CS344: Lecture 16

SC Components Algorithm

• Do DFS of G and put vertices in a stack at their finishing times.

• Reverse edges of G to get G’.

• Do DFS of G’ starting from vertices that get popped off the stack from the first step! Each DFS tree generated will be a SCC.

• Question: What is the graph of SCC components of a directed graph G?