0% found this document useful (0 votes)
26 views

Applications BFS DFS

Uploaded by

mgaganreddy3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Applications BFS DFS

Uploaded by

mgaganreddy3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Applications of BFS

and DFS
Topological Sort

A topological sort of a DAG G(V,E) is a linear ordering of


vertices of G such that if G contains an edge (u,v) then u
appears before v in the ordering.
Topological Sort

A topological sort of a DAG G(V,E) is a linear ordering of


vertices of G such that if G contains an edge (u,v) then u
appears before v in the ordering.

- used to show precedence among events


Topological Sort

• call DFS(G) to compute f[v] for each v

• Aseach vertex is finished, insert it to


the front of a linked list

• return the linked list


Topological Sort
A directed graph G is acyclic if an only if a DFS of G
yields no back edges
Topological Sort

Algorithm is correct.
Strongly Connected
Components
• Strongly connected component of a directed graph
G(V, E) is a maximal set of vertices C ⊆ V
such that for every pair of vertices u and v in C,

u and v are reachable from each other.


Call DFS(G) to compute f[u] for each vertex u

T
Compute G

T
call DFS(G ), consider the vertices in order of decreasing
f[u]
Output the vertices in each tree in the DFS forest (formed in the
previous step) as a separate strongly connected component
Let C, C′ be distinct strongly connected components in
directed graph G(V, E).

Let u, v ∈ C, u′, v′ ∈ C′ . Suppose there is a


path from u to u′ . Then there cannot also be a path

from v to v′ .






Let C, C′ be distinct strongly connected components in
directed graph G(V, E).

• (u, v) ∈ E such that u ∈ C and


Suppose

v ∈ C′. Then f(C) > f(C′).





Let C, C′ be distinct strongly connected components in directed
graph G(V, E).

• Suppose (u, v) ∈ E such that u ∈ C and v ∈ C′. Then


f(C) > f(C′).
T
• Suppose there exists (u, v) ∈ E , u ∈ C, v ∈ C′ . Then
f(C) < f(C′).




You might also like