Lec 17
Lec 17
Keren Zhu
[email protected]
Announcement
• Quiz 2 on April 15 in class
• HW 8 is posted and is due on April 23
o No late submission this time
2
Graph traversal
• Graph traversal is very similar to tree traversal
• The difference is that we need to record whether a vertex is
visited and we need to visit all unvisited vertices
• Traversals of graphs are also called searches
3
Strategies
We can use either breadth-first or depth-first traversals
o Breadth-first requires a queue
o Depth-first requires a stack
We each case, we will have to track which vertices have been visited
requiring Q(|V|) memory
o One option is a hash table
o If we can use a bit array, this requires only |V|/8 bytes
The time complexity cannot be better than and should not be worse
than Q(|V| + |E|)
o Connected graphs simplify this to Q(|E|)
o Worst case: Q(|V|2)
4
Breadth-first traversal
Consider implementing a breadth-first traversal on a graph:
Choose any vertex, mark it as visited and push it onto queue
While the queue is not empty:
Pop to top vertex v from the queue
For each vertex adjacent to v that has not been visited:
Mark it visited, and
Push it onto the queue
5
Depth-first traversal
Consider implementing a depth-first traversal on a graph:
Choose any vertex, mark it as visited
From that vertex:
If there is another adjacent vertex not yet visited, go to it
Otherwise, go back to the most previous vertex that has not yet
had all of its adjacent vertices visited and continue from there
Continue until no visited vertices have unvisited adjacent vertices
Two implementations:
Recursive
Iterative
6
Example
Consider this graph
7
Example
Performing a breadth-first traversal
o Push the first vertex onto the queue
8
Example
Performing a breadth-first traversal
o Pop A and push B, C and E
A
B C E
9
Example
Performing a breadth-first traversal:
o Pop B and push D
A, B
C E D
10
Example
Performing a breadth-first traversal:
o Pop C and push F
A, B, C
E D F
11
Example
Performing a breadth-first traversal:
o Pop E and push G and H
A, B, C, E
D F G H
12
Example
Performing a breadth-first traversal:
o Pop D
A, B, C, E, D
F G H
13
Example
Performing a breadth-first traversal:
o Pop F
A, B, C, E, D, F
G H
14
Example
Performing a breadth-first traversal:
o Pop G and push I
A, B, C, E, D, F, G
H I
15
Example
Performing a breadth-first traversal:
o Pop H
A, B, C, E, D, F, G, H
16
Example
Performing a breadth-first traversal:
o Pop I
A, B, C, E, D, F, G, H, I
17
Example
Performing a breadth-first traversal:
o The queue is empty: we are finished
A, B, C, E, D, F, G, H, I
18
Example
Perform a recursive depth-first traversal on this same graph
19
Example
Performing a recursive depth-first traversal:
o Visit the first node
A
20
Example
Performing a recursive depth-first traversal:
o A has an unvisited neighbor
A, B
21
Example
Performing a recursive depth-first traversal:
o B has an unvisited neighbor
A, B, C
22
Example
Performing a recursive depth-first traversal:
o C has an unvisited neighbor
A, B, C, D
23
Example
Performing a recursive depth-first traversal:
o D has no unvisited neighbors, so we return to C
A, B, C, D, E
24
Example
Performing a recursive depth-first traversal:
o E has an unvisited neighbor
A, B, C, D, E, G
25
Example
Performing a recursive depth-first traversal:
o F has an unvisited neighbor
A, B, C, D, E, G, I
26
Example
Performing a recursive depth-first traversal:
o H has an unvisited neighbor
A, B, C, D, E, G, I, H
27
Example
Performing a recursive depth-first traversal:
o We recurse back to C which has an unvisited neighbour
A, B, C, D, E, G, I, H, F
28
Example
Performing a recursive depth-first traversal:
o We recurse finding that no other nodes have unvisited
neighbours
A, B, C, D, E, G, I, H, F
29
Comparison
Performing a recursive depth-first traversal:
o We recurse finding that no other nodes have unvisited
neighbours
A, B, C, D, E, G, I, H, F
30
Comparison
The order in which vertices can differ greatly
o An iterative depth-first traversal may also be different again
A, B, C, E, D, F, G, H, I A, B, C, D, E, G, I, H, F
31
CLRS BFS procedures
• We can add a few attributes to each vertex
• Color: determine the status of BFS process
o White: undiscovered
o Gray: discovered for the fist time
o Black: when its edges have been explored
o Gray and black is just for illustration, it does not impact
the results
• We can record a few things
o holes the distance from the source s to v
o holds v’s predecessor
32
CLRS BFS procedures
• Basically the same as our previous illustration
33
BFS and shortest path
• The BFS procedure computes the shortest paths to every
connected vertices from the source , in an unweighted
directed or undirected graph
• Intuition is simple: BFS from s will explore all reachable
vertices, in a order of ascending distances from s
• CLRS Chapter 20 has a complete proof
• Runs in
• We needs other shortest path algorithm if the distances of
edges are different
34
CLRS DFS algorithm
• Color is similar to BFS
• denotes the discovery
time
• denotes the finish time
• denotes the predecessor
• Line 7 will explore a DFS
tree from
• The Line 6 for loop makes
sure the entire graph is
visited
o Construct a DFS
forest
35
• T: tree
• B: back
• F:
forward
• C: cross
36
Runtime
• The procedure DFS-VISIT is called exactly once for each
vertex,
• During an execution of DFS-VISIT, the loop in lines 4-7
executes times
37
Cycle theorem
• Let T be an arbitrary DFS forest. G contains a cycle if and
only if there is a back edge in T
38
Parenthesis theorem
• The timestamps can helps know the relations of nodes in the
forest
• In any DFS of a (directed or undirected) graph for any two
vertices and , exactly one of the following three conditions
holds
o the intervals and are disjoint, and neither nor is a descendant
of the other in DFS forest
o is entirely within then is a descendant of in a DFS tree
o is entirely within then is a descendant of in a DFS tree
39
White-path theorem
• In a DFS forest, vertex is a descendant of vertex if and only
if at the time that the search discovers , there is a path from
to consisting of entirely of white vertices.
• Example:
40
Proof of
• Trivial case: if , then the path from to is just the vertex ,
which is a white at the time of receives a value
• Nontrivial case: is a proper descendant of in the DFS forest,
and there is a path from to
o By parenthesis theorem, we know , therefore , then at
the moment, v is still white
o This observation can be extended to all the vertices in
the unique path from to
41
Proof of
• Suppose it is not true, that is suppose there is a path of
white vertices from to at time, but does not become a
descendant of in the DFS tree
• WLOG, let be the the first vertex on path from to , that is
not a descendant of
• Let be its predecessor on ( can be )
42
Proof of
• Consider the moment , when it turns black
• The color of cannot be white, ie.
o Otherwise, the procedure need to first discover
• So is either white or gray
o , therefore must be a descendent which contradicts
• Intuitionally, when enter stack, was white. There must be
pushed into the stack was still in the stack
43
Topological sort and DAG
• A a directed acyclic graph (DAG)
o is directed
o does not contain cycle
• In many application, we hope our graph has no cycle
o A graph representing pre-requisite of courses
o A dependent task graph
44
Restriction of paths in DAGs
In a DAG, given two different vertices vj and vk,
there cannot both be a path from vj to vk and a path from vk to vj
Proof:
Assume otherwise; thus there exists two paths:
(vj, v1,1, v1,2 , v1,3 , … , vk)
(vk, v2,1, v2,2 , v2,3 , … , vj)
From this, we can construct the path
(vj, v1,1, v1,2 , v1,3 , … , vk, v2,1, v2,2 , v2,3 , … , vj)
This a path is a cycle, but this is an acyclic graph
∴ contradiction
45
Definition of topological sorting
A topological sorting of the vertices in a DAG is an ordering
v1, v2, v3, …, v|V|
such that vj appears before vk if there is a path from vj to vk
46
Definition of topological sorting
Given this DAG, a topological sort is
H, C, I, D, J, A, F, B, G, K, E, L
47
Example
For example, there are paths from H, C, I, D and J to F, so all these
must come before F in a topological sort
H, C, I, D, J, A, F, B, G, K, E, L
48
Applications
Consider the course instructor getting ready for a dinner out
49
Applications
The following is a task graph for getting dressed:
Proof strategy:
Such a statement is of the form a ↔ b and this is equivalent
to:
a → b and b → a
52
Topological Sort
First, we need a two lemmas:
A DAG always has at least one vertex with in-degree zero
That is, it has at least one source
Proof:
If we cannot find a vertex with in-degree zero, we will show there must be a
cycle
Start with any vertex and define a list L = (v)
Then iterate this loop |V| times:
Given the list L, the first vertex ℓ1 does not have in-degree zero
Find any vertex w such that (w, ℓ1) is an edge
Create a new list L = (w, ℓ1, …, ℓk)
By the pigeon-hole principle, at least one vertex must appear twice
This forms a cycle; hence a contradiction, as this is a DAG
53
Topological Sort
First, we need a two lemmas:
Any sub-graph of a DAG is a DAG
Proof:
If a sub-graph has a cycle, that same cycle must appear in the
super-graph
We assumed the super-graph was a DAG
This is a contradiction
54
Proof of lemma 1
• We will start with showing a → b:
o If a graph is a DAG, it has a topological sort
• By induction:
o A graph with one vertex is a DAG and it has a
topological sort
o Assume a DAG with n vertices has a topological sort
o A DAG with n + 1 vertices must have at least one vertex
v of in-degree zero
55
Proof of lemma 1
• Continue on induction:
o Removing the vertex v and consider the vertex-induced
sub-graph with the remaining n vertices
If this sub-graph has a cycle, so would the original
graph—contradiction
Thus, the graph with n vertices is also a DAG, therefore
it has a topological sort
o Add the vertex v to the start of the topological sort to get one for
the graph of size n + 1
56
Proof of lemma 2
• Next, we will show that b → a: If a graph has a topological ordering, it
must be a DAG
• We will show this by showing the contrapositive: ¬a → ¬b:
o If a graph is not a DAG, it does not have a topological sort
o By definition, it has a cycle: (v1, v2, v3, …, vk, v1)
In any topological sort, v1 must appear before v2, because (v1, v2) is
a path
However, there is also a path from v2 to v1: (v2, v3, …, vk, v1)
Therefore, v2 must appear in the topological sort before v1
o Contradiction, therefore b → a
• With the two lemmas, we have the theorem:
o A graph is a DAG iff it has a topological sort
57
Topological sort procedure
• Using DFS can easily produce a topological sort of a graph
• Runs in , same as DFS asymtopically
• But the question is how can we say it is correct
58
Proof topological-sort correctness
• Lemma: a directed graph is acyclic iff a DFS of yields no
back edges
• ->: Suppose a DFS produces a back edge Then is an
ancestor of vertex of in the DFS forest. Thus, contains a
path from to , and the back edge completes a cycle
• <-: Suppose contains a cycle . We show that a DFS ofyield a
back edge.
o Let be the first vertex to be discovered in , and let be
the preceding edge in . At time , the vertices of form a
path of white vertices from to .
o Then by the white-path theorem, vertex becomes a
descendant ofin the DFS forest. Therefore is a back edge
and contradicts our assumption.
59
Proof topological-sort correctness
• Then let’s prove the algorithm is correct
• Suppose DFS is run a given DAG
• We want to use that for any pair of distinct vertices , if
contains an edge then , which prove the correctness of our
algorithm
• Consider any edge explored by DFS().
• When this edge is explored, cannot be gray, since then
would be an ancestor of andwould be a back edge.
• Therefore, must be either white or black.
• If white, becomes a descendant of u, and therefore
• If black, it has already finished so that has been set.
Eventually after u is finished,
60