SlideShare a Scribd company logo
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

PPT
Arrays searching-sorting
PPT
Breadth first search
PDF
Bellman ford
DOCX
ω La regla del pivote
PPTX
PRIM'S ALGORITHM
PDF
Linked list
Arrays searching-sorting
Breadth first search
Bellman ford
ω La regla del pivote
PRIM'S ALGORITHM
Linked list

What's hot (20)

DOCX
Trees and Graphs in data structures and Algorithms
PDF
Árboles Binarios
PPTX
Kruskal’s Algorithm
PPTX
Minimum Spanning Tree
PDF
Shortest Path in Graph
PDF
Chapitre 1 rappel
PPTX
PROLOG: Database Manipulation In Prolog
PPT
Breadth first search and depth first search
PPTX
Bellman ford algorithm
PPT
Basics of data structure
PPTX
Graph Traversal Algorithm
PPTX
DFS and BFS
PPTX
JAVA PROGRAM CONSTRUCTS OR LANGUAGE BASICS.pptx
PDF
Chapitre iii récursivité et paradigme diviser pour régner
PPTX
Recursividad (Divide y Vencerás)
PDF
Programación 1: cadenas en C
PPT
Algorithm analysis
PPTX
DAA-Floyd Warshall Algorithm.pptx
PDF
Les enregistrements
Trees and Graphs in data structures and Algorithms
Árboles Binarios
Kruskal’s Algorithm
Minimum Spanning Tree
Shortest Path in Graph
Chapitre 1 rappel
PROLOG: Database Manipulation In Prolog
Breadth first search and depth first search
Bellman ford algorithm
Basics of data structure
Graph Traversal Algorithm
DFS and BFS
JAVA PROGRAM CONSTRUCTS OR LANGUAGE BASICS.pptx
Chapitre iii récursivité et paradigme diviser pour régner
Recursividad (Divide y Vencerás)
Programación 1: cadenas en C
Algorithm analysis
DAA-Floyd Warshall Algorithm.pptx
Les enregistrements
Ad

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

PPTX
an EN Mean Value Theorem by Slidesgo.pptx
PPTX
an introduction to Topological Sort.pptx
PPTX
Topological Sort Algorithm.pptx
PPTX
UNIT III.pptx
PPTX
Topological sort
PPTX
Topoloical sort
PDF
Graph Data Structure
DOCX
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
PPSX
Unit-6 Graph.ppsx ppt
PPTX
Depth first traversal(data structure algorithms)
PPTX
Data Structure of computer science and technology
PPTX
VANU no sql ppt.pptx
PPTX
Vanmathy no sql
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
PDF
LEC 12-DSALGO-GRAPHS(final12).pdf
PDF
Algorithms of graph
PPT
Graphs in Data Structure
PPTX
Unit 9 graph
an EN Mean Value Theorem by Slidesgo.pptx
an introduction to Topological Sort.pptx
Topological Sort Algorithm.pptx
UNIT III.pptx
Topological sort
Topoloical sort
Graph Data Structure
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
Unit-6 Graph.ppsx ppt
Depth first traversal(data structure algorithms)
Data Structure of computer science and technology
VANU no sql ppt.pptx
Vanmathy no sql
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
LEC 12-DSALGO-GRAPHS(final12).pdf
Algorithms of graph
Graphs in Data Structure
Unit 9 graph
Ad

Recently uploaded (20)

PDF
bbec55_b34400a7914c42429908233dbd381773.pdf
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
PDF
Phytochemical Investigation of Miliusa longipes.pdf
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
microscope-Lecturecjchchchchcuvuvhc.pptx
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PPTX
BIOMOLECULES PPT........................
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PDF
Placing the Near-Earth Object Impact Probability in Context
PPTX
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
famous lake in india and its disturibution and importance
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
bbec55_b34400a7914c42429908233dbd381773.pdf
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
Phytochemical Investigation of Miliusa longipes.pdf
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
microscope-Lecturecjchchchchcuvuvhc.pptx
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
BIOMOLECULES PPT........................
Introduction to Fisheries Biotechnology_Lesson 1.pptx
Placing the Near-Earth Object Impact Probability in Context
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
ECG_Course_Presentation د.محمد صقران ppt
famous lake in india and its disturibution and importance
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
The KM-GBF monitoring framework – status & key messages.pptx
POSITIONING IN OPERATION THEATRE ROOM.ppt
Classification Systems_TAXONOMY_SCIENCE8.pptx
7. General Toxicologyfor clinical phrmacy.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