SlideShare a Scribd company logo
Applications of DFS
Directed Acyclic Graph
• A directed acyclic graph or DAG is a directed graph with no
directed cycles:
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
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"
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
shirtshirt
tietie
jacketjacket
sockssocks
shoesshoes
watchwatch
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
Professor Bumstead Gets Dressed
shirtshirt
tietie
jacketjacket
sockssocks
shoesshoes
watchwatch
undershortsundershorts
pantspants
beltbelt
11/16
12/15
6/7
17/18
9/10
13/14
1/8
2/5
3/4
1. After applying the Depth
first search . Timestamp
assigned as d/f
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
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 - socks
16 - undershorts
15 - pants
14 - shoes
10 - watch
8 - shirt
7 - belt
5 - tie
4 - jacket
Shorted According to the finishing time
10
Topological Sort: Definition
• Consider the following graph of course prerequisities
111
201
123
213
205
220 302
304
306
446
427
402Problem: Find an order
in which all these
courses 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
Strongly connected components
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
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.
Strongly Connected Components
a
b
d
c
e
f
g
{ a , c , g }
{ f , d , e , b }
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
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
Algorithm
• The intention is to:
1. Perform depth first search and label vertices in
post fix order
2. compute reversed directed graph
3. perform depth first search on reversed graph
4. components of this forest are strongly connected
components
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 G
3. 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.
A
d=13
f=14
A
d=13
f=14
B
d=11
f=16
B
d=11
f=16
C
d=1
f=10
C
d=1
f=10
D
d=8
f=9
D
d=8
f=9
E
d=12
f=15
E
d=12
f=15
F
d=3
f=4
F
d=3
f=4
G
d=2
f=7
G
d=2
f=7
H
d=5
f=6
H
d=5
f=6
Strongly-Connected Components
DFS on G, starting at c.
A
d=2
f=5
π=B
A
d=2
f=5
π=B
B
d=1
f=6
π=NIL
B
d=1
f=6
π=NIL
C
d=7
f=10
π=NIL
C
d=7
f=10
π=NIL
D
d=8
f=9
π=C
D
d=8
f=9
π=C
E
d=3
f=4
π=A
E
d=3
f=4
π=A
F
d=12
f=13
π=G
F
d=12
f=13
π=G
G
d=11
f=14
π=NIL
G
d=11
f=14
π=NIL
H
d=16
f=15
π=NIL
H
d=16
f=15
π=NIL
DFS on GT
A
d=2
f=5
π=B
A
d=2
f=5
π=B
B
d=1
f=6
π=NIL
B
d=1
f=6
π=NIL
C
d=7
f=10
π=NIL
C
d=7
f=10
π=NIL
D
d=8
f=9
π=C
D
d=8
f=9
π=C
E
d=3
f=4
π=A
E
d=3
f=4
π=A
F
d=12
f=13
π=G
F
d=12
f=13
π=G
G
d=11
f=14
π=NIL
G
d=11
f=14
π=NIL
H
d=16
f=15
π=NIL
H
d=16
f=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
:
Strongly-Connected Components
These 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
Time Complexity Analysis
Strongly-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 by
reversing the arrows on the digraph.
• Cost: O(E+V)
Example
MD. Shakhawat Hossain
Student of Computer Science & Engineering Dept.
University of Rajshahi
CSE-201CSE-201
CSE 403CSE 403
CSE 404CSE 404
CSE-101CSE-101
CSE-203CSE-203
CSE-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
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-101
2. Mathematics → CSE-102
3. Programming with C → CSE-103
4. OOP → CSE-203
5. Algorithm → CSE-202
6. Data Structure → CSE-201
7. Compiler Design → CSE 303
8. Software Engineering → CSE 403
9. Advanced Software Engineering → CSE 403
CSE-201CSE-201
CSE 403CSE 403
CSE 404CSE 404
CSE-101CSE-101
CSE-203CSE-203
CSE-202CSE-202
CSE-102CSE-102
CSE-103CSE-103
CSE 303CSE 303

More Related Content

PPTX
Bfs and Dfs
PPTX
Depth-First Search
PPTX
Depth first search [dfs]
PPTX
DFS and BFS
PPTX
PPTX
Dfs presentation
PPT
Depth First Search ( DFS )
PPTX
Presentation on Breadth First Search (BFS)
Bfs and Dfs
Depth-First Search
Depth first search [dfs]
DFS and BFS
Dfs presentation
Depth First Search ( DFS )
Presentation on Breadth First Search (BFS)

What's hot (20)

PPT
Breadth first search and depth first search
PPTX
Depth first search and breadth first searching
PPT
Graph traversal-BFS & DFS
PPTX
Graph traversals in Data Structures
PPTX
Graph in data structure
PPTX
Depth-First Search
PPT
Multi Head, Multi Tape Turing Machine
PPTX
Polygon mesh
PPTX
Graph representation
PPT
Iterative deepening search
PDF
03 image transform
PPTX
Doubly Linked List
PPTX
Data structure - Graph
PPT
Graph coloring
PPTX
Chapter 9 morphological image processing
PPT
Minimization of DFA
PPTX
Topological Sorting
PPSX
Image Processing: Spatial filters
PDF
Graph Representation
PPT
Floyd Warshall Algorithm
Breadth first search and depth first search
Depth first search and breadth first searching
Graph traversal-BFS & DFS
Graph traversals in Data Structures
Graph in data structure
Depth-First Search
Multi Head, Multi Tape Turing Machine
Polygon mesh
Graph representation
Iterative deepening search
03 image transform
Doubly Linked List
Data structure - Graph
Graph coloring
Chapter 9 morphological image processing
Minimization of DFA
Topological Sorting
Image Processing: Spatial filters
Graph Representation
Floyd Warshall Algorithm
Ad

Viewers also liked (13)

PPTX
Search algorithms master
PPTX
130210107039 2130702
PPT
Heuristic Search
ODP
Hillclimbing search algorthim #introduction
PPTX
Linear and Binary Search Algorithms.(Discrete Mathematics)
PPT
Ch2 3-informed (heuristic) search
PDF
Algorithm Analysis and Design Class Notes
PDF
ADA complete notes
PPT
Bfs and dfs in data structure
PPTX
Design and Analysis of Algorithms
PPT
Hill climbing
PPT
Heuristic Search Techniques {Artificial Intelligence}
PPT
17. Trees and Graphs
Search algorithms master
130210107039 2130702
Heuristic Search
Hillclimbing search algorthim #introduction
Linear and Binary Search Algorithms.(Discrete Mathematics)
Ch2 3-informed (heuristic) search
Algorithm Analysis and Design Class Notes
ADA complete notes
Bfs and dfs in data structure
Design and Analysis of Algorithms
Hill climbing
Heuristic Search Techniques {Artificial Intelligence}
17. Trees and Graphs
Ad

Similar to Application of dfs (20)

PDF
Unit ii divide and conquer -3
PDF
Topological Sort
DOCX
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
PPT
Topological sort
PPT
topological_sort_strongly Connected Components
PPT
Algorithm Design and Complexity - Course 8
PPT
Strongly Connected Components
PDF
Merrk
PPTX
an EN Mean Value Theorem by Slidesgo.pptx
PPTX
an introduction to Topological Sort.pptx
PPTX
Topological sort
PDF
Topological sorting
DOCX
IntroductionTopological sorting is a common operation performed .docx
PPTX
topologicalsort-using c++ as development language.pptx
PPT
ALG5.1.pptdsfnj,sdhfjk hsdjkfhsdjkfhj ksd hfjksdhfjksd
PDF
Sccd and topological sorting
PPT
Unit 3 graph chapter6
DOCX
Cs6660 compiler design november december 2016 Answer key
Unit ii divide and conquer -3
Topological Sort
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
Topological sort
topological_sort_strongly Connected Components
Algorithm Design and Complexity - Course 8
Strongly Connected Components
Merrk
an EN Mean Value Theorem by Slidesgo.pptx
an introduction to Topological Sort.pptx
Topological sort
Topological sorting
IntroductionTopological sorting is a common operation performed .docx
topologicalsort-using c++ as development language.pptx
ALG5.1.pptdsfnj,sdhfjk hsdjkfhsdjkfhj ksd hfjksdhfjksd
Sccd and topological sorting
Unit 3 graph chapter6
Cs6660 compiler design november december 2016 Answer key

More from Hossain Md Shakhawat (20)

PPTX
Recipe for the effective presentaion
PPTX
The Road to Higher study in Japan
PPTX
Islamic jurisprudence
PPTX
Introduction to Medical Imaging
PPTX
Jpeg compression
PPTX
Surah Fatiha
PPT
Decision making and looping
PPT
Decision making and branching
PPT
Digital signature
PPT
Caesar cipher
PPT
Rsa rivest shamir adleman
PPT
Fundamentals of cryptography
PPT
Introduction to programming with c,
PPT
Introduction to digital image processing
PPT
History of computing
PPT
Introduction to Printers
PPT
Input devices_(Mouse and Keyboard)
PPT
Binary search tree(bst)
PPT
Introduction to computer
Recipe for the effective presentaion
The Road to Higher study in Japan
Islamic jurisprudence
Introduction to Medical Imaging
Jpeg compression
Surah Fatiha
Decision making and looping
Decision making and branching
Digital signature
Caesar cipher
Rsa rivest shamir adleman
Fundamentals of cryptography
Introduction to programming with c,
Introduction to digital image processing
History of computing
Introduction to Printers
Input devices_(Mouse and Keyboard)
Binary search tree(bst)
Introduction to computer

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
RMMM.pdf make it easy to upload and study
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Lesson notes of climatology university.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
RMMM.pdf make it easy to upload and study
LDMMIA Reiki Yoga Finals Review Spring Summer
Microbial disease of the cardiovascular and lymphatic systems
Lesson notes of climatology university.
Module 4: Burden of Disease Tutorial Slides S2 2025
Paper A Mock Exam 9_ Attempt review.pdf.
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Weekly quiz Compilation Jan -July 25.pdf
Cell Structure & Organelles in detailed.
A systematic review of self-coping strategies used by university students to ...
Updated Idioms and Phrasal Verbs in English subject
What if we spent less time fighting change, and more time building what’s rig...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers

Application of dfs