lecture-9 - university of illinois at chicago › ~xiaorui › cs401 › slides ›...
TRANSCRIPT
CS 401
Depth First Search / Topological Order
Xiaorui Sun
1
Last lecture: DFS
Initialization: mark all vertices undiscovered
DFS(!) Mark ! discovered
for each edge {!, $}if ($ is undiscovered)
Mark $ discoveredDFS($)
Mark ! fully-explored
2
3
DFS(A)A,1
B J
I
H
C
G
FD
E
K L
M
Suppose edge listsat each vertex are sorted alphabetically
Color code:undiscovereddiscoveredfully-explored
Call Stack(Edge list):
A (B,J)
st[] = {1}
4
DFS(A)A,1
B,2 J,10
I,9
H,8
C,3
G,7
F,6D,4
E,5
K,11 L,12
M,13
Color code:undiscovereddiscoveredfully-explored
Call Stack:(Edge list)
TA-DA!!
st[] = {}
5
DFS(A)A,1
B,2 J,10
I,9
H,8
C,3
G,7
F,6D,4
E,5
K,11 L,12
M,13
Edge code:Tree edgeBack edge
6
DFS(A) A,1
B,2
J,10
I,9
H,8
C,3
G,7
F,6
D,4
E,5
K,11L,12
M,13
Edge code:Tree edgeBack edgeNo Cross Edges!
Non-Tree Edges in DFSLemma: For every edge {", $}, if {", $} is not in DFS tree, then one of " or $ is an ancestor of the other in the tree.
Proof:Suppose that " is visited first.Therefore DFS(") was called before DFS($)
Since {", $} is not in DFS tree, $ was visited when the edge {", $}was examined during DFS(")
Therefore $ was visited during the call to DFS(") so $ is a descendant of ".
7
Non-Tree Edges in DFSBFS tree ≠ DFS tree, but, as with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple" in some way.
All non-tree edges join a vertex and one of its descendents/ancestors in the DFS tree
8
Properties of (undirected) DFSLike BFS(!):• DFS(!) visits " iff there is a path in G from ! to "
So, we can use DFS to find connected components• Edges into then-undiscovered vertices define a tree –
the "depth first spanning tree" of G
Unlike the BFS tree: • The DF spanning tree isn't minimum depth• Its levels don't reflect min distance from the root• Non-tree edges never join vertices on the same or
adjacent levels
9
Directed Graph and Topological Ordering
Directed Graphs
11
1
210
9
8
3
4
56
7
1112
13Multi edge
self loop
Node = intersection, edge = one-way street
Directed Graphs
12
Precedence ConstraintsIn a directed graph, an edge (", $) means task " must occur before task $.
Applications• Course prerequisite:
course " must be taken before $• Compilation:
must compile module " before $• Computing overflow:
output of job " is part of input to job $• Manufacturing or assembly:
sand it before paint it13
Directed Acyclic Graphs (DAG)Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles.
Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as !", !$, … , !& so that for every edge (!(, !)) we have + < -.
14a DAG
2 3
6 5 4
7 1a topological ordering of that DAG–all edges left-to-right
1 2 3 4 5 6 7
DAGs: A Sufficient ConditionLemma: If ! has a topological order, then ! is a DAG.
Proof. (by contradiction)
Suppose that ! has a topological order 1,2, … , & and that ! also has a directed cycle '.
Let ( be the lowest-indexed node in ', and let ) be the node just before (; thus (), () is an (directed) edge.
By our choice of (, we have ( < ).On the other hand, since (), () is an edge and 1,… , & is a topological order, we must have ) < (, a contradiction
15
1 i j n
the directed cycle C
the supposed topological order: 1,2,…,n
DAGs: A Sufficient Condition
16
G has a topological order G is a DAG?
Every DAG has a source nodeLemma: If ! is a DAG, then ! has a node with no incoming edges (i.e., a source).
Proof. (by contradiction)Suppose that ! is a DAG and it has no source
Pick any node ", and begin following edges backward from ". Since "has at least one incoming edge ($, ") we can walk backward to $.
Then, since $ has at least one incoming edge (', $), we can walk backward to '.
Repeat until we visit a node, say w, twice.
Let C be the sequence of nodes encountered between successive visits to w. C is a cycle.
17
w x u v
Cw x u v
DAG => Topological OrderLemma: If ! is a DAG, then ! has a topological order
Proof. (by induction on n)Base case: true if " = 1.
Hypothesis: Every DAG with " − 1 vertices has a topological ordering.
Inductive Step: Given DAG with " > 1 nodes, find a source node '.
! − { ' } is a DAG, since deleting ' cannot create cycles.
By hypothesis, ! − { ' } has a topological ordering.
Place ' first in topological ordering; then append nodes of ! − {'}in topological order. This is valid since ' has no incoming edges.
18
Reminder: Always remove vertices/edges to use hypothesis
A Characterization of DAGs
19
G has a topological order G is a DAG
20
Topological Order Algorithm 1: Example
2 3
6 5 4
7 1
21
Topological order: 1, 2, 3, 4, 5, 6, 7
Topological Order Algorithm 1: Example
2 3
6 5 4
7 1
1 2 3 4 5 6 7
Running time: O(n+m)• Adjacency list• Maintain # outgoing edge for each node