SlideShare a Scribd company logo
1 of 29
GRAPH AND NETWORK THEORY
TOPOLOGICAL SORT OGUZHAN OSMA
• Indtroduction
• Directed Acylic
Graph(DAG)
• Comparison of Algorithms
• Pseudocode of Top. Sort.
• Example of Topological
Sort
• Shortest Path in DAG with
Single Source
• Pseudocode
• Example
• Applications
• References
OUTLINE
WHAT IS TOPOLOGICAL SORT?
Topological sorting for Directed Acyclic Graph
(DAG) is a linear ordering of vertices such that
for every directed edge uv, vertex u comes
before v in the ordering. Topological Sorting
for a graph is not possible if the graph is not a
DAG.[1]
Introduction
In mathematics, particularly graph theory, and computer science, a
directed acyclic graph is a finite directed graph with no directed cycles.
That is, it consists of finitely many vertices and edges with each edge
directed from one vertex to another, such that there is no way to start
at any vertex v and follow a consistently-directed sequence of edges
that eventually loops back to v again. Equivalently, a DAG is a directed
graph that has a topological ordering, a sequence of the vertices such
that every edge is directed from earlier to later in the sequence.[2]
Directed Acylic Graph
Directed Acylic Graph
DFS vs Kahn’s Algorithm vs
Topological Sort
In topological sorting DFS algorithm can be used as Kahn’s Algorithm . In DFS, a vertex is
printed and then DFS is called recursively for its adjacent vertices. In topological sorting, a
vertex is need to be printed before its adjacent vertices For an instance, in the given graph
below, the vertex ‘F’ should be printed before ‘A’, but unlie DFS, the vertex ‘E’ should also be
printed before vertex ‘A’. So Topological sorting is different from DFS. Or an instance, a DFS of
the shown graph is «F C D B A E», but it’s not a topological sorting.
Kahn Algorithm works by choosing vertices in the same order as the eventual topological sort
[3]. First, find a list of "start nodes" which have no incoming edges and insert them into a set S;
at least one such node must exist in an acyclic graph. [4]
Both Kahn’s algorithm’s and DFS’s complexity is O(E+V). Also there is a new algorithm is
available for topological sorting.
Pseudocode
Example
Example
Example
Example
Example
Example
Example
Example
Shortest Path in DAG
with Single Source
In a general graph which is wieghted, single source
shortest distances can be calculated by using Bellman-
Ford Algorithm. If the graph has no negative weight, it
can be calculated more efficient by using Djikstra’s
Algorithm. But in DAG by using Topological Sorting as I
explained with an example before is better. Time
complexity of topological sorting is O(V+E). After finding
topological order, the algorithm process all vertices and
for every vertex, it runs a loop for all adjacent vertices.
Total adjacent vertices in a graph is O(E). So the inner
loop runs O(V+E) times. Therefore, overall time
complexity of this algorithm is O(V+E).[5]
How It Works
At the beginning, distances to all vertices as
infinite and distance to source as zero should
be initialized.
Then topological sorting of graph should be
found. Once topological order is found, we
should process all vertices one by one. For
each vertex processed, distances should be
updated.
Pseudocode
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
IMPORTANT NOTE
Result of sorting may change for every node. For an
instance in the previous example if we choose C first, the
result will be [Inf, 0, 4, 10, 3, 4].
Applications
• While creating complex database tables, some tables
have interdependencies . Topological sort can determine
the order in which we create them.
• Determining what order to take courses and their pre-
requisites in to complete a degree.
• Formulating a lesson plan for a course
• Topological sort algorithm in UNIX rearranges source files to
define all the methods before they are used in the file.
• It is used to detect cycles in a graph.
REFERENCES
[1] : https://www.geeksforgeeks.org/topological-sorting/
[2]: https://en.wikipedia.org/wiki/Directed_acyclic_graph
[3]: A. B. Kahn, “Topological sorting of large
networks,”Communications of the ACM, vol. 5, no. 11,
pp. 558–562, 1962, doi: 10.1145/368996.369025.
[4]: http://www.ijmlc.org/papers/411-LC039.pdf
[5]: https://www.geeksforgeeks.org/shortest-path-for-
directed-acyclic-graphs/?ref=rp
THANK
YOU

More Related Content

What's hot

Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
Vasavi College of Engg
 

What's hot (20)

Dwh lecture 08-denormalization tech
Dwh   lecture 08-denormalization techDwh   lecture 08-denormalization tech
Dwh lecture 08-denormalization tech
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Computer Architecture – An Introduction
Computer Architecture – An IntroductionComputer Architecture – An Introduction
Computer Architecture – An Introduction
 
Pipelining slides
Pipelining slides Pipelining slides
Pipelining slides
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
Address Binding Scheme
Address Binding SchemeAddress Binding Scheme
Address Binding Scheme
 
Segmentation in operating systems
Segmentation in operating systemsSegmentation in operating systems
Segmentation in operating systems
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Computer Organisation & Architecture (chapter 1)
Computer Organisation & Architecture (chapter 1) Computer Organisation & Architecture (chapter 1)
Computer Organisation & Architecture (chapter 1)
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Linked List
Linked ListLinked List
Linked List
 
Productizing Structured Streaming Jobs
Productizing Structured Streaming JobsProductizing Structured Streaming Jobs
Productizing Structured Streaming Jobs
 
Feature Hashing for Scalable Machine Learning with Nick Pentreath
Feature Hashing for Scalable Machine Learning with Nick PentreathFeature Hashing for Scalable Machine Learning with Nick Pentreath
Feature Hashing for Scalable Machine Learning with Nick Pentreath
 
data Structure
data Structuredata Structure
data Structure
 
Photon Technical Deep Dive: How to Think Vectorized
Photon Technical Deep Dive: How to Think VectorizedPhoton Technical Deep Dive: How to Think Vectorized
Photon Technical Deep Dive: How to Think Vectorized
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Unit 4 external sorting
Unit 4   external sortingUnit 4   external sorting
Unit 4 external sorting
 

Similar to Topological Sort and Shortest Path in Directed Acyclic Graph with Single Source

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
 
IntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docxIntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docx
mariuse18nolet
 

Similar to Topological Sort and Shortest Path in Directed Acyclic Graph with Single Source (20)

Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptx
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Graph 1
Graph 1Graph 1
Graph 1
 
Topological sort
Topological sortTopological sort
Topological sort
 
Topoloical sort
Topoloical sortTopoloical sort
Topoloical sort
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
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
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptx
 
Vanmathy no sql
Vanmathy no sql Vanmathy no sql
Vanmathy no sql
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Graphs
GraphsGraphs
Graphs
 
IntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docxIntroductionTopological sorting is a common operation performed .docx
IntroductionTopological sorting is a common operation performed .docx
 
Data structure
Data structureData structure
Data structure
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 

Recently uploaded

CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
Cherry
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Sérgio Sacani
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
Cherry
 
PODOCARPUS...........................pptx
PODOCARPUS...........................pptxPODOCARPUS...........................pptx
PODOCARPUS...........................pptx
Cherry
 
Pteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecyclePteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecycle
Cherry
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptx
seri bangash
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for vote
RaunakRastogi4
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
Cherry
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
MohamedFarag457087
 

Recently uploaded (20)

FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
PODOCARPUS...........................pptx
PODOCARPUS...........................pptxPODOCARPUS...........................pptx
PODOCARPUS...........................pptx
 
Pteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecyclePteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecycle
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptx
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for vote
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
 
Cot curve, melting temperature, unique and repetitive DNA
Cot curve, melting temperature, unique and repetitive DNACot curve, melting temperature, unique and repetitive DNA
Cot curve, melting temperature, unique and repetitive DNA
 
BHUBANESHWAR ODIA CALL GIRL SEIRVEC ❣️ 72051//37929❣️ CALL GIRL IN ODIA HAND ...
BHUBANESHWAR ODIA CALL GIRL SEIRVEC ❣️ 72051//37929❣️ CALL GIRL IN ODIA HAND ...BHUBANESHWAR ODIA CALL GIRL SEIRVEC ❣️ 72051//37929❣️ CALL GIRL IN ODIA HAND ...
BHUBANESHWAR ODIA CALL GIRL SEIRVEC ❣️ 72051//37929❣️ CALL GIRL IN ODIA HAND ...
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
 
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center ChimneyX-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
 
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLGwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsTransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptx
 

Topological Sort and Shortest Path in Directed Acyclic Graph with Single Source

  • 1. GRAPH AND NETWORK THEORY TOPOLOGICAL SORT OGUZHAN OSMA
  • 2. • Indtroduction • Directed Acylic Graph(DAG) • Comparison of Algorithms • Pseudocode of Top. Sort. • Example of Topological Sort • Shortest Path in DAG with Single Source • Pseudocode • Example • Applications • References OUTLINE
  • 3. WHAT IS TOPOLOGICAL SORT? Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.[1] Introduction
  • 4. In mathematics, particularly graph theory, and computer science, a directed acyclic graph is a finite directed graph with no directed cycles. That is, it consists of finitely many vertices and edges with each edge directed from one vertex to another, such that there is no way to start at any vertex v and follow a consistently-directed sequence of edges that eventually loops back to v again. Equivalently, a DAG is a directed graph that has a topological ordering, a sequence of the vertices such that every edge is directed from earlier to later in the sequence.[2] Directed Acylic Graph
  • 6. DFS vs Kahn’s Algorithm vs Topological Sort In topological sorting DFS algorithm can be used as Kahn’s Algorithm . In DFS, a vertex is printed and then DFS is called recursively for its adjacent vertices. In topological sorting, a vertex is need to be printed before its adjacent vertices For an instance, in the given graph below, the vertex ‘F’ should be printed before ‘A’, but unlie DFS, the vertex ‘E’ should also be printed before vertex ‘A’. So Topological sorting is different from DFS. Or an instance, a DFS of the shown graph is «F C D B A E», but it’s not a topological sorting. Kahn Algorithm works by choosing vertices in the same order as the eventual topological sort [3]. First, find a list of "start nodes" which have no incoming edges and insert them into a set S; at least one such node must exist in an acyclic graph. [4] Both Kahn’s algorithm’s and DFS’s complexity is O(E+V). Also there is a new algorithm is available for topological sorting.
  • 16. Shortest Path in DAG with Single Source In a general graph which is wieghted, single source shortest distances can be calculated by using Bellman- Ford Algorithm. If the graph has no negative weight, it can be calculated more efficient by using Djikstra’s Algorithm. But in DAG by using Topological Sorting as I explained with an example before is better. Time complexity of topological sorting is O(V+E). After finding topological order, the algorithm process all vertices and for every vertex, it runs a loop for all adjacent vertices. Total adjacent vertices in a graph is O(E). So the inner loop runs O(V+E) times. Therefore, overall time complexity of this algorithm is O(V+E).[5]
  • 17. How It Works At the beginning, distances to all vertices as infinite and distance to source as zero should be initialized. Then topological sorting of graph should be found. Once topological order is found, we should process all vertices one by one. For each vertex processed, distances should be updated.
  • 26. IMPORTANT NOTE Result of sorting may change for every node. For an instance in the previous example if we choose C first, the result will be [Inf, 0, 4, 10, 3, 4].
  • 27. Applications • While creating complex database tables, some tables have interdependencies . Topological sort can determine the order in which we create them. • Determining what order to take courses and their pre- requisites in to complete a degree. • Formulating a lesson plan for a course • Topological sort algorithm in UNIX rearranges source files to define all the methods before they are used in the file. • It is used to detect cycles in a graph.
  • 28. REFERENCES [1] : https://www.geeksforgeeks.org/topological-sorting/ [2]: https://en.wikipedia.org/wiki/Directed_acyclic_graph [3]: A. B. Kahn, “Topological sorting of large networks,”Communications of the ACM, vol. 5, no. 11, pp. 558–562, 1962, doi: 10.1145/368996.369025. [4]: http://www.ijmlc.org/papers/411-LC039.pdf [5]: https://www.geeksforgeeks.org/shortest-path-for- directed-acyclic-graphs/?ref=rp