SlideShare a Scribd company logo
1
Graphs : Topological Sort
reading: Weiss Ch. 9
2
Ordering a graph
• Suppose we have a directed acyclic graph (DAG) of courses, and we
want to find an order in which the courses can be taken.
 Must take all prereqs before you can take a given course. Example:
• [142, 143, 140, 154, 341, 374, 331, 403, 311, 332, 344,
312, 351, 333, 352, 373, 414, 410, 417, 413, 415]
• There might be more than one allowable ordering.
 How can we find a valid ordering of the vertices?
142
143
154
140
311
312
331
351
333
341
344
403
352
373
410
332
374
415
413
417
414
3
Topological Sort
• topological sort: Given a digraph G = (V, E), a total ordering of G's
vertices such that for every edge (v, w) in E, vertex v precedes w in
the ordering. Examples:
 determining the order to recalculate updated cells in a spreadsheet
 finding an order to recompile files that have dependencies
• (any problem of finding an order to perform tasks with dependencies)
142
143
154
140
311
312
331
351
333
341
344
403
352
373
410
332
374
415
413
417
414
4
Topo sort example
• How many valid topological sort orderings can you find for the
vertices in the graph below?
 [A, B, C, D, E, F], [A, B, C, D, F, E],
 [A, B, D, C, E, F], [A, B, D, C, F, E],
 [B, A, C, D, E, F], [B, A, C, D, F, E],
 [B, A, D, C, E, F], [B, A, D, C, F, E],
 [B, C, A, D, E, F], [B, C, A, D, F, E],
 ...
 What if there were a new vertex G unconnected to the others?
C
A
F
D
B E
5
Topo sort: Algorithm 1
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
C
A
F
D
B E
6
Topo sort example
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
 ordering = { B }
C
A
F
D
B E
7
Topo sort example
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
 ordering = { B, C }
C
A
F
D
B E
8
Topo sort example
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
 ordering = { B, C, A }
C
A
F
D
B E
9
Topo sort example
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
 ordering = { B, C, A, D }
C
A
F
D
B E
10
Topo sort example
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
 ordering = { B, C, A, D, F }
C
A
F
D
B E
11
Topo sort example
• function topologicalSort():
 ordering := { }.
 Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
 (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
 ordering = { B, C, A, D, F, E }
C
A
F
D
B E
12
Revised algorithm
• We don't want to literally delete vertices and edges from the graph
while trying to topological sort it; so let's revise the algorithm:
 map := {each vertex  its in-degree}.
 queue := {all vertices with in-degree = 0}.
 ordering := { }.
 Repeat until queue is empty:
• Dequeue the first vertex v from the queue.
• ordering += v.
• Decrease the in-degree of all v's neighbors by 1 in the map.
• queue += {any neighbors whose in-degree is now 0}.
 If all vertices are processed, success.
Otherwise, there is a cycle.

More Related Content

PPTX
kjugyfdtryuytrdtfyugtfdxghjgfdial algorithm.pptx
PPTX
Internet Download Manager Crack Patch Latest IDM Free
PPT
Data structure
PPTX
Topological Sorting
PPTX
Graph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptx
PPTX
Topoloical sort
PDF
Graph applications chapter
kjugyfdtryuytrdtfyugtfdxghjgfdial algorithm.pptx
Internet Download Manager Crack Patch Latest IDM Free
Data structure
Topological Sorting
Graph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptx
Topoloical sort
Graph applications chapter

Similar to 25-graphs4-topological-sort.ppt data structures (20)

PPT
Unit24_TopologicalSort (2).ppt
PPT
Unit24_TopologicalSort.ppt data structures
PPTX
PPTX
Introduction to graphs
PPTX
an EN Mean Value Theorem by Slidesgo.pptx
PPTX
an introduction to Topological Sort.pptx
PDF
Unit ii divide and conquer -3
PPTX
Topological Sort Algorithm.pptx
PPTX
Module 2_Decrease and Conquer_2021 Scheme.pptx
PDF
Algorithms of graph
PPTX
DAG and topological sort.pptx
PDF
14 chapter9 graph_algorithmstopologicalsort_shortestpath
PPTX
Topological sort
PPT
cs201-tree-graph as a data structure.ppt
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
PPTX
topologicalsort-using c++ as development language.pptx
PPTX
Topological sort
PPTX
Topological Sort and BFS
PPT
Topological sort
PPT
Graphs
Unit24_TopologicalSort (2).ppt
Unit24_TopologicalSort.ppt data structures
Introduction to graphs
an EN Mean Value Theorem by Slidesgo.pptx
an introduction to Topological Sort.pptx
Unit ii divide and conquer -3
Topological Sort Algorithm.pptx
Module 2_Decrease and Conquer_2021 Scheme.pptx
Algorithms of graph
DAG and topological sort.pptx
14 chapter9 graph_algorithmstopologicalsort_shortestpath
Topological sort
cs201-tree-graph as a data structure.ppt
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
topologicalsort-using c++ as development language.pptx
Topological sort
Topological Sort and BFS
Topological sort
Graphs
Ad

More from SyedAliShahid3 (13)

PPT
lecture3.pptlecture3 data structures pptt
PPT
SecurityPolicy.ppt SecurityPolicy.ppt
PPTX
webapplicationattacks-101005070110-phpapp02.pptx
PPT
144205230-Cross-Site-Scripting-XSS-ppt.ppt
PPT
linkedLists.ppt presentation on the topic
PPTX
Levels_of_Requirements_SRE.pptx presentation
PPTX
BST.pptx data structures presentation ppt
PPT
DivideAndConquer.pptDivideAndConquer.ppt
PPTX
AVLTrees.pptx data structures presentation
PPT
16-avl-trees.ppt data structures prestentation
PPT
22-graphs1-dfs-bfs.ppt data structures ppt
PPT
dipfinal1--150927115011-lva1-app6891.ppt
PPT
QualityModelsAndAtttribQualityModels.ppt
lecture3.pptlecture3 data structures pptt
SecurityPolicy.ppt SecurityPolicy.ppt
webapplicationattacks-101005070110-phpapp02.pptx
144205230-Cross-Site-Scripting-XSS-ppt.ppt
linkedLists.ppt presentation on the topic
Levels_of_Requirements_SRE.pptx presentation
BST.pptx data structures presentation ppt
DivideAndConquer.pptDivideAndConquer.ppt
AVLTrees.pptx data structures presentation
16-avl-trees.ppt data structures prestentation
22-graphs1-dfs-bfs.ppt data structures ppt
dipfinal1--150927115011-lva1-app6891.ppt
QualityModelsAndAtttribQualityModels.ppt
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Sports Quiz easy sports quiz sports quiz
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Lesson notes of climatology university.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Complications of Minimal Access Surgery at WLH
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
Sports Quiz easy sports quiz sports quiz
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Basic Mud Logging Guide for educational purpose
Lesson notes of climatology university.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial diseases, their pathogenesis and prophylaxis
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Complications of Minimal Access Surgery at WLH
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

25-graphs4-topological-sort.ppt data structures

  • 1. 1 Graphs : Topological Sort reading: Weiss Ch. 9
  • 2. 2 Ordering a graph • Suppose we have a directed acyclic graph (DAG) of courses, and we want to find an order in which the courses can be taken.  Must take all prereqs before you can take a given course. Example: • [142, 143, 140, 154, 341, 374, 331, 403, 311, 332, 344, 312, 351, 333, 352, 373, 414, 410, 417, 413, 415] • There might be more than one allowable ordering.  How can we find a valid ordering of the vertices? 142 143 154 140 311 312 331 351 333 341 344 403 352 373 410 332 374 415 413 417 414
  • 3. 3 Topological Sort • topological sort: Given a digraph G = (V, E), a total ordering of G's vertices such that for every edge (v, w) in E, vertex v precedes w in the ordering. Examples:  determining the order to recalculate updated cells in a spreadsheet  finding an order to recompile files that have dependencies • (any problem of finding an order to perform tasks with dependencies) 142 143 154 140 311 312 331 351 333 341 344 403 352 373 410 332 374 415 413 417 414
  • 4. 4 Topo sort example • How many valid topological sort orderings can you find for the vertices in the graph below?  [A, B, C, D, E, F], [A, B, C, D, F, E],  [A, B, D, C, E, F], [A, B, D, C, F, E],  [B, A, C, D, E, F], [B, A, C, D, F, E],  [B, A, D, C, E, F], [B, A, D, C, F, E],  [B, C, A, D, E, F], [B, C, A, D, F, E],  ...  What if there were a new vertex G unconnected to the others? C A F D B E
  • 5. 5 Topo sort: Algorithm 1 • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v . C A F D B E
  • 6. 6 Topo sort example • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v .  ordering = { B } C A F D B E
  • 7. 7 Topo sort example • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v .  ordering = { B, C } C A F D B E
  • 8. 8 Topo sort example • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v .  ordering = { B, C, A } C A F D B E
  • 9. 9 Topo sort example • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v .  ordering = { B, C, A, D } C A F D B E
  • 10. 10 Topo sort example • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v .  ordering = { B, C, A, D, F } C A F D B E
  • 11. 11 Topo sort example • function topologicalSort():  ordering := { }.  Repeat until graph is empty: • Find a vertex v with in-degree of 0 (no incoming edges).  (If there is no such vertex, the graph cannot be sorted; stop.) • Delete v and all of its outgoing edges from the graph. • ordering += v .  ordering = { B, C, A, D, F, E } C A F D B E
  • 12. 12 Revised algorithm • We don't want to literally delete vertices and edges from the graph while trying to topological sort it; so let's revise the algorithm:  map := {each vertex  its in-degree}.  queue := {all vertices with in-degree = 0}.  ordering := { }.  Repeat until queue is empty: • Dequeue the first vertex v from the queue. • ordering += v. • Decrease the in-degree of all v's neighbors by 1 in the map. • queue += {any neighbors whose in-degree is now 0}.  If all vertices are processed, success. Otherwise, there is a cycle.