SlideShare a Scribd company logo
TOPOLOGICAL SORT
Definition
 A topological sort of a Directed Acylic Graph
(DAG) is a linear ordering of all its vertices such
that if there is a path from vi to vk then vi appears
before vk in the ordering.
Algorithm for finding a topological sort
Initialize TSArray to null
for (i=1; i<=numofvertices; i++)
 v = find vertex of degree zero
 if v is null
 Graph has a cycle. Exit
 else
 add v to TSArray
 remove v and all its outgoing vertices
Print TSArray
Algorithm for enumerating all topological sorts
TopologicalSorts()
compute the indegree count array, Indegree[ ]
initialize TSArray[ ] to null
for each vertex v
if indegree[v] == zero
push Indegree[ ], TSArray[ ], v
while (stack not empty)
pop Indegree[ ], TSArray[ ], v
add v to TSArray
for each x adjacent to v, Indegree[x]--
for each vertex v
if indegree[v] == zero
push Indegree[ ], TSArray[ ], v
Find out the ordering
1 2
4
6 7
53
Solution
•1 2 5 4 3 7 6
•1 2 5 4 7 3 6
•Find out other possibilities.
2.2 topological sort 02
How many squares can you create in this figure by connecting any 4
dots (the corners of a square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and
1 sixteen-cell triangle.
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and
1 sixteen-cell triangle.

More Related Content

PPTX
PPTX
Graphing polynomial functions by tabby
PDF
Matlab file
PPTX
Graph Algorithms: Breadth-First Search (BFS)
PDF
Topological Sort
PPTX
Topological sort
PDF
Algorithms of graph
PDF
Skiena algorithm 2007 lecture12 topological sort connectivity
Graphing polynomial functions by tabby
Matlab file
Graph Algorithms: Breadth-First Search (BFS)
Topological Sort
Topological sort
Algorithms of graph
Skiena algorithm 2007 lecture12 topological sort connectivity

Viewers also liked (20)

PPT
Biconnected components (13024116056)
PPTX
Topological sort
PPT
Trees
PDF
Advance data structure & algorithm
PDF
Distributed Graph Algorithms
PPT
1535 graph algorithms
PPT
Graph algorithm
PDF
18 Basic Graph Algorithms
PPTX
Graph Traversal Algorithm
PPT
PDF
Skiena algorithm 2007 lecture10 graph data strctures
PPT
1.9 b trees eg 03
PPT
5.2 divede and conquer 03
PDF
문제는 한글이 잘 구현되는가?
PPT
1.9 b trees 02
DOCX
CV Belinda Wahl 2015
PPT
4.2 bst 02
PPT
2.4 mst prim &kruskal demo
DOC
RESUME-ARITRA BHOWMIK
DOCX
nhận thiết kế clip quảng cáo giá tốt
Biconnected components (13024116056)
Topological sort
Trees
Advance data structure & algorithm
Distributed Graph Algorithms
1535 graph algorithms
Graph algorithm
18 Basic Graph Algorithms
Graph Traversal Algorithm
Skiena algorithm 2007 lecture10 graph data strctures
1.9 b trees eg 03
5.2 divede and conquer 03
문제는 한글이 잘 구현되는가?
1.9 b trees 02
CV Belinda Wahl 2015
4.2 bst 02
2.4 mst prim &kruskal demo
RESUME-ARITRA BHOWMIK
nhận thiết kế clip quảng cáo giá tốt
Ad

Similar to 2.2 topological sort 02 (20)

PPTX
Graph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptx
PPTX
Topological Sorting
PPTX
an EN Mean Value Theorem by Slidesgo.pptx
PPTX
an introduction to Topological Sort.pptx
PPT
Unit24_TopologicalSort.ppt data structures
PPT
Unit24_TopologicalSort (2).ppt
PPTX
Topological sorting
PPT
Data structure
PPTX
DAG and topological sort.pptx
PPTX
Discrete_4.pptx
PPTX
Topoloical sort
PPTX
Topological sort
PPTX
Topological Sort Algorithm.pptx
PPTX
Topological sort
PDF
Unit ii divide and conquer -3
PPTX
topologicalsort-using c++ as development language.pptx
PPT
25-graphs4-topological-sort.ppt data structures
PPTX
Topological Sort and Shortest Path in Directed Acyclic Graph with Single Source
PDF
Topological Sorting (Decrease and Conquer)
PPT
Topological sort
Graph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptxGraph.pptx
Topological Sorting
an EN Mean Value Theorem by Slidesgo.pptx
an introduction to Topological Sort.pptx
Unit24_TopologicalSort.ppt data structures
Unit24_TopologicalSort (2).ppt
Topological sorting
Data structure
DAG and topological sort.pptx
Discrete_4.pptx
Topoloical sort
Topological sort
Topological Sort Algorithm.pptx
Topological sort
Unit ii divide and conquer -3
topologicalsort-using c++ as development language.pptx
25-graphs4-topological-sort.ppt data structures
Topological Sort and Shortest Path in Directed Acyclic Graph with Single Source
Topological Sorting (Decrease and Conquer)
Topological sort
Ad

More from Krish_ver2 (20)

PPT
5.5 back tracking
PPT
5.5 back track
PPT
5.5 back tracking 02
PPT
5.4 randomized datastructures
PPT
5.4 randomized datastructures
PPT
5.4 randamized algorithm
PPT
5.3 dynamic programming 03
PPT
5.3 dynamic programming
PPT
5.3 dyn algo-i
PPT
5.2 divede and conquer 03
PPT
5.2 divide and conquer
PPT
5.1 greedyyy 02
PPT
5.1 greedy
PPT
5.1 greedy 03
PPT
4.4 hashing02
PPT
4.4 hashing
PPT
4.4 hashing ext
PPT
4.4 external hashing
PPT
4.2 bst
PPT
4.2 bst 03
5.5 back tracking
5.5 back track
5.5 back tracking 02
5.4 randomized datastructures
5.4 randomized datastructures
5.4 randamized algorithm
5.3 dynamic programming 03
5.3 dynamic programming
5.3 dyn algo-i
5.2 divede and conquer 03
5.2 divide and conquer
5.1 greedyyy 02
5.1 greedy
5.1 greedy 03
4.4 hashing02
4.4 hashing
4.4 hashing ext
4.4 external hashing
4.2 bst
4.2 bst 03

Recently uploaded (20)

PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
1_English_Language_Set_2.pdf probationary
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
RMMM.pdf make it easy to upload and study
PDF
Hazard Identification & Risk Assessment .pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
IGGE1 Understanding the Self1234567891011
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
Trump Administration's workforce development strategy
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
History, Philosophy and sociology of education (1).pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
1_English_Language_Set_2.pdf probationary
Chinmaya Tiranga quiz Grand Finale.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
RMMM.pdf make it easy to upload and study
Hazard Identification & Risk Assessment .pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
IGGE1 Understanding the Self1234567891011
What if we spent less time fighting change, and more time building what’s rig...
A powerpoint presentation on the Revised K-10 Science Shaping Paper
A systematic review of self-coping strategies used by university students to ...
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Digestion and Absorption of Carbohydrates, Proteina and Fats
Trump Administration's workforce development strategy
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
Empowerment Technology for Senior High School Guide
Supply Chain Operations Speaking Notes -ICLT Program
History, Philosophy and sociology of education (1).pptx

2.2 topological sort 02

  • 2. Definition  A topological sort of a Directed Acylic Graph (DAG) is a linear ordering of all its vertices such that if there is a path from vi to vk then vi appears before vk in the ordering.
  • 3. Algorithm for finding a topological sort Initialize TSArray to null for (i=1; i<=numofvertices; i++)  v = find vertex of degree zero  if v is null  Graph has a cycle. Exit  else  add v to TSArray  remove v and all its outgoing vertices Print TSArray
  • 4. Algorithm for enumerating all topological sorts TopologicalSorts() compute the indegree count array, Indegree[ ] initialize TSArray[ ] to null for each vertex v if indegree[v] == zero push Indegree[ ], TSArray[ ], v while (stack not empty) pop Indegree[ ], TSArray[ ], v add v to TSArray for each x adjacent to v, Indegree[x]-- for each vertex v if indegree[v] == zero push Indegree[ ], TSArray[ ], v
  • 5. Find out the ordering 1 2 4 6 7 53
  • 6. Solution •1 2 5 4 3 7 6 •1 2 5 4 7 3 6 •Find out other possibilities.
  • 8. How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot? TRIANGLES: How many triangles are located in the image below?
  • 9. There are 11 squares total; 5 small, 4 medium, and 2 large. 27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.
  • 10. There are 11 squares total; 5 small, 4 medium, and 2 large. 27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.