0% found this document useful (0 votes)
38 views5 pages

Algorithms: Single Source Shortest Path Problem

Here is an example graph with negative edge weights if (d[v] > d[u] + w(u,v)) that would not work with Dijkstra's algorithm: d[v] = d[u] + w(u,v); A graph with a negative weight cycle. For example: s - 2 -> v - -3 -> s Dijkstra's algorithm would get stuck in an infinite loop updating the distance to s, since relaxing the negative edge from v to s would continue to decrease the distance. Bellman-Ford is needed to handle graphs with negative edge weights.

Uploaded by

Maor Cohen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Algorithms: Single Source Shortest Path Problem

Here is an example graph with negative edge weights if (d[v] > d[u] + w(u,v)) that would not work with Dijkstra's algorithm: d[v] = d[u] + w(u,v); A graph with a negative weight cycle. For example: s - 2 -> v - -3 -> s Dijkstra's algorithm would get stuck in an infinite loop updating the distance to s, since relaxing the negative edge from v to s would continue to decrease the distance. Bellman-Ford is needed to handle graphs with negative edge weights.

Uploaded by

Maor Cohen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Single Source Shortest Path

Problem
Algorithms  Given a weighted, directed graph 1
G=(V, E), with weight function 5
10
w: E → R. 1

2 8 3
Dana Shapira 3 4
 Single source shortest path
Lesson #7: problem: given vertex s, for every 1
vertex vV find a shortest path 1 1 3
Single source shortest path problem 6 4
from s to v.
6 2 5
 Dijkstra’s algorithm solves this
problem efficiently for the case in
which all weights are nonnegative.
1 2

Shortest Path Properties Shortest Path Properties


 We have optimal substructure: the shortest path  Define (u,v) to be the weight of the shortest path
consists of shortest subpaths: from u to v
 Shortest paths satisfy the triangle inequality:
(u,v)  (u,x) + (x,v)

x
 Proof: suppose some subpath is not a shortest path
 There must then exist a shorter subpath

 Could substitute the shorter subpath for a u v


shorter path
 But then overall path is not the shortest path.
This path is no longer than any other path
- Contradiction 3 4
Shortest Path Properties Shortest Path Properties
 Define (u,v) to be the weight of the shortest path
 In graphs with negative weight cycles, some shortest
from u to v
 Shortest paths satisfy the triangle inequality: paths will not exist (Why?):
(u,v)  (u,x) + (x,v)
 For each edge (x,v) (u,v)  (u,x) + (x,v) (why?)

x
< 0

u v

This path is no longer than any other path 5 6

Relaxation Dijkstra’s Algorithm


For all v, maintain upper bound d[v] on (s,v) Dijkstra(G,s) B
Relax(u,v,w) { 10 2
for each v  V
if (d[v] > d[u]+w(u,v)) then d[v] = ; (v)=NULL; A 4 3 D
d[v]=d[u]+w(u,v); d[s] = 0; S = ; Q = V; (s)=NULL;
5 1
} while (Q  ) C
u = ExtractMin(Q);
S = S U {u};
2 2 for each v  Adj[u]
5 9 5 6
if (d[v] > d[u]+w(u,v))
d[v] = d[u]+w(u,v); Relaxation
Relax Relax
(v)=u Step
2 2
5 7 5 6 What will be the total running time?
7 8
Correctness Of Dijkstra's Correctness Of Dijkstra's
Algorithm Algorithm
Lemma: d[v]  (s,v) v always We show that when u is removed from Q, it has already
converged
 Initially true
p2
 Let v be first vertex for which d[v] < (s,v) u
s
 Let u be the vertex that caused d[v] to change:
y
d[v] = d[u] + w(u,v) x
p1
 Then d[v] < (s,v)  (s,u) + w(u,v)  d[u] + w(u,v)
(Why?)  By the Lemma, d[v]  (s,v) v
 So d[v] < d[u] + w(u,v). - Contradiction  Let u be first vertex in S such that d[u]  (s,u) when it is
9 removed from Q. 10

Correctness Of Dijkstra's Correctness Of Dijkstra's


Algorithm Algorithm
p2 p2
u u
s s
y y
x x
p1 p1
 Let y be first vertex V-S on actual shortest path from su  d[u]> (s,u)
 d[y] = (s,y) = (s,y) + (y,u) (Why?)
 Because d[x] is set correctly for y's predecessor x  S on = d[y] + (y,u)
the shortest path (u was the first vertex in S such that  d[y]
d[u]  (s,u)) ,  But if d[u] > d[y], wouldn't have chosen u.
Contradiction.
 When we put x into S, we relaxed (x,y), giving d[y] the 11 12

correct value
Question Bellman-Ford Algorithm
 Can you give an example of a graph that includes BellmanFord()
Initialize d[], which
for each v  V
negative weights, and does not work with Dijkstra’s d[v] = ;
will converge to
shortest-path value 
algorithm? d[s] = 0;
0 4 for i=1 to |V|-1
Relaxation:
4 4 for each edge (u,v)  E Make |V|-1 passes,
A B A B Relax(u,v, w(u,v)); relaxing each edge
for each edge (u,v)  E
-2 if (d[v] > d[u] + w(u,v)) Test for solution
3 3 return “no solution”; Under what condition
do we get a solution?
4 3
D C D C Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
1 1

13 14

Bellman-Ford Algorithm Bellman-Ford Algorithm


BellmanFord() What will be the running time? BellmanFord()
for each v  V for each v  V -1 B
s 2
d[v] = ; A: O(|V||E|) d[v] = ;
d[s] = 0; d[s] = 0; A 3 2 E
for i=1 to |V|-1 for i=1 to |V|-1
for each edge (u,v)  E for each edge (u,v)  E 1
4 -3
Relax(u,v, w(u,v)); Relax(u,v, w(u,v));
for each edge (u,v)  E for each edge (u,v)  E
C D
if (d[v] > d[u] + w(u,v)) if (d[v] > d[u] + w(u,v)) 5
return “no solution”; return “no solution”;
Example

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

15 16
Correctness of Bellman-Ford Bellman-Ford Example
 Show that d[v] = (s,v) after |V|-1 passes
 Consider shortest path from s to v: s 6 B -2
s  v1  v2  v3 …  v
A 5 E
 Initially, d[s] = 0 is correct, and doesn’t change 2 -3
(Why?) 7 8 7
 After 1 pass through edges, d[v1] is correct
(Why?) and doesn’t change C
9
D
 After 2 passes, d[v2] is correct and doesn’t
change
 …

 Terminates in |V| - 1 passes: (Why?)


17 18
 What if it doesn’t?

DAG Shortest Paths


 Problem: finding shortest paths in DAG
 Bellman-Ford takes O(|V||E|) time.

 How can we do better?

 Idea: use topological sort

 If were lucky and processes vertices on each


shortest path from left to right, would be done in
one pass
 Every path in a DAG is subsequence of topologically
sorted vertex order, so processing vertices in that
order, we will do each path in forward order (will
never relax edges out of vertices before doing all
edges into vertices).
 Thus: just one pass. What will be the running time?
19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy