SlideShare a Scribd company logo
Topological Sort
Strongly Connected Components
 For a directed acyclic graph G=(V,E)
 A topological sort is an ordering of all of G’svertices
 v1, v2, v3,….,vn such that …
 Vertex u comes before vertex v if edge (u,v) belongs to G
 So,formally for every egde (vi,vk) in E ,i<k
WHAT IS TOPOLOGICAL SORT ?
TOPOLOGICAL SORT
• Definition:
A topological sort or topological ordering of a directed ayclic
graph is a linear ordering of its vertices such that for every
directed edge (u,v) from vertex u to v, u comes before v in the
ordering.
0 1
5
2
4
3
• There can be more then one
topological sorting for a graph.
• A topological sorting of the given
graph can be
• “5 4 2 3 1 0”
• “4 5 2 3 1 0”
• “5 2 3 4 1 0”
• “5 2 3 4 0 1”
• The first vertex in topological
sorting is always a vertex with
in-degree as “0”
• ( a vertex with no incoming edges).
Topological sorting Application
• The vertices of the graph may represent tasks to be performed, and the
edges may represent constraints that one task must be performed before
another;
• In this application, a topological ordering is just a valid sequence for the
tasks.
• A topological ordering is possible if and only if the graph has no
directed cycles, that is, if it is a directed acyclic graph (DAG).
Topological sorting Application
The graph shown to the left has many valid
topological sorts, including:
• 5, 7, 3, 11, 8, 2, 9, 10 (visual left-to-right,top-to-bottom)
• 3, 5, 7, 8, 11, 2, 9, 10 (smallest-numbered available vertexfirst)
• 5, 7, 3, 8, 11, 10, 9, 2 (fewest edges first)
• 7, 5, 11, 3, 10, 8, 9, 2 (largest-numbered available vertexfirst)
• 5, 7, 11, 2, 3, 8, 9, 10 (attempting top-to-bottom,left-to-right)
• 3, 7, 8, 5, 11, 10, 2, 9 (arbitrary)
Algorithms
Kahn's Algorithm
One of these algorithms, first described by Kahn (1962) works by choosing vertices
in the same order as the eventual topological sort.
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edge
while S is non-empty do
remove a node n from S
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
If the graph is a DAG (Directed Acyclic Graph) a solution will be contained in the list L
Example
1 2
4
3 5
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 1
Output :
1
Contd…
2
4
3 5
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 2
Output :
1 2
Contd…
4
3 5
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 5
Output :
1 2 5
Contd…
4
3
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 4
Output :
1 2 5 4
Contd…
3
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 3
Output :
1 2 5 4 3
Contd…
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 6, 7
Output :
1 2 5 4 3 6 7
1 2
4
3 5
6 7
Topological sort using DFS
Topological_Sort(G)
1. Call DFS(G) to compute finishing time v.f for each vertex v.
2. As each vertex is finished, insert it into the front of a linked list.
3. Return the linked list of vertices.
Time complexity : 𝛳(V+E) as DFS takes 𝛳(V+E) time.
It takes O(1) time to insert each of the |V| vertices onto the front of the
linked list.
Example
e a b d f g c h i
a e
d
b
c g
h
i
f
11/16
12/15
6/7
1/8
2/5
3/4
17/18
13/14
9/10
DFS Traversal
3/4
2/5
6/7
1/8 9/10
13/14
12/15
11/16
17/18
Strongly Connected
Components
A strongly connected component of a directed graph
G=(V,E) is a maximal set of vertices such that
for every pair of vertices u and v in C , we have both
u~v and v~u
C V
STRONGLY-CONNECTED-COMPONENTS(G)
1. call DFS(G) to compute finishing times f[u] for each vertex u
2. compute GT
GT = (V, ET), where ET = {(u,v): (v,u)ϵ E}. That is, ET consists of
the edges of G with their directions reversed.
3. call DFS(GT), but in the main loop of DFS, consider the vertices
in order of decreasing f[u] (as computed in line 1)
4. output the vertices of each tree in the depth-first forest formed in
line 3 as a separate strongly connected component
(a) A directed graph G. Each shaded
region is a strongly connected
component of G. Each vertex is
labeled with its discovery and
finishing times in a depth-first search,
and tree edges are shaded.
(b) The graph GT, the transpose of G,
with the depth-first forest computed
in line 3 of STRONGLY-
CONNECTED-COMPONENTS
shown and tree edges shaded. Each
strongly connected component
corresponds to one depth-first tree.
Vertices b, c, g, and h, which are
heavily shaded, are the roots of the
depth-first trees produced by the
depth-first search of GT.
(c) The acyclic component graph GSCC
obtained by contracting all edges
within each strongly connected
component of G so that only a single
vertex remains in each component.
Strongly Connected Components
The transpose of a graph G, GT=(V,ET) is used for finding the strongly
connected components of a graph G=(V,E)
It is interesting to observe that G and GT have exactly the same strongly
connected components
• The complexity of algorithm is 𝛳(V+E).
Strongly Connected Components

More Related Content

Similar to topologicalsort-using c++ as development language.pptx

Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
Ppt 1
Ppt 1Ppt 1
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
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
 
Graphs
GraphsGraphs
Graphs
Dwight Sabio
 
Graphs
GraphsGraphs
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
Venkat Sai Sharath Mudhigonda
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
Hector Zenil
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
Samet öztoprak
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questionsSamet öztoprak
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
Graph representation
Graph representationGraph representation
Graph representation
Amit Kumar Rathi
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
ssuser1989da
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
Edhole.com
 
Graphs
GraphsGraphs
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 

Similar to topologicalsort-using c++ as development language.pptx (20)

Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
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
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
Optimisation random graph presentation
Optimisation random graph presentationOptimisation random graph presentation
Optimisation random graph presentation
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
 
Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graph representation
Graph representationGraph representation
Graph representation
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Graphs
GraphsGraphs
Graphs
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 

Recently uploaded

欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
foismail170
 
0524.priorspeakingengagementslist-01.pdf
0524.priorspeakingengagementslist-01.pdf0524.priorspeakingengagementslist-01.pdf
0524.priorspeakingengagementslist-01.pdf
Thomas GIRARD BDes
 
Data_structures_with_c_by_schaum_series (1).pdf
Data_structures_with_c_by_schaum_series (1).pdfData_structures_with_c_by_schaum_series (1).pdf
Data_structures_with_c_by_schaum_series (1).pdf
Anita Pal
 
The Impact of Artificial Intelligence on Modern Society.pdf
The Impact of Artificial Intelligence on Modern Society.pdfThe Impact of Artificial Intelligence on Modern Society.pdf
The Impact of Artificial Intelligence on Modern Society.pdf
ssuser3e63fc
 
How to Master LinkedIn for Career and Business
How to Master LinkedIn for Career and BusinessHow to Master LinkedIn for Career and Business
How to Master LinkedIn for Career and Business
ideatoipo
 
133. Reviewer Certificate in Advances in Research
133. Reviewer Certificate in Advances in Research133. Reviewer Certificate in Advances in Research
133. Reviewer Certificate in Advances in Research
Manu Mitra
 
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid feverWidal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
taexnic
 
DIGITAL MARKETING COURSE IN CHENNAI.pptx
DIGITAL MARKETING COURSE IN CHENNAI.pptxDIGITAL MARKETING COURSE IN CHENNAI.pptx
DIGITAL MARKETING COURSE IN CHENNAI.pptx
FarzanaRbcomcs
 
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
larisashrestha558
 
Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!
LukeRoyak
 
0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf
0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf
0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf
Thomas GIRARD BDes
 
D.El.Ed. College List -Session 2024-26.pdf
D.El.Ed. College List -Session 2024-26.pdfD.El.Ed. College List -Session 2024-26.pdf
D.El.Ed. College List -Session 2024-26.pdf
bipedoy339
 
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
Chapters 3  Contracts.pptx Chapters 3  Contracts.pptxChapters 3  Contracts.pptx Chapters 3  Contracts.pptx
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
Sheldon Byron
 
GO PRO SHOWCASE 24 - ENGLISH.pdf download
GO PRO SHOWCASE 24 - ENGLISH.pdf downloadGO PRO SHOWCASE 24 - ENGLISH.pdf download
GO PRO SHOWCASE 24 - ENGLISH.pdf download
daiyaanreddy772
 
Midterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptxMidterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptx
Sheldon Byron
 
Operating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdfOperating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdf
harikrishnahari6276
 
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
foismail170
 
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
foismail170
 
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdfDr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam
 
My Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya WestrinMy Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya Westrin
AlinaseFaith
 

Recently uploaded (20)

欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
欧洲杯买球平台-欧洲杯买球平台推荐-欧洲杯买球平台| 立即访问【ac123.net】
 
0524.priorspeakingengagementslist-01.pdf
0524.priorspeakingengagementslist-01.pdf0524.priorspeakingengagementslist-01.pdf
0524.priorspeakingengagementslist-01.pdf
 
Data_structures_with_c_by_schaum_series (1).pdf
Data_structures_with_c_by_schaum_series (1).pdfData_structures_with_c_by_schaum_series (1).pdf
Data_structures_with_c_by_schaum_series (1).pdf
 
The Impact of Artificial Intelligence on Modern Society.pdf
The Impact of Artificial Intelligence on Modern Society.pdfThe Impact of Artificial Intelligence on Modern Society.pdf
The Impact of Artificial Intelligence on Modern Society.pdf
 
How to Master LinkedIn for Career and Business
How to Master LinkedIn for Career and BusinessHow to Master LinkedIn for Career and Business
How to Master LinkedIn for Career and Business
 
133. Reviewer Certificate in Advances in Research
133. Reviewer Certificate in Advances in Research133. Reviewer Certificate in Advances in Research
133. Reviewer Certificate in Advances in Research
 
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid feverWidal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
 
DIGITAL MARKETING COURSE IN CHENNAI.pptx
DIGITAL MARKETING COURSE IN CHENNAI.pptxDIGITAL MARKETING COURSE IN CHENNAI.pptx
DIGITAL MARKETING COURSE IN CHENNAI.pptx
 
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
皇冠体育- 皇冠体育官方网站- CROWN SPORTS| 立即访问【ac123.net】
 
Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!
 
0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf
0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf
0524.THOMASGIRARD_SINGLEPAGERESUME-01.pdf
 
D.El.Ed. College List -Session 2024-26.pdf
D.El.Ed. College List -Session 2024-26.pdfD.El.Ed. College List -Session 2024-26.pdf
D.El.Ed. College List -Session 2024-26.pdf
 
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
Chapters 3  Contracts.pptx Chapters 3  Contracts.pptxChapters 3  Contracts.pptx Chapters 3  Contracts.pptx
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
 
GO PRO SHOWCASE 24 - ENGLISH.pdf download
GO PRO SHOWCASE 24 - ENGLISH.pdf downloadGO PRO SHOWCASE 24 - ENGLISH.pdf download
GO PRO SHOWCASE 24 - ENGLISH.pdf download
 
Midterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptxMidterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptx
 
Operating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdfOperating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdf
 
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
 
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
 
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdfDr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
 
My Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya WestrinMy Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya Westrin
 

topologicalsort-using c++ as development language.pptx

  • 2.  For a directed acyclic graph G=(V,E)  A topological sort is an ordering of all of G’svertices  v1, v2, v3,….,vn such that …  Vertex u comes before vertex v if edge (u,v) belongs to G  So,formally for every egde (vi,vk) in E ,i<k WHAT IS TOPOLOGICAL SORT ?
  • 3. TOPOLOGICAL SORT • Definition: A topological sort or topological ordering of a directed ayclic graph is a linear ordering of its vertices such that for every directed edge (u,v) from vertex u to v, u comes before v in the ordering.
  • 4. 0 1 5 2 4 3 • There can be more then one topological sorting for a graph. • A topological sorting of the given graph can be • “5 4 2 3 1 0” • “4 5 2 3 1 0” • “5 2 3 4 1 0” • “5 2 3 4 0 1” • The first vertex in topological sorting is always a vertex with in-degree as “0” • ( a vertex with no incoming edges).
  • 5. Topological sorting Application • The vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; • In this application, a topological ordering is just a valid sequence for the tasks. • A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).
  • 7. The graph shown to the left has many valid topological sorts, including: • 5, 7, 3, 11, 8, 2, 9, 10 (visual left-to-right,top-to-bottom) • 3, 5, 7, 8, 11, 2, 9, 10 (smallest-numbered available vertexfirst) • 5, 7, 3, 8, 11, 10, 9, 2 (fewest edges first) • 7, 5, 11, 3, 10, 8, 9, 2 (largest-numbered available vertexfirst) • 5, 7, 11, 2, 3, 8, 9, 10 (attempting top-to-bottom,left-to-right) • 3, 7, 8, 5, 11, 10, 2, 9 (arbitrary)
  • 8. Algorithms Kahn's Algorithm One of these algorithms, first described by Kahn (1962) works by choosing vertices in the same order as the eventual topological sort. L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edge while S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order) If the graph is a DAG (Directed Acyclic Graph) a solution will be contained in the list L
  • 9. Example 1 2 4 3 5 6 7 Identify nodes having in degree ‘0’ Select a node and delete it with its edges then add node to output Select Node : 1 Output : 1
  • 10. Contd… 2 4 3 5 6 7 Identify nodes having in degree ‘0’ Select a node and delete it with its edges then add node to output Select Node : 2 Output : 1 2
  • 11. Contd… 4 3 5 6 7 Identify nodes having in degree ‘0’ Select a node and delete it with its edges then add node to output Select Node : 5 Output : 1 2 5
  • 12. Contd… 4 3 6 7 Identify nodes having in degree ‘0’ Select a node and delete it with its edges then add node to output Select Node : 4 Output : 1 2 5 4
  • 13. Contd… 3 6 7 Identify nodes having in degree ‘0’ Select a node and delete it with its edges then add node to output Select Node : 3 Output : 1 2 5 4 3
  • 14. Contd… 6 7 Identify nodes having in degree ‘0’ Select a node and delete it with its edges then add node to output Select Node : 6, 7 Output : 1 2 5 4 3 6 7 1 2 4 3 5 6 7
  • 15. Topological sort using DFS Topological_Sort(G) 1. Call DFS(G) to compute finishing time v.f for each vertex v. 2. As each vertex is finished, insert it into the front of a linked list. 3. Return the linked list of vertices. Time complexity : 𝛳(V+E) as DFS takes 𝛳(V+E) time. It takes O(1) time to insert each of the |V| vertices onto the front of the linked list.
  • 16. Example e a b d f g c h i a e d b c g h i f 11/16 12/15 6/7 1/8 2/5 3/4 17/18 13/14 9/10 DFS Traversal 3/4 2/5 6/7 1/8 9/10 13/14 12/15 11/16 17/18
  • 17. Strongly Connected Components A strongly connected component of a directed graph G=(V,E) is a maximal set of vertices such that for every pair of vertices u and v in C , we have both u~v and v~u C V
  • 18. STRONGLY-CONNECTED-COMPONENTS(G) 1. call DFS(G) to compute finishing times f[u] for each vertex u 2. compute GT GT = (V, ET), where ET = {(u,v): (v,u)ϵ E}. That is, ET consists of the edges of G with their directions reversed. 3. call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u] (as computed in line 1) 4. output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component
  • 19. (a) A directed graph G. Each shaded region is a strongly connected component of G. Each vertex is labeled with its discovery and finishing times in a depth-first search, and tree edges are shaded. (b) The graph GT, the transpose of G, with the depth-first forest computed in line 3 of STRONGLY- CONNECTED-COMPONENTS shown and tree edges shaded. Each strongly connected component corresponds to one depth-first tree. Vertices b, c, g, and h, which are heavily shaded, are the roots of the depth-first trees produced by the depth-first search of GT. (c) The acyclic component graph GSCC obtained by contracting all edges within each strongly connected component of G so that only a single vertex remains in each component.
  • 20. Strongly Connected Components The transpose of a graph G, GT=(V,ET) is used for finding the strongly connected components of a graph G=(V,E) It is interesting to observe that G and GT have exactly the same strongly connected components
  • 21. • The complexity of algorithm is 𝛳(V+E). Strongly Connected Components