Top Banner
Applications of DFS
28

Application of dfs

Jan 21, 2018

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Application of dfs

Applications of DFS

Page 2: Application of dfs

Directed Acyclic Graph• A directed acyclic graph or DAG is a directed graph with no

directed cycles:

Page 3: Application of dfs

Topological Sorting Topological sort of a DAG:

Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v) ∈ G

Real-world application: Scheduling a dependent graph, Find a feasible course plan for university studies

Page 4: Application of dfs

Topological Sorting Assume a directed acyclic graph G = (V,E).

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.

We can view a topological sort of a graph as an ordering of its vertices along a horizontal line so that all directed edges go from left to right.

If the graph is cyclic, then such ordering is not possible. In other words the topological sorting can be applied on a graph if it is directed and acyclic.

Thus topological sort is different from the usual kind of "sorting"

Page 5: Application of dfs

Topological Sorting TOPOLOGICAL-SORT(G)1. Call DFS(G) to compute finishing time f[v] for

each vertex v. 2. As each vertex is finished, it is inserted into the

front of the linked list.3. Finally return the linked list of vertices

Page 6: Application of dfs

shirtshirt

tietie

jacketjacket

sockssocks

shoesshoeswatchwatch

undershortsundershorts

pantspants

beltbelt Assume that Bumstead can only do one thing at time.

Produce a list of tasks in order such that no task has an edge connecting it to a task earlier in the list.

Professor Bumstead Gets Dressed

Page 7: Application of dfs

Professor Bumstead Gets Dressed

shirtshirt

tietie

jacketjacket

sockssocks

shoesshoeswatchwatch

undershortsundershorts

pantspants

beltbelt

11/16

12/15

6/7

17/18

9/10

13/141/8

2/5

3/4

1. After applying the Depth first search . Timestamp

assigned as d/f

Page 8: Application of dfs

Topological Sort Example

shirtshirt tietie jacketjacketsockssocks shoesshoes watchwatchundershortsundershorts pantspants beltbelt

17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4

2. As each vertex is finished, it was inserted into the front of the linked list 3. Finally the linked list of vertices is returned

Page 9: Application of dfs

Topological Sort Example

shirtshirt tietie jacketjacketsockssocks shoesshoes watchwatchundershortsundershorts pantspants beltbelt

17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4

18 - socks16 - undershorts15 - pants14 - shoes10 - watch8 - shirt7 - belt5 - tie4 - jacket

Shorted According to the finishing time

Page 10: Application of dfs

10

Topological Sort: Definition• Consider the following graph of course prerequisities

111

201

123

213

205220 302

304

306

446

427

402Problem: Find an orderin which all thesecourses can be taken. • To take a course, all of its

prerequisites must be taken first

Example: 111, 123, 201, 213, 304, 306, 427, 205, 446, 220, 302, 402

Page 11: Application of dfs

Strongly connected components

Page 12: Application of dfs

Strongly Connected

• Every pair of vertices are reachable from each other• Graph G is strongly connected if, for every u and v in V,

there is some path from u to v and some path from v to u.

Strongly Connected

Not Strongly Connected

A B

C

D

E

A B

C

E

D

Page 13: Application of dfs

Strongly-Connected Components

A strongly connected component of a graph is a maximal subset of nodes (along with their associated edges) that is strongly connected. Nodes share a strongly connected component if they are inter-reachable.

Page 14: Application of dfs

Strongly Connected Components

a

b

d

c

e

f

g { a , c , g }

{ f , d , e , b }

Page 15: Application of dfs

Transpose of a Directed Graph

Transpose of G = (V,E): GT=(V, ET), where ET={(u, v): (v, u) ∈E}

If G is a DAG then GT is also a DAG If we print the topological order of G in the reverse

order, then it is a topological order of GT

Page 16: Application of dfs

Finding Strongly Connected Components

• Input: A directed graph G = (V,E)• Output: a partition of V into disjoint sets so that each

set defines a strongly connected component of G

Page 17: Application of dfs

Algorithm

• The intention is to: 1. Perform depth first search and label vertices in

post fix order2. compute reversed directed graph3. perform depth first search on reversed graph4. components of this forest are strongly connected

components

Page 18: Application of dfs

Algorithm

Strongly-Connected-Components(G)1. Perform depth first search on graph G that is call DFS(G)

to compute finishing times f[u] for each vertex u.

2. Compute reversed directed graph GT of graph G3. Perform depth first search on reversed graph that is call

DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u]

4. Output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component.

Page 19: Application of dfs

Ad=13f=14

Ad=13f=14

Bd=11f=16

Bd=11f=16

Cd=1f=10

Cd=1f=10

Dd=8f=9

Dd=8f=9

Ed=12f=15

Ed=12f=15

Fd=3f=4

Fd=3f=4

Gd=2f=7

Gd=2f=7

Hd=5f=6

Hd=5f=6

Strongly-Connected Components

DFS on G, starting at c.

Page 20: Application of dfs

Ad=2f=5π=B

Ad=2f=5π=B

Bd=1f=6

π=NIL

Bd=1f=6

π=NIL

Cd=7f=10

π=NIL

Cd=7f=10

π=NIL

Dd=8f=9π=C

Dd=8f=9π=C

Ed=3f=4π=A

Ed=3f=4π=A

Fd=12f=13π=G

Fd=12f=13π=G

Gd=11f=14

π=NIL

Gd=11f=14

π=NIL

Hd=16f=15

π=NIL

Hd=16f=15

π=NIL

DFS on GT

Page 21: Application of dfs

Ad=2f=5π=B

Ad=2f=5π=B

Bd=1f=6

π=NIL

Bd=1f=6

π=NIL

Cd=7f=10

π=NIL

Cd=7f=10

π=NIL

Dd=8f=9π=C

Dd=8f=9π=C

Ed=3f=4π=A

Ed=3f=4π=A

Fd=12f=13π=G

Fd=12f=13π=G

Gd=11f=14

π=NIL

Gd=11f=14

π=NIL

Hd=16f=15

π=NIL

Hd=16f=15

π=NIL

DFS on GT

This is GT, labeled after running DFS(GT). but in the main loop of DFS, the vertices are processed in order of decreasing f[u](from G). That is B E A C D G H F

GT:

Page 22: Application of dfs

Strongly-Connected ComponentsThese are the 4 trees that result, yielding the strongly connected

components. Finally, merge the nodes of any given tree into a super-node, and

draw links between them, showing the resultant acyclic component graph.

a

b c

d

e

f

g h

a b c d

e f g h

abe cd

fg h

Component Graph

Page 23: Application of dfs

Time Complexity AnalysisStrongly-Connected-Components(G)1. call DFS(G) to compute finishing times f[u] for each vertex u.

Cost: O(E+V)2. compute GT Cost: O(E+V)3. call DFS(GT), but in the main loop of DFS, consider the vertices in

order of decreasing f[u] Cost: O(E+V)4. output the vertices of each tree in the depth-first forest of step 3

as a separate strongly connected component.

The graph GT is the transpose of G, which is visualized byreversing the arrows on the digraph.• Cost: O(E+V)

Page 24: Application of dfs

Example

Page 25: Application of dfs

MD. Shakhawat Hossain Student of Computer Science & Engineering Dept.

University of Rajshahi

Page 26: Application of dfs

CSE-201CSE-201

CSE 403CSE 403

CSE 404CSE 404

CSE-101CSE-101

CSE-203CSE-203CSE-202CSE-202

CSE-102CSE-102

CSE-103CSE-103

CSE 303CSE 303

Problem: Find an order in which all these courses can be taken.

• Introduction to Computer → CSE-101• Mathematics → CSE-102• Programming with C → CSE-103• Data Structure → CSE-201• Algorithm → CSE-202• OOP → CSE-203• Compiler Design → CSE 303• Software Engineering → CSE 403• Advanced Software Engineering → CSE 403

• Consider the following graph of course prerequisites

• To take a course, all of its prerequisites must be taken first

Page 27: Application of dfs

Desired Output

CSE 201CSE 201 CSE 403CSE 403 CSE 404CSE 404CSE-101CSE-101 CSE-203CSE-203 CSE-202CSE-202CSE-102CSE-102 CSE-103CSE-103 CSE 303CSE 303

17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4

1. Introduction to Computer → CSE-1012. Mathematics → CSE-1023. Programming with C → CSE-1034. OOP → CSE-2035. Algorithm → CSE-2026. Data Structure → CSE-2017. Compiler Design → CSE 3038. Software Engineering → CSE 4039. Advanced Software Engineering → CSE 403

Page 28: Application of dfs

CSE-201CSE-201

CSE 403CSE 403

CSE 404CSE 404

CSE-101CSE-101

CSE-203CSE-203CSE-202CSE-202

CSE-102CSE-102

CSE-103CSE-103

CSE 303CSE 303