April16BFS Dijkstra
April16BFS Dijkstra
G F
BFS explores the closest vertices first
• So the intuition is that it should find shortest paths.
• How can we keep track of shortest paths/minimum
distances?
Augmented BFS
procedure BFS(G, s)
Input: Graph G = (V,E), (directed or undirected) and a vertex s in V.
Output: For all vertices u reachable from s, dist(u) is the distance from s to u. and for all
vertices u not reachable from s, dist(u) = ∞
for each vertex u in V:
dist(u)=∞
dist(s) = 0
Q = [s] (queue just containing s)
while Q is not empty
v = Q.dequeue
for all edges (v,u) in E
if dist(u)=∞ then
Q.enqueue(u); prev(u)=v;
dist(v)=dist(u) + 1
Example
A
B
C
H
E A B C D E F G H
G F
Proof of correctness
• For each vertex v, we want to show that dist(v) is the
minimum distance of all paths from s to v.
• Claim: at the first time when the head of the queue has
distance marked d:
• (1) all vertices with distances at distance ≤ 𝑑 from s have their
distance values correctly set.
• (2). All vertices in the queue are exactly those of distance d.
• (2) All other vertices are marked distance infinity
Claim: for each distance value d = 0, 1, 2,…:
•Corollary:
For each vertex 𝑣, dist(𝑣) is set at most one time.
Edge lengths (weights)
edges can be given values such as
• Distance
• Cost
• Time
• Bandwidth
• Value
BFS on weighted graphs.
• BFS only works to find shortest distances on graphs in
which each edge has equal weight.
A 3 B 2 C
2 1 4 1
D 4 E F
BFS on weighted graphs.
• Discuss how we can use a reduction to solve the
problem of shortest paths with edge lengths.
A 3 B 2 C
2 1 4 1
D 4 E F
BFS on weighted graphs.
• On a graph 𝐺 with integer edge lengths, form 𝐺 ! by adding ℓ" − 1 many new
vertices between 𝑢 and 𝑣 for every edge 𝑒 = (𝑢, 𝑣). Then run BFS on 𝐺′.
A 3 B 2 C A B C
2 1 4 1
D 4 E F D E F
𝐺 𝐺′
Problems with this method
If the edge lengths
(weights) are large
integers then it is
impractical.
In this example
with 10 vertices,
we must add 1,783
more vertices!!!!!!!!
Dijkstra’s algorithm
• Simulates what would happen if we introduced
intermediate vertices, but without actually introducing
intermediate vertices.
60
ORD 80
250
IAD
0
10
DEN
10
0
60
0
25 100
ATL
SAN 120
0
DFW 30 0
9
Graph reachability:
Dijkstra’s at a high-level
procedure GraphSearch (Dijkstra’s) (G: directed graph with edgeweights, s: vertex)
60
200
250
250
IAD,
IAD, ∞
0 270
420
110000
25
60
DEN,
DEN, ∞
100 100
SAN, 0 0
30 ATL,
ATL, ∞
210
10
120
0
90
DFW,
DFW,
120∞
DFW,
120
Correctness of Dijkstra’s
Claim: After Dijkstra’s is done, dist(v) is the length of the
shortest path from s to v.
S z w v
dist(s) = d(s) dist(z) = d(z) dist(w) > d(w) dist(v) > d(v)
Correctness of Dijkstra’s
(Let d(v) be the actual length of the shortest path from s to v.)
Then let z be the last vertex in the path s.t. dist(z) = d(z) and
Let w be the first vertex in the path s.t. dist(w) > d(w) (at the end)
Case 1: d(w) = d(z) + ℓ(𝑧, 𝑤). 𝑑𝑖𝑠𝑡 𝑧 = 𝑑 𝑧 ≤ 𝑑 𝑤 < 𝑑𝑖𝑠𝑡 𝑤 . By the
increasing distance lemma, z is moved into X before w. When z is chosen from
F, the algorithm would reset dist(w) = d(z) + ℓ 𝑧, 𝑤 = 𝑑(𝑤). So dist(w) = d(w)
at the end, a contradiction.
Case 2: d(w) < d(z) + ℓ(𝑧, 𝑤). Then the path p is actually not the shortest path.
S z w v
dist(s) = d(s) dist(z) = d(z) dist(w) > d(w) dist(v) > d(v)
Graph reachability:
Dijkstra’s high-level (Runtime)
procedure GraphSearch (Dijkstra’s) (G: directed graph with edgeweights, s: vertex)
(This claim implies that once a vertex moves into X, it will never move back to F. Therefore, every vertex
moves from F to X at most once
Graph reachability:
Dijkstra’s high-level (carefully choosing)
Claim: Let d(v) be the length of the shortest path from s to v. Then after every iteration, dist(v) = d(v) for all
vertices v in X.
Inductive Hypothesis: After k vertices have been moved into X, dist(v) = d(v) for all vertices v in X.
y
X w
P
u
Graph reachability:
Did we use non-negativity?
y
X w
P
u
• deletemin:
• decreasekey:
Array as a priority queue
• Array(hash table): indexed by vertex, giving key value.
Example: (A,2),(B,9),(C,4),(D,1),(E,6),(F,3),(G,4)
H[A] = 2, H[B] = 9 ,…..
• deletemin: O(|V|)
• decreasekey: O(1)
• Total runtime:
|V|*deletemin + |E|*decreasekey
|V|*O(|V|) + |E|*O(1) = O( 𝑉 ! )
Binary heap
A complete binary tree
of objects (vertices)
with the property that
each key value of an
object is less than or
equal to the key value
of its children.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Binary heap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
•
Binary heap (deletemin)
The object with the
minimum key value is
guaranteed to be the
root. Once you take it
out, you must reorder
the tree. You replace
the
root with the last object
and let it trickle
down.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Binary heap (deletemin)
•
Binary heap (decreasekey)
When you decrease a
key, you may have to
adjust the heap by
having the decreased
𝑂!
key object bubble up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
𝑂!
ONE COMPLICATION
• How do we know where v is in the binary heap?
•
Binary heap
• deletemin: 𝑂 log 𝑉
• decreasekey: 𝑂 log 𝑉