SlideShare a Scribd company logo
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 Slots
zlatkodo
 
[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, 2009
Mr. Smith
 
Pc 1.7 notes
Pc 1.7 notesPc 1.7 notes
Pc 1.7 notes
Jonathan Fjelstrom
 
6th Math (C2) - L61--Feb24
6th Math (C2) - L61--Feb246th Math (C2) - L61--Feb24
6th Math (C2) - L61--Feb24
jdurst65
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
Umme 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 firstsearchalgorithm
hansa khan
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
Edhole.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
Systemsday4
cirilah
 

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

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
 
Graphs
GraphsGraphs
Application of dfs
Application of dfsApplication of dfs
Application of dfs
Hossain Md Shakhawat
 
Topological sort
Topological sortTopological sort
Topological sort
Md. Shafiuzzaman Hira
 
Lecture13
Lecture13Lecture13
Lecture13
vaishali_singh
 
Ppt 1
Ppt 1Ppt 1
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
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
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
AbhinavAshish17
 
Topological sort
Topological sortTopological sort
Topological sort
jabishah
 
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
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
LakshyaBaliyan2
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
Jonghoon Park
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Unit24_TopologicalSort (2).ppt
Unit24_TopologicalSort (2).pptUnit24_TopologicalSort (2).ppt
Unit24_TopologicalSort (2).ppt
ShivareddyGangam
 
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
Yi-Lung Tsai
 
Graph
GraphGraph
Algorithm
AlgorithmAlgorithm
Algorithm
Shakil Ahmed
 

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
 
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
 
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
 

More from subhashchandra197

Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptUnit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
subhashchandra197
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxUnit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptx
subhashchandra197
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxData Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptx
subhashchandra197
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
subhashchandra197
 
Data science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxData science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptx
subhashchandra197
 
Unit ii divide and conquer -4
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
subhashchandra197
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
subhashchandra197
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
subhashchandra197
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
subhashchandra197
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
subhashchandra197
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
subhashchandra197
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
subhashchandra197
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
subhashchandra197
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
subhashchandra197
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
subhashchandra197
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
subhashchandra197
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
subhashchandra197
 

More from subhashchandra197 (17)

Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptUnit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxUnit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptx
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxData Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptx
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
 
Data science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxData science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptx
 
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

Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 

Recently uploaded (20)

Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 

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