disc04_slides
disc04_slides
DISCUSSION 4
GRAPHS AND PATHS
Raymond Chan
UC Berkeley Fall 17
DEPTH FIRST SEARCH
• O(|V| + |E|)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D
I
T T
T
B T T (8, 19)
G H F J
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D
I
T T
T
B T T (8, 19)
G H F J
• If (u, v) is an edge in an indirect graph and during DFS, post(v) < post(u),
then u is an ancestor of v in the DFS tree.
• True
• For any two nodes, u, v, [pre(u), post(u)] and [pre(v), post(v)] are
either disjoint or one is contained in the other.
(1, 6)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D
I
T T
T
B T T (8, 19)
G H F J
w v
T
u B
(2, 3)
• In a directed graph, if there is a path from u to v and pre(u) < pre(v) then
u is an ancestor of v in the DFS tree.
• Consider the case when u and v are a common ancestor’s direct children.
(1, 6) T (4, 5)
w v
T
u B
(2, 3)
• u gets visited first via w, who then visits v.
• False
• True
• True
(1, 6)
root
T A
T C (7, 20)
(2, 3)
ancestor to F, J, D, …
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
(1, 6)
root
T A
T C (7, 20)
(2, 3)
ancestor to F, J, D, …
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
(1, 6)
root
T A
T C (7, 20)
(2, 3)
ancestor to F, J, D, …
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
(1, 6)
root
T A
T C (7, 20)
(2, 3)
ancestor to F, J, D, …
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
• Run DFS.
(1, 6)
root
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
(1, 6)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
(1, 6)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
• Visualize it
• https://www.cs.usfca.edu/~galles/JavascriptVisual/
ConnectedComponent.html
(1, 6)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
• Source:
• Sink
• No outgoing edges
(1, 6)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
(1, 6)
T A
T C (7, 20)
(2, 3)
B E C
(4, 5)
B
C C
C T
(13, 16) (10,11) D F
I
T T
T
B T T (8, 19)
G H J F
B E C
D
I
G H J F
C, D, G, J G, H, I A B E
• Explore node u’s neighbors, then all vertices that are adjacent to
u’s neighbors, and so on.
• Can keep distance values to find how many edges a node is away
from the root.
iterative_BFF (G, v):
• Visits all vertices once. d[v] = 0
queue.enqueue(v)
while queue not empty:
• Uses all edges once. v = pop from queue
for all v’s neighbors w:
• O(|V| + |E|) if w not visited
mark w as visited:
d[w] = d[v] + 1
queue.enqueue(w)
• When we pop off a node from Priority Queue, the distance is the
shortest path from s to this node so far.
dijkstra (G, s):
d[v] = infinity
d[s] = 0
prev[s] = s
PQ.add(G.V, infinity)
PQ.add(s,0)
while PQ not empty:
u = PQ.DeleteMin()
for edge (u, v):
if d[v] > d[u] + w(u, v):
d[v] = d[u] + w[u, v]
prev[v] = u
PQ.DecreaseKey(v, d[v])
Raymond Chan, UC Berkeley Fall 2017
DIJKSTRA’S SHORTEST PATH
Demo
dijkstra (G, s):
d[v] = infinity
d[s] = 0
prev[s] = s
PQ.add(G.V, infinity)
PQ.add(s,0)
while PQ not empty:
u = PQ.DeleteMin()
for edge (u, v):
if d[v] > d[u] + w(u, v):
d[v] = d[u] + w[u, v]
prev[v] = u
PQ.DecreaseKey(v, d[v])
3
C