SlideShare a Scribd company logo
Topological Sort
(an application of DFS)
Prepare by
Md. Shafiuzzaman
Lecturer, Dept. of CSE, JUST
Topological sort
• We have a set of tasks and a set of dependencies
(precedence constraints) of form “task A must be
done before task B”
• Topological sort: An ordering of the tasks that
conforms with the given dependencies
• Goal: Find a topological sort of the tasks or decide
that there is no such ordering
Topological sort more formally
• Suppose that in a directed graph G = (V, E)
vertices V represent tasks, and each edge (u, v)∊E
means that task u must be done before task v
• What is an ordering of vertices 1, ..., |V| such
that for every edge (u, v), u appears before v in
the ordering?
• Such an ordering is called a topological sort of G
• Note: there can be multiple topological sorts of G
Topological sort more formally
• Is it possible to execute all the tasks in G in an order
that respects all the precedence requirements given
by the graph edges?
• The answer is "yes" if and only if the directed graph
G has no cycle!
(otherwise we have a deadlock)
• Such a G is called a Directed Acyclic Graph, or just a
DAG
Edge classification by DFS
Edge (u,v) of G is classified as a:
(1) Tree edge iff u discovers v during the DFS: P[v] = u
If (u,v) is NOT a tree edge then it is a:
(2) Forward edge iff u is an ancestor of v in the DFS tree
(3) Back edge iff u is a descendant of v in the DFS tree
(4) Cross edge iff u is neither an ancestor nor a
descendant of v
Edge classification by DFS
b
a
Tree edges
Forward edges
Back edges
Cross edges
c
c
The edge classification
depends on the particular
DFS tree!
Edge classification by DFS
b
a
Tree edges
Forward edges
Back edges
Cross edges
c
b
a
c
Both are valid
The edge classification
depends on the particular
DFS tree!
DAGs and back edges
• Can there be a back edge in a DFS on a DAG?
• NO! Back edges close a cycle!
• A graph G is a DAG <=> there is no back edge
classified by DFS(G)
Topological sort
b
a
c
ed
f
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
Time = 1Time = 2
c
d = 1
f = ∞
Next we discover the vertex d
1) Call DFS(G) to compute the
finishing times f[v]
Topological sort
b
a
c
ed
f
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
Time = 2Time = 3
c
d = 1
f = ∞
Next we discover the vertex d
d
d = 2
f = ∞
1) Call DFS(G) to compute the
finishing times f[v]
Topological sort
b
a
c
ed
f
1) Call DFS(G) to compute the
finishing times f[v]
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = 3
f = ∞
d = ∞
f = ∞
Time = 3Time = 4
c
d = 1
f = ∞
Next we discover the vertex d
d
d = 2
f = ∞
Next we discover the vertex f
fd = 3
f = 4
f is done, move back to d
2) as each vertex is finished,
insert it onto the front of a
linked list
f
Topological sort
b
a
c
ed
f
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
Time = 4Time = 5
c
d = 1
f = ∞
Next we discover the vertex d
d
d = 2
f = 5
Next we discover the vertex f
fd = 3
f = 4
f is done, move back to d
d is done, move back to c
1) Call DFS(G) to compute the
finishing times f[v]
fd
Topological sort
b
a
c
ed
f
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = ∞
f = ∞
Time = 5
c
d = 1
f = ∞
Next we discover the vertex d
d
d = 2
f = 5
Next we discover the vertex f
fd = 3
f = 4
f is done, move back to d
d is done, move back to c
Next we discover the vertex e
Time = 6
1) Call DFS(G) to compute the
finishing times f[v]
fd
Topological sort
b
a
c
ed
f
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = 6
f = ∞
Time = 6Time = 7
e
d = 1
f = ∞
Next we discover the vertex d
d
d = 2
f = 5
Next we discover the vertex f
fd = 3
f = 4
f is done, move back to d
d is done, move back to c
Next we discover the vertex e
Both edges from e are
cross edges
e is done, move back to c
1) Call DFS(G) to compute the
finishing times f[v]
fde
Topological sort
b
a
c
ed
f
Let’s say we start the DFS
from the vertex c
d = ∞
f = ∞
d = ∞
f = ∞
d = 6
f = 7
Time = 7Time = 8
e
d = 1
f = ∞
Next we discover the vertex d
d
d = 2
f = 5
Next we discover the vertex f
fd = 3
f = 4
f is done, move back to d
d is done, move back to c
Next we discover the vertex e
e is done, move back to c
1) Call DFS(G) to compute the
finishing times f[v]
fde c is done as wellc
Just a note: If there was (c,f)
edge in the graph, it would be
classified as a forward edge
(in this particular DFS run)
Topological sort
b
a
c
ed
f
Let’s now call DFS visit from
the vertex a
d = ∞
f = ∞
d = ∞
f = ∞
d = 6
f = 7
Time = 9
e
d = 1
f = 8
d
d = 2
f = 5
fd = 3
f = 4
1) Call DFS(G) to compute the
finishing times f[v]
fdec
a
d = 9
f = ∞
Next we discover the vertex c,
but c was already processed
=> (a,c) is a cross edge
Time = 10
Next we discover the vertex b
Topological sort
b
a
c
ed
f
Let’s now call DFS visit from
the vertex a
d = 10
f = ∞
d = 6
f = 7
Time = 10
e
d = 1
f = 8
d
d = 2
f = 5
fd = 3
f = 4
1) Call DFS(G) to compute the
finishing times f[v]
fdec
a
d = 9
f = ∞
Next we discover the vertex c,
but c was already processed
=> (a,c) is a cross edge
Time = 11
Next we discover the vertex b
b is done as (b,d) is a cross
edge => now move back to c
b
d = 10
f = 11
b
Topological sort
b
a
c
ed
f
Let’s now call DFS visit from
the vertex a
d = 6
f = 7
Time = 11
e
d = 1
f = 8
d
d = 2
f = 5
fd = 3
f = 4
1) Call DFS(G) to compute the
finishing times f[v]
fdec
a
d = 9
f = ∞
Next we discover the vertex c,
but c was already processed
=> (a,c) is a cross edge
Time = 12
Next we discover the vertex b
b is done as (b,d) is a cross
edge => now move back to c
b
d = 10
f = 11
b
a is done as well
Topological sort
b
a
c
ed
f
Let’s now call DFS visit from
the vertex a
d = 6
f = 7
Time = 11
e
d = 1
f = 8
d
d = 2
f = 5
fd = 3
f = 4
1) Call DFS(G) to compute the
finishing times f[v]
fdec
a
d = 9
f = 12
Next we discover the vertex c,
but c was already processed
=> (a,c) is a cross edge
Time = 13
Next we discover the vertex b
b is done as (b,d) is a cross
edge => now move back to c
b
d = 10
f = 11
b
a is done as well
a
WE HAVE THE RESULT!
3) return the linked list of
vertices
Topological sort
b
a
c
ed
f
d = 6
f = 7
Time = 11
e
d = 1
f = 8
d
d = 2
f = 5
fd = 3
f = 4
fdec
a
d = 9
f = 12
Time = 13
b
d = 10
f = 11
ba
The linked list is sorted in
decreasing order of finishing
times f[]
Try yourself with different
vertex order for DFS visit
Note: If you redraw the graph
so that all vertices are in a line
ordered by a valid topological
sort, then all edges point
„from left to right“
Back to topological sort
• TOPOLOGICAL-SORT(G):
1) call DFS(G) to compute finishing times f[v] for
each vertex v
2) as each vertex is finished, insert it onto the front
of a linked list
3) return the linked list of vertices
Algorithm for TS
• TOPOLOGICAL-SORT(G):
1) call DFS(G) to compute finishing times f[v] for
each vertex v
2) as each vertex is finished, insert it onto the front
of a linked list
3) return the linked list of vertices
• Note that the result is just a list of vertices in
order of decreasing finish times f[]
Time complexity of TS(G)
• Running time of topological sort:
Θ(n + m)
where n=|V| and m=|E|
• Why? Depth first search takes Θ(n + m) time
in the worst case, and inserting into the front
of a linked list takes Θ(1) time

More Related Content

What's hot

Maximum flow
Maximum flowMaximum flow
Maximum flow
Md. Shafiuzzaman Hira
 
CSE633
CSE633CSE633
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
taimurkhan803
 
Flow Network Talk
Flow Network TalkFlow Network Talk
Flow Network Talk
Imane Haf
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
Akila Krishnamoorthy
 
Lecture 3 tangent & velocity problems
Lecture 3   tangent & velocity problemsLecture 3   tangent & velocity problems
Lecture 3 tangent & velocity problems
njit-ronbrown
 
Graph
GraphGraph
Automata theory - RE to DFA Conversion
Automata theory - RE to DFA ConversionAutomata theory - RE to DFA Conversion
Automata theory - RE to DFA Conversion
Akila Krishnamoorthy
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
Minimum cost maximum flow
Minimum cost maximum flowMinimum cost maximum flow
Minimum cost maximum flow
SaruarChowdhury
 
Sol57
Sol57Sol57
A* (aster) Search Algorithm
A* (aster) Search AlgorithmA* (aster) Search Algorithm
A* (aster) Search Algorithm
Sanzid Kawsar
 
bode_plot By DEV
 bode_plot By DEV bode_plot By DEV
bode_plot By DEV
Devchandra Thakur
 
Max Flow Problem
Max Flow ProblemMax Flow Problem
Max Flow Problem
Guillaume Guérard
 
Lesson 5 Nov 3
Lesson 5 Nov 3Lesson 5 Nov 3
Lesson 5 Nov 3
ingroy
 
Ford Fulkerson Algorithm
Ford Fulkerson AlgorithmFord Fulkerson Algorithm
Ford Fulkerson Algorithm
Adarsh Rotte
 
Transforms
TransformsTransforms
Transforms
nagathangaraj
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
montaser185
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 

What's hot (19)

Maximum flow
Maximum flowMaximum flow
Maximum flow
 
CSE633
CSE633CSE633
CSE633
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Flow Network Talk
Flow Network TalkFlow Network Talk
Flow Network Talk
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
 
Lecture 3 tangent & velocity problems
Lecture 3   tangent & velocity problemsLecture 3   tangent & velocity problems
Lecture 3 tangent & velocity problems
 
Graph
GraphGraph
Graph
 
Automata theory - RE to DFA Conversion
Automata theory - RE to DFA ConversionAutomata theory - RE to DFA Conversion
Automata theory - RE to DFA Conversion
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
Minimum cost maximum flow
Minimum cost maximum flowMinimum cost maximum flow
Minimum cost maximum flow
 
Sol57
Sol57Sol57
Sol57
 
A* (aster) Search Algorithm
A* (aster) Search AlgorithmA* (aster) Search Algorithm
A* (aster) Search Algorithm
 
bode_plot By DEV
 bode_plot By DEV bode_plot By DEV
bode_plot By DEV
 
Max Flow Problem
Max Flow ProblemMax Flow Problem
Max Flow Problem
 
Lesson 5 Nov 3
Lesson 5 Nov 3Lesson 5 Nov 3
Lesson 5 Nov 3
 
Ford Fulkerson Algorithm
Ford Fulkerson AlgorithmFord Fulkerson Algorithm
Ford Fulkerson Algorithm
 
Transforms
TransformsTransforms
Transforms
 
Bellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraqBellmanford . montaser hamza.iraq
Bellmanford . montaser hamza.iraq
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 

Similar to Topological sort

Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
subhashchandra197
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
Traian Rebedea
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
Hossain Md Shakhawat
 
Algorithm
AlgorithmAlgorithm
Algorithm
Shakil Ahmed
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
JahidulIslam47153
 
Ai4 heuristic2
Ai4 heuristic2Ai4 heuristic2
Ai4 heuristic2
Mohammad Faizan
 
Graphs
GraphsGraphs
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
Praveen Yadav
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
kenbot
 
Incremental Topological Ordering (and Cycle Detection)
Incremental Topological Ordering (and Cycle Detection)Incremental Topological Ordering (and Cycle Detection)
Incremental Topological Ordering (and Cycle Detection)
⌨️ Andrey Goder
 
Ppt 1
Ppt 1Ppt 1
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
Krish_ver2
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
A. S. M. Shafi
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
janafridi251
 
13 05-curl-and-divergence
13 05-curl-and-divergence13 05-curl-and-divergence
13 05-curl-and-divergence
Abdelrahman Elshaer
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 
Topological sort
Topological sortTopological sort
Topological sort
jabishah
 
CAPS_Discipline_Training
CAPS_Discipline_TrainingCAPS_Discipline_Training
CAPS_Discipline_Training
Hannah Butler
 
Monad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional ProgrammingMonad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional Programming
Namuk Park
 

Similar to Topological sort (20)

Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
 
Ai4 heuristic2
Ai4 heuristic2Ai4 heuristic2
Ai4 heuristic2
 
Graphs
GraphsGraphs
Graphs
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
Incremental Topological Ordering (and Cycle Detection)
Incremental Topological Ordering (and Cycle Detection)Incremental Topological Ordering (and Cycle Detection)
Incremental Topological Ordering (and Cycle Detection)
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
 
13 05-curl-and-divergence
13 05-curl-and-divergence13 05-curl-and-divergence
13 05-curl-and-divergence
 
lecture 18
lecture 18lecture 18
lecture 18
 
Topological sort
Topological sortTopological sort
Topological sort
 
CAPS_Discipline_Training
CAPS_Discipline_TrainingCAPS_Discipline_Training
CAPS_Discipline_Training
 
Monad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional ProgrammingMonad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional Programming
 

More from Md. Shafiuzzaman Hira

Introduction to Web development
Introduction to Web developmentIntroduction to Web development
Introduction to Web development
Md. Shafiuzzaman Hira
 
Software measurement and estimation
Software measurement and estimationSoftware measurement and estimation
Software measurement and estimation
Md. Shafiuzzaman Hira
 
Why do we test software?
Why do we test software?Why do we test software?
Why do we test software?
Md. Shafiuzzaman Hira
 
Software Requirements engineering
Software Requirements engineeringSoftware Requirements engineering
Software Requirements engineering
Md. Shafiuzzaman Hira
 
Software architectural patterns
Software architectural patternsSoftware architectural patterns
Software architectural patterns
Md. Shafiuzzaman Hira
 
Class based modeling
Class based modelingClass based modeling
Class based modeling
Md. Shafiuzzaman Hira
 
Class diagram
Class diagramClass diagram
Class diagram
Md. Shafiuzzaman Hira
 
State diagram
State diagramState diagram
State diagram
Md. Shafiuzzaman Hira
 
Use case Modeling
Use case ModelingUse case Modeling
Use case Modeling
Md. Shafiuzzaman Hira
 
User stories
User storiesUser stories
User stories
Md. Shafiuzzaman Hira
 
Dev ops
Dev opsDev ops
Agile Methodology
Agile MethodologyAgile Methodology
Agile Methodology
Md. Shafiuzzaman Hira
 
Software Process Model
Software Process ModelSoftware Process Model
Software Process Model
Md. Shafiuzzaman Hira
 
Introduction to Software Engineering Course
Introduction to Software Engineering CourseIntroduction to Software Engineering Course
Introduction to Software Engineering Course
Md. Shafiuzzaman Hira
 
C files
C filesC files
C pointers
C pointersC pointers
C structures
C structuresC structures
C structures
Md. Shafiuzzaman Hira
 
How to Create Python scripts
How to Create Python scriptsHow to Create Python scripts
How to Create Python scripts
Md. Shafiuzzaman Hira
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
Md. Shafiuzzaman Hira
 
Password locker project
Password locker project Password locker project
Password locker project
Md. Shafiuzzaman Hira
 

More from Md. Shafiuzzaman Hira (20)

Introduction to Web development
Introduction to Web developmentIntroduction to Web development
Introduction to Web development
 
Software measurement and estimation
Software measurement and estimationSoftware measurement and estimation
Software measurement and estimation
 
Why do we test software?
Why do we test software?Why do we test software?
Why do we test software?
 
Software Requirements engineering
Software Requirements engineeringSoftware Requirements engineering
Software Requirements engineering
 
Software architectural patterns
Software architectural patternsSoftware architectural patterns
Software architectural patterns
 
Class based modeling
Class based modelingClass based modeling
Class based modeling
 
Class diagram
Class diagramClass diagram
Class diagram
 
State diagram
State diagramState diagram
State diagram
 
Use case Modeling
Use case ModelingUse case Modeling
Use case Modeling
 
User stories
User storiesUser stories
User stories
 
Dev ops
Dev opsDev ops
Dev ops
 
Agile Methodology
Agile MethodologyAgile Methodology
Agile Methodology
 
Software Process Model
Software Process ModelSoftware Process Model
Software Process Model
 
Introduction to Software Engineering Course
Introduction to Software Engineering CourseIntroduction to Software Engineering Course
Introduction to Software Engineering Course
 
C files
C filesC files
C files
 
C pointers
C pointersC pointers
C pointers
 
C structures
C structuresC structures
C structures
 
How to Create Python scripts
How to Create Python scriptsHow to Create Python scripts
How to Create Python scripts
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
 
Password locker project
Password locker project Password locker project
Password locker project
 

Recently uploaded

WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
Jhone kinadey
 
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and MoreManyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
narinav14
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
confluent
 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
Zycus
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
widenerjobeyrl638
 
What is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdfWhat is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdf
kalichargn70th171
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
Optimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptxOptimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptx
WebConnect Pvt Ltd
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 

Recently uploaded (20)

WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
 
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and MoreManyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
bgiolcb
bgiolcbbgiolcb
bgiolcb
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
 
What is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdfWhat is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdf
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
Optimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptxOptimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptx
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 

Topological sort

  • 1. Topological Sort (an application of DFS) Prepare by Md. Shafiuzzaman Lecturer, Dept. of CSE, JUST
  • 2. Topological sort • We have a set of tasks and a set of dependencies (precedence constraints) of form “task A must be done before task B” • Topological sort: An ordering of the tasks that conforms with the given dependencies • Goal: Find a topological sort of the tasks or decide that there is no such ordering
  • 3. Topological sort more formally • Suppose that in a directed graph G = (V, E) vertices V represent tasks, and each edge (u, v)∊E means that task u must be done before task v • What is an ordering of vertices 1, ..., |V| such that for every edge (u, v), u appears before v in the ordering? • Such an ordering is called a topological sort of G • Note: there can be multiple topological sorts of G
  • 4. Topological sort more formally • Is it possible to execute all the tasks in G in an order that respects all the precedence requirements given by the graph edges? • The answer is "yes" if and only if the directed graph G has no cycle! (otherwise we have a deadlock) • Such a G is called a Directed Acyclic Graph, or just a DAG
  • 5. Edge classification by DFS Edge (u,v) of G is classified as a: (1) Tree edge iff u discovers v during the DFS: P[v] = u If (u,v) is NOT a tree edge then it is a: (2) Forward edge iff u is an ancestor of v in the DFS tree (3) Back edge iff u is a descendant of v in the DFS tree (4) Cross edge iff u is neither an ancestor nor a descendant of v
  • 6. Edge classification by DFS b a Tree edges Forward edges Back edges Cross edges c c The edge classification depends on the particular DFS tree!
  • 7. Edge classification by DFS b a Tree edges Forward edges Back edges Cross edges c b a c Both are valid The edge classification depends on the particular DFS tree!
  • 8. DAGs and back edges • Can there be a back edge in a DFS on a DAG? • NO! Back edges close a cycle! • A graph G is a DAG <=> there is no back edge classified by DFS(G)
  • 9. Topological sort b a c ed f Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ Time = 1Time = 2 c d = 1 f = ∞ Next we discover the vertex d 1) Call DFS(G) to compute the finishing times f[v]
  • 10. Topological sort b a c ed f Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ Time = 2Time = 3 c d = 1 f = ∞ Next we discover the vertex d d d = 2 f = ∞ 1) Call DFS(G) to compute the finishing times f[v]
  • 11. Topological sort b a c ed f 1) Call DFS(G) to compute the finishing times f[v] Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = 3 f = ∞ d = ∞ f = ∞ Time = 3Time = 4 c d = 1 f = ∞ Next we discover the vertex d d d = 2 f = ∞ Next we discover the vertex f fd = 3 f = 4 f is done, move back to d 2) as each vertex is finished, insert it onto the front of a linked list f
  • 12. Topological sort b a c ed f Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ Time = 4Time = 5 c d = 1 f = ∞ Next we discover the vertex d d d = 2 f = 5 Next we discover the vertex f fd = 3 f = 4 f is done, move back to d d is done, move back to c 1) Call DFS(G) to compute the finishing times f[v] fd
  • 13. Topological sort b a c ed f Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = ∞ f = ∞ Time = 5 c d = 1 f = ∞ Next we discover the vertex d d d = 2 f = 5 Next we discover the vertex f fd = 3 f = 4 f is done, move back to d d is done, move back to c Next we discover the vertex e Time = 6 1) Call DFS(G) to compute the finishing times f[v] fd
  • 14. Topological sort b a c ed f Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = 6 f = ∞ Time = 6Time = 7 e d = 1 f = ∞ Next we discover the vertex d d d = 2 f = 5 Next we discover the vertex f fd = 3 f = 4 f is done, move back to d d is done, move back to c Next we discover the vertex e Both edges from e are cross edges e is done, move back to c 1) Call DFS(G) to compute the finishing times f[v] fde
  • 15. Topological sort b a c ed f Let’s say we start the DFS from the vertex c d = ∞ f = ∞ d = ∞ f = ∞ d = 6 f = 7 Time = 7Time = 8 e d = 1 f = ∞ Next we discover the vertex d d d = 2 f = 5 Next we discover the vertex f fd = 3 f = 4 f is done, move back to d d is done, move back to c Next we discover the vertex e e is done, move back to c 1) Call DFS(G) to compute the finishing times f[v] fde c is done as wellc Just a note: If there was (c,f) edge in the graph, it would be classified as a forward edge (in this particular DFS run)
  • 16. Topological sort b a c ed f Let’s now call DFS visit from the vertex a d = ∞ f = ∞ d = ∞ f = ∞ d = 6 f = 7 Time = 9 e d = 1 f = 8 d d = 2 f = 5 fd = 3 f = 4 1) Call DFS(G) to compute the finishing times f[v] fdec a d = 9 f = ∞ Next we discover the vertex c, but c was already processed => (a,c) is a cross edge Time = 10 Next we discover the vertex b
  • 17. Topological sort b a c ed f Let’s now call DFS visit from the vertex a d = 10 f = ∞ d = 6 f = 7 Time = 10 e d = 1 f = 8 d d = 2 f = 5 fd = 3 f = 4 1) Call DFS(G) to compute the finishing times f[v] fdec a d = 9 f = ∞ Next we discover the vertex c, but c was already processed => (a,c) is a cross edge Time = 11 Next we discover the vertex b b is done as (b,d) is a cross edge => now move back to c b d = 10 f = 11 b
  • 18. Topological sort b a c ed f Let’s now call DFS visit from the vertex a d = 6 f = 7 Time = 11 e d = 1 f = 8 d d = 2 f = 5 fd = 3 f = 4 1) Call DFS(G) to compute the finishing times f[v] fdec a d = 9 f = ∞ Next we discover the vertex c, but c was already processed => (a,c) is a cross edge Time = 12 Next we discover the vertex b b is done as (b,d) is a cross edge => now move back to c b d = 10 f = 11 b a is done as well
  • 19. Topological sort b a c ed f Let’s now call DFS visit from the vertex a d = 6 f = 7 Time = 11 e d = 1 f = 8 d d = 2 f = 5 fd = 3 f = 4 1) Call DFS(G) to compute the finishing times f[v] fdec a d = 9 f = 12 Next we discover the vertex c, but c was already processed => (a,c) is a cross edge Time = 13 Next we discover the vertex b b is done as (b,d) is a cross edge => now move back to c b d = 10 f = 11 b a is done as well a WE HAVE THE RESULT! 3) return the linked list of vertices
  • 20. Topological sort b a c ed f d = 6 f = 7 Time = 11 e d = 1 f = 8 d d = 2 f = 5 fd = 3 f = 4 fdec a d = 9 f = 12 Time = 13 b d = 10 f = 11 ba The linked list is sorted in decreasing order of finishing times f[] Try yourself with different vertex order for DFS visit Note: If you redraw the graph so that all vertices are in a line ordered by a valid topological sort, then all edges point „from left to right“
  • 21. Back to topological sort • TOPOLOGICAL-SORT(G): 1) call DFS(G) to compute finishing times f[v] for each vertex v 2) as each vertex is finished, insert it onto the front of a linked list 3) return the linked list of vertices
  • 22. Algorithm for TS • TOPOLOGICAL-SORT(G): 1) call DFS(G) to compute finishing times f[v] for each vertex v 2) as each vertex is finished, insert it onto the front of a linked list 3) return the linked list of vertices • Note that the result is just a list of vertices in order of decreasing finish times f[]
  • 23. Time complexity of TS(G) • Running time of topological sort: Θ(n + m) where n=|V| and m=|E| • Why? Depth first search takes Θ(n + m) time in the worst case, and inserting into the front of a linked list takes Θ(1) time