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

PDF
Geom2-5hour3
DOCX
Lkm transormasi fungsi rev (1)
PPT
Area of trapezoids
PPTX
Algorithm, Basic topic of algorithm
PPTX
3D Geometry QA 11
PPTX
A* (aster) Search Algorithm
PPT
2.5 bfs & dfs 02
PPS
Motor Winding for 12 poles, 144 slots
Geom2-5hour3
Lkm transormasi fungsi rev (1)
Area of trapezoids
Algorithm, Basic topic of algorithm
3D Geometry QA 11
A* (aster) Search Algorithm
2.5 bfs & dfs 02
Motor Winding for 12 poles, 144 slots

What's hot (13)

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

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

DOCX
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
PPT
topological_sort_strongly Connected Components
PPTX
Unit 4-PartB of data design and algorithms
PPT
ALG5.1.pptdsfnj,sdhfjk hsdjkfhsdjkfhj ksd hfjksdhfjksd
PPT
Application of dfs
PPT
Topological sort
PPT
25-graphs4-topological-sort.ppt data structures
PDF
Lecture13
PDF
Topological Sort
PPT
Algorithm Design and Complexity - Course 8
PPT
ALG5.1.ppt
PPTX
Topological sort
PPTX
topologicalsort-using c++ as development language.pptx
PPT
Graphs in data structures
PPTX
an EN Mean Value Theorem by Slidesgo.pptx
PPTX
an introduction to Topological Sort.pptx
PDF
U1 L5 DAA.pdf
PPTX
8150.graphs
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
topological_sort_strongly Connected Components
Unit 4-PartB of data design and algorithms
ALG5.1.pptdsfnj,sdhfjk hsdjkfhsdjkfhj ksd hfjksdhfjksd
Application of dfs
Topological sort
25-graphs4-topological-sort.ppt data structures
Lecture13
Topological Sort
Algorithm Design and Complexity - Course 8
ALG5.1.ppt
Topological sort
topologicalsort-using c++ as development language.pptx
Graphs in data structures
an EN Mean Value Theorem by Slidesgo.pptx
an introduction to Topological Sort.pptx
U1 L5 DAA.pdf
8150.graphs
Ad

More from subhashchandra197 (17)

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

Recently uploaded (20)

PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PPT on Performance Review to get promotions
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Sustainable Sites - Green Building Construction
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Construction Project Organization Group 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Well-logging-methods_new................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
web development for engineering and engineering
PPT
Mechanical Engineering MATERIALS Selection
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
UNIT 4 Total Quality Management .pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
additive manufacturing of ss316l using mig welding
Automation-in-Manufacturing-Chapter-Introduction.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT on Performance Review to get promotions
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Sustainable Sites - Green Building Construction
Foundation to blockchain - A guide to Blockchain Tech
OOP with Java - Java Introduction (Basics)
Construction Project Organization Group 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Well-logging-methods_new................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
web development for engineering and engineering
Mechanical Engineering MATERIALS Selection
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
UNIT 4 Total Quality Management .pptx
573137875-Attendance-Management-System-original
additive manufacturing of ss316l using mig welding

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