This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: DAGs and Topological Ordering
A directed acyclic graph (DAG) is a
digraph that has no directed cycles
A topological ordering of a digraph
is a numbering D v1 , …, vn B of the vertices such that for every
edge (vi , vj), we have i < j C
DAG G A Example: in a task scheduling
digraph, a topological ordering is a
task sequence that satisfies the
precedence constraints v4
D v2 Theorem B A digraph admits a topological
ordering if and only if it is a DAG A
 91  v5
E v3
C v1 CSE 2011
Prof. J. Elder E Topological
ordering of G Last Updated: 4/1/10 10:16 AM Topological (Linear) Order
underwear socks pants shoes underwear
pants
socks
shoes socks CSE 2011
Prof. J. Elder underwear
pants
shoes
 92  Last Updated: 4/1/10 10:16 AM Topological (Linear) Order
underwear socks pants shoes Invalid
Order socks
shoes
pants
underwear
CSE 2011
Prof. J. Elder  93  Last Updated: 4/1/10 10:16 AM Algorithm for Topological Sorting
Note: This algorithm is different than the one
in GoodrichTamassia
Method TopologicalSort(G)
H
G // Temporary copy of G
n
G.numVertices()
while H is not empty do
Let v be a vertex with no outgoing edges
Label v
n
n
n1
Remove v from H //as well as edges involving v
CSE 2011
Prof. J. Elder  94  Last Updated: 4/1/10 10:16 AM Linear Order
a
h
i
j
k b
c
d
e
g l f PreCondition:
A Directed Acyclic Graph
(DAG)
PostCondition:
Find one valid linear order
Algorithm:
•Find a terminal node (sink).
•Put it last in sequence.
•Delete from graph & repeat
V i =1 CSE 2011
Prof. J. Elder ….. l
 95  () i =O V Running time: O(V) 2 Can we do better?
Last Updated: 4/1/10 10:16 AM ...
View
Full
Document
This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.
 Fall '11
 Elder
 Data Structures

Click to edit the document details