SlideShare a Scribd company logo
1 of 15
Download to read offline
8/31/2020
1
Topological Sort
Material prepared by
Dr. N. Subhash Chandra from Web resources
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
1
2
8/31/2020
2
Example: Topological order:
linear ordering of vertices
of a graph according prerequisites.
Sort 1:
C1, C2, C4, C5, C3, C6, C8,
C7, C10, C13, C12, C14, C15,
C11, C9
Sort 2:
C4, C5, C2, C1, C6, C3, C8,
C15, C7, C9, C10, C11, C13,
C12, C14
Definition of Topological Sort
• Given a directed graph G = (V, E) a topological sort of G is an
ordering of V such that for any edge (u, v), u comes before v in
the ordering.
• Example1:
• Example2:
3
4
8/31/2020
3
Definition of Topological Sort
• Example3: The graph in (a) can be topologically sorted as in (b)
(a) (b)
Topological Sort is not unique
• Topological sort is not unique.
• The following are all topological sort of the graph below:
s1 = {a, b, c, d, e, f, g, h, i}
s2 = {a, c, b, f, e, d, h, g, i}
s3 = {a, b, d, c, e, g, f, h, i}
s4 = {a, c, f, b, e, h, d, g, i}
etc.
5
6
8/31/2020
4
Applications of topological sorting
A common application of topological sorting is in scheduling a
sequence of jobs.
•The jobs are represented by vertices, and there is an edge
from x to y if job x must be completed before job y can be
started
•For example, in constructing a building, the basement must
be completed before the first floor, which must be completed
before the second floor and so on.
•A topological sort gives an order in which we should perform
the jobs.
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
7
8
8/31/2020
5
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
9
10
8/31/2020
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!
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!
11
12
8/31/2020
7
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)
What is a DAG?
• A directed acyclic graph (DAG) is a directed graph without cycles.
• DAGs arrise in modeling many problems that involve prerequisite constraints (construction
projects, course prerequisites, document version control, compilers, etc.)
• Some properties of DAGs:
– Every DAG must have at least one vertex with in-degree zero and at least one vertex
with out-degree zero
• A vertex with in-degree zero is called a source vertex, a vertex with out-degree zero
is called a sink vertex.
– G is a DAG iff each vertex in G is in its own strongly connected component
– Every edge (v, w) in a DAG G has finishingTime[w] < finishingTime[v] in a DFS traversal of
G
Example:
13
14
8/31/2020
8
Topological sort: Source removal Method
Example
15
16
8/31/2020
9
Example: Find Topological sort
A B C D E
A 0 1 1 1 0
B 0 0 1 1 0
C 0 0 0 0 1
D 0 0 0 0 1
E 0 0 0 0 0
Topological order:
A B C D E
A B D C E
Exercise problems
17
18
8/31/2020
10
Topological sort: DFS Method
Example
Top of
Stack
Adjacen
t
Stack Pop
A B A
B C AB
C E ABC
E F ABCE
F --- ABCEF F
E
C
B
D
B
A
G
----
---
D
----
__
---
--
ABCE
ABC
ABD
AB
AB
A
G
E
C
---
D
B
A
G
Topological sorted : G
ABDCEF
19
20
8/31/2020
11
Exercise problems
Problems
21
22
8/31/2020
12
Topological sort Algorithm
• 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
Topological Sort Algorithms: DFS based algorithm
Topological-Sort(G)
{
1. Call dfs AllVertices on G to compute f[v] for each vertex v
2. If G contains a back edge (v, w) (i.e., if f[w] > f[v]) , report error ;
3. else, as each vertex is finished prepend it to a list; // or push in stack
4. Return the list; // list is a valid topological sort
}
• Running time is O(V+E), which is the running time for DFS.
Topological order: A C D B E H F G
23
24
8/31/2020
13
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
Proof of correctness
• Theorem: TOPOLOGICAL-SORT(G) produces a
topological sort of a DAG G
• The TOPOLOGICAL-SORT(G) algorithm does a DFS
on the DAG G, and it lists the nodes of G in order
of decreasing finish times f[]
• We must show that this list satisfies the
topological sort property, namely, that for every
edge (u,v) of G, u appears before v in the list
• Claim: For every edge (u,v) of G: f[v] < f[u] in DFS
25
26
8/31/2020
14
Proof of correctness
“For every edge (u,v) of G, f[v] < f[u] in this DFS”
• The DFS classifies (u,v) as a tree edge, a
forward edge or a cross-edge (it cannot be a
back-edge since G has no cycles):
i. If (u,v) is a tree or a forward edge ⇒ v is a
descendant of u ⇒ f[v] < f[u]
ii. If (u,v) is a cross-edge
Proof of correctness
“For every edge (u,v) of G: f[v] < f[u] in this DFS”
ii. If (u,v) is a cross-edge:
• as (u,v) is a cross-edge, by definition, neither u
is a descendant of v nor v is a descendant of u:
d[u] < f[u] < d[v] < f[v]
or
d[v] < f[v] < d[u] < f[u]
since (u,v) is an edge, v is
surely discovered before
u's exploration completes
f[v] < f[u]
Q.E.D. of Claim
27
28
8/31/2020
15
Proof of correctness
• TOPOLOGICAL-SORT(G) lists the nodes of G
from highest to lowest finishing times
• By the Claim, for every edge (u,v) of G:
f[v] < f[u]
⇒ u will be before v in the algorithm's list
• Q.E.D of Theorem
END
29

More Related Content

What's hot

Motor winding 12 Poles, 144 Slots
Motor winding 12 Poles, 144 SlotsMotor winding 12 Poles, 144 Slots
Motor winding 12 Poles, 144 Slotszlatkodo
 
[Question Paper] Computer Graphics (Old Course) [June / 2014]
[Question Paper] Computer Graphics (Old Course) [June / 2014][Question Paper] Computer Graphics (Old Course) [June / 2014]
[Question Paper] Computer Graphics (Old Course) [June / 2014]Mumbai B.Sc.IT Study
 
Geo May 6, 2009
Geo May 6, 2009Geo May 6, 2009
Geo May 6, 2009Mr. Smith
 
6th Math (C2) - L61--Feb24
6th Math (C2) - L61--Feb246th Math (C2) - L61--Feb24
6th Math (C2) - L61--Feb24jdurst65
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Topological Sorting (Decrease and Conquer)
Topological Sorting (Decrease and Conquer)Topological Sorting (Decrease and Conquer)
Topological Sorting (Decrease and Conquer)Gem WeBlog
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
[Question Paper] Computer Graphics (Revised Course) [June / 2016]
[Question Paper] Computer Graphics (Revised Course) [June / 2016][Question Paper] Computer Graphics (Revised Course) [June / 2016]
[Question Paper] Computer Graphics (Revised Course) [June / 2016]Mumbai B.Sc.IT Study
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Systemsday4
Systemsday4Systemsday4
Systemsday4cirilah
 

What's hot (13)

Motor winding 12 Poles, 144 Slots
Motor winding 12 Poles, 144 SlotsMotor winding 12 Poles, 144 Slots
Motor winding 12 Poles, 144 Slots
 
[Question Paper] Computer Graphics (Old Course) [June / 2014]
[Question Paper] Computer Graphics (Old Course) [June / 2014][Question Paper] Computer Graphics (Old Course) [June / 2014]
[Question Paper] Computer Graphics (Old Course) [June / 2014]
 
Geo May 6, 2009
Geo May 6, 2009Geo May 6, 2009
Geo May 6, 2009
 
Pc 1.7 notes
Pc 1.7 notesPc 1.7 notes
Pc 1.7 notes
 
6th Math (C2) - L61--Feb24
6th Math (C2) - L61--Feb246th Math (C2) - L61--Feb24
6th Math (C2) - L61--Feb24
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Topological Sorting (Decrease and Conquer)
Topological Sorting (Decrease and Conquer)Topological Sorting (Decrease and Conquer)
Topological Sorting (Decrease and Conquer)
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
[Question Paper] Computer Graphics (Revised Course) [June / 2016]
[Question Paper] Computer Graphics (Revised Course) [June / 2016][Question Paper] Computer Graphics (Revised Course) [June / 2016]
[Question Paper] Computer Graphics (Revised Course) [June / 2016]
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Systemsday4
Systemsday4Systemsday4
Systemsday4
 

Similar to Unit ii divide and conquer -3

Similar to Unit ii divide and conquer -3 (20)

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
 
Graphs
GraphsGraphs
Graphs
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
Topological sort
Topological sortTopological sort
Topological sort
 
Lecture13
Lecture13Lecture13
Lecture13
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
 
Topological sort
Topological sortTopological sort
Topological sort
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Unit24_TopologicalSort (2).ppt
Unit24_TopologicalSort (2).pptUnit24_TopologicalSort (2).ppt
Unit24_TopologicalSort (2).ppt
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Graph
GraphGraph
Graph
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 

More from subhashchandra197

More from subhashchandra197 (12)

Unit ii divide and conquer -4
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...Call girls in Ahmedabad High profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 

Unit ii divide and conquer -3

  • 1. 8/31/2020 1 Topological Sort Material prepared by Dr. N. Subhash Chandra from Web resources 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 1 2
  • 2. 8/31/2020 2 Example: Topological order: linear ordering of vertices of a graph according prerequisites. Sort 1: C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15, C11, C9 Sort 2: C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C13, C12, C14 Definition of Topological Sort • Given a directed graph G = (V, E) a topological sort of G is an ordering of V such that for any edge (u, v), u comes before v in the ordering. • Example1: • Example2: 3 4
  • 3. 8/31/2020 3 Definition of Topological Sort • Example3: The graph in (a) can be topologically sorted as in (b) (a) (b) Topological Sort is not unique • Topological sort is not unique. • The following are all topological sort of the graph below: s1 = {a, b, c, d, e, f, g, h, i} s2 = {a, c, b, f, e, d, h, g, i} s3 = {a, b, d, c, e, g, f, h, i} s4 = {a, c, f, b, e, h, d, g, i} etc. 5 6
  • 4. 8/31/2020 4 Applications of topological sorting A common application of topological sorting is in scheduling a sequence of jobs. •The jobs are represented by vertices, and there is an edge from x to y if job x must be completed before job y can be started •For example, in constructing a building, the basement must be completed before the first floor, which must be completed before the second floor and so on. •A topological sort gives an order in which we should perform the jobs. 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 7 8
  • 5. 8/31/2020 5 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 9 10
  • 6. 8/31/2020 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! 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! 11 12
  • 7. 8/31/2020 7 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) What is a DAG? • A directed acyclic graph (DAG) is a directed graph without cycles. • DAGs arrise in modeling many problems that involve prerequisite constraints (construction projects, course prerequisites, document version control, compilers, etc.) • Some properties of DAGs: – Every DAG must have at least one vertex with in-degree zero and at least one vertex with out-degree zero • A vertex with in-degree zero is called a source vertex, a vertex with out-degree zero is called a sink vertex. – G is a DAG iff each vertex in G is in its own strongly connected component – Every edge (v, w) in a DAG G has finishingTime[w] < finishingTime[v] in a DFS traversal of G Example: 13 14
  • 8. 8/31/2020 8 Topological sort: Source removal Method Example 15 16
  • 9. 8/31/2020 9 Example: Find Topological sort A B C D E A 0 1 1 1 0 B 0 0 1 1 0 C 0 0 0 0 1 D 0 0 0 0 1 E 0 0 0 0 0 Topological order: A B C D E A B D C E Exercise problems 17 18
  • 10. 8/31/2020 10 Topological sort: DFS Method Example Top of Stack Adjacen t Stack Pop A B A B C AB C E ABC E F ABCE F --- ABCEF F E C B D B A G ---- --- D ---- __ --- -- ABCE ABC ABD AB AB A G E C --- D B A G Topological sorted : G ABDCEF 19 20
  • 12. 8/31/2020 12 Topological sort Algorithm • 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 Topological Sort Algorithms: DFS based algorithm Topological-Sort(G) { 1. Call dfs AllVertices on G to compute f[v] for each vertex v 2. If G contains a back edge (v, w) (i.e., if f[w] > f[v]) , report error ; 3. else, as each vertex is finished prepend it to a list; // or push in stack 4. Return the list; // list is a valid topological sort } • Running time is O(V+E), which is the running time for DFS. Topological order: A C D B E H F G 23 24
  • 13. 8/31/2020 13 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 Proof of correctness • Theorem: TOPOLOGICAL-SORT(G) produces a topological sort of a DAG G • The TOPOLOGICAL-SORT(G) algorithm does a DFS on the DAG G, and it lists the nodes of G in order of decreasing finish times f[] • We must show that this list satisfies the topological sort property, namely, that for every edge (u,v) of G, u appears before v in the list • Claim: For every edge (u,v) of G: f[v] < f[u] in DFS 25 26
  • 14. 8/31/2020 14 Proof of correctness “For every edge (u,v) of G, f[v] < f[u] in this DFS” • The DFS classifies (u,v) as a tree edge, a forward edge or a cross-edge (it cannot be a back-edge since G has no cycles): i. If (u,v) is a tree or a forward edge ⇒ v is a descendant of u ⇒ f[v] < f[u] ii. If (u,v) is a cross-edge Proof of correctness “For every edge (u,v) of G: f[v] < f[u] in this DFS” ii. If (u,v) is a cross-edge: • as (u,v) is a cross-edge, by definition, neither u is a descendant of v nor v is a descendant of u: d[u] < f[u] < d[v] < f[v] or d[v] < f[v] < d[u] < f[u] since (u,v) is an edge, v is surely discovered before u's exploration completes f[v] < f[u] Q.E.D. of Claim 27 28
  • 15. 8/31/2020 15 Proof of correctness • TOPOLOGICAL-SORT(G) lists the nodes of G from highest to lowest finishing times • By the Claim, for every edge (u,v) of G: f[v] < f[u] ⇒ u will be before v in the algorithm's list • Q.E.D of Theorem END 29