Directed Acyclic Graphs & Topological Sort
CS16: Introduction to Data Structures & Algorithms Spring 2020
Directed Acyclic Graphs & Topological Sort CS16: Introduction - - PowerPoint PPT Presentation
Directed Acyclic Graphs & Topological Sort CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Directed Acyclic Graphs Topological Sort Hand-simulation Pseudo-code Runtime analysis 2 Directed
CS16: Introduction to Data Structures & Algorithms Spring 2020
2
acyclic
there is no path that leads back to the same vertex Directed Undirected
A B A B
Acyclic Cyclic Cyclic
A B D C A B D C A B
trees!
A B D C
Tree
trees!
A B D C
DAG
trees!
A B D C
DAG
trees!
A B D C
DAG
trees!
A B D C
NOT a DAG
completing certain things depend on completing other things
vertex
9
10
22 141 33 16 123 224 15
Source Sink
requisites?
11
before it in the ordering
topological ordering given a DAG
12
22 141 16 15
13
14
22 141 33 16 123 224 15
Stack List:
22 141 33 16 123 224 15
Stack List:
15 22
Populate Stack with source vertices
15
16
22 141 33 16 123 224 15
Stack List:
15 22
Pop from stack and add to list
17
22 141 33 16 123 224 15
Stack List:
15 22
Remove outgoing edges & check corresponding vertices
18
22 141 33 123 224 15
Stack List:
15 22
16 has no more incoming edges so push it on the stack
16 16
19
22 141 33 123 224 15
Stack List:
15 22
Pop from the stack and add to list
16 16
20
22 141 33 123 224 15
Stack List:
15 22
Remove outgoing edges & check the corresponding vertices
16 16
21
22 141 33 123 224 15
Stack List:
15 22
33 has no more incoming edges so push it onto the stack 141 still has an incoming edge
16 16 33
# of incoming edges = 1
22
22 141 33 123 224 15
Stack List:
15 22
Pop from the stack & repeat!
16 16 33
23
22 141 33 123 224 15
Stack List:
15 22 16 16 33
24
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123
25
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123
26
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123 224
27
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123 224
28
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123 224
29
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123 224 141
30
22 141 33 123 224 15
Stack List:
15 22 16 16 33 123 224 141
We're done!
31
function top_sort(graph g): // Input: A DAG g // Output: A list of vertices of g, in topological order s = Stack() l = List() for each vertex in g: if vertex is source: s.push(vertex) while s is not empty: v = s.pop() l.append(v) for each outgoing edge e from v: w = e.destination delete e if w is a source: s.push(w) return l
a stack
vertex:
function top_sort(graph g): // Input: A DAG g // Output: A list of vertices of g, in topological order s = Stack() l = List() for each vertex in g: if vertex is source: s.push(vertex) while s is not empty: v = s.pop() l.append(v) for each outgoing edge e from v: w = e.destination delete e if w is a source: s.push(w) return l
Looping through every vertex to find sources is O(|V|)
a stack
vertex:
function top_sort(graph g): // Input: A DAG g // Output: A list of vertices of g, in topological order s = Stack() l = List() for each vertex in g: if vertex is source: s.push(vertex) while s is not empty: v = s.pop() l.append(v) for each outgoing edge e from v: w = e.destination delete e if w is a source: s.push(w) return l
Looping through every vertex to find sources is O(|V|) Stack will hold each vertex once At each iteration we only visit outgoing edges from popped vertex. So every edge visited once. Total runtime: O(|V|+|E|)
34
35
36
Job
Experience