0% found this document useful (0 votes)
13 views12 pages

Ts Ca513

Uploaded by

Gladwin Tirkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

Ts Ca513

Uploaded by

Gladwin Tirkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPILER DESIGN

(CA513)

TOPOLOGICAL
ORDERING

Efforts By: Guided By:


Osman Rabbani
MCA/45045/21 Prof. Aruna
Malik
Applications of Graphs: Topological
Sorting

• Topological order
– A list of vertices in a directed graph without cycles such
that vertex x precedes vertex y if there is a directed edge
from x to y in the graph
– There may be several topological orders in a given graph
• Topological sorting
– Arranging the vertices into a topological order

2
Topological Sort
• Directed graph G.
• Rule: if there is an edge u -> v, then u must come
before v.
• Ex: A B
G
F C
I H
A B
E D
G
F C
I H

E D
3
Intuition
• Cycles make topological sort impossible.
• Select any node with no in-edges
– print it
– delete it
– and delete all the edges leaving it
• Repeat
• What if there are some nodes left over?
• Implementation? Efficiency?

4
Implementation
• Start with a list of nodes with in-degree = 0
• Select any edge from list
– mark as deleted
– mark all outgoing edges as deleted
– update in-degree of the destinations of those edges
• If any drops below zero, add to the list
• Running time?

5
Topological Sorting

Figure 13.14
A directed graph without

cycles

Figure 13.15
The graph in Figure 13-14
arranged according to the
topological orders a) a, g, d,
b, e, c, f and b) a, b, g, d, e, f,
c

6
Topological Sorting
• Simple algorithms for finding a topological order
– topSort1
• Find a vertex that has no successor
• Remove from the graph that vertex and all edges that lead to it, and add
the vertex to the beginning of a list of vertices
• Add each subsequent vertex that has no successor to the beginning of the
list
• When the graph is empty, the list of vertices will be in topological order

7
Topological Sorting
• Simple algorithms for finding a topological order
(Continued)
– topSort2
• A modification of the iterative DFS algorithm
• Strategy
– Push all vertices that have no predecessor onto a stack
– Each time you pop a vertex from the stack, add it to the beginning
of a list of vertices
– When the traversal ends, the list of vertices will be in topological
order

8
Implementation
• Start with a list of nodes with in-degree = 0
• Select any edge from list
– mark as deleted
– mark all outgoing edges as deleted
– update in-degree of the destinations of those edges
• If any drops below zero, add to the list
• Running time? In all, algorithm does work:
– O(|V|) to construct initial list
– Every edge marked “deleted” at most once: O(|E|) total
– Every node marked “deleted” at most once: O(|V|) total
– So linear time overall (in |E| and |V|)

9
Why should we care?
• Shortest path problem in directed, acyclic graph
– Called a DAG for short
• General problem:
– Given a DAG input G with weights on edges
– Find shortest paths from source A to every other vertex

10
References
https://en.wikipedia.org/wiki/Topological_sorting
https://www.geeksforgeeks.org/topological-sorting/
https://www.javatpoint.com/topological-sorting
https://www.hackerearth.com/practice/algorithms/grap
hs/topological-sort/tutorial/

TextBook: Aho A.V., Sheth R. I. and Ullman J.D. “Compilers Principles


Techniques and Tools”, Pearson Education
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy