0% found this document useful (0 votes)
9 views

15_shortestPath

The document discusses shortest path algorithms in graph theory, focusing on single-source shortest path algorithms such as Bellman-Ford and Dijkstra's algorithm. It explains concepts like sub-optimality of shortest paths, triangle inequality, and the relaxation process, which are essential for finding the shortest paths in a graph. The document also highlights the differences between algorithms, their runtime complexities, and the conditions under which they operate effectively.

Uploaded by

attendancedckyb
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)
9 views

15_shortestPath

The document discusses shortest path algorithms in graph theory, focusing on single-source shortest path algorithms such as Bellman-Ford and Dijkstra's algorithm. It explains concepts like sub-optimality of shortest paths, triangle inequality, and the relaxation process, which are essential for finding the shortest paths in a graph. The document also highlights the differences between algorithms, their runtime complexities, and the conditions under which they operate effectively.

Uploaded by

attendancedckyb
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/ 26

CS 260 - Data Structures

Shortest Path
..Single Source Shortest Path Algorithms..
Yusuf Osmanlioglu
Department of Computer Science
Drexel University
Shortest Paths
● How do I get from point A to point B?
● Transportation
○ Find the cheapest way to travel between two locations
○ Find the shortest way to travel between two locations
● Motion
○ What is the best way to travel a robot in an environment
● Communication
○ Which two locations in a network are farthest apart
○ The shortest set of hubs to get a message between two nodes in a
network.

2
Shortest Paths
u v
6
3
s 4
2 1
2 7
3
5
6
x y

u v
6
3 9
3
s 4
2 1
0 2 7
3
5
5 11
6
x y 3
Single Source Shortest Paths
● Starting at node A, what is the shortest path to every other node in the graph?
● Each edge has a weight w(a, b) = x means, the edge from a → b has a weight of x.
● A path is an ordered list of nodes:
○ P = (a, c, b, d)
○ means path P, starts at a, takes edges a → c, c → b, b → d, and ends at d.
● The weight of a path is the sum of all edges along the path.
○ w(P) = ∑(u,v ) in Pw(u, v) = w(a, c) + w(c, b) + w(b, d)
● The shortest path from X to Y is the path with the smallest total weight.

4
Single Source Shortest Paths
● Given a graph G=(V,E), we want to find a shortest path from a source “s” to every vertex
v in V.
● Variants:
○ Single destination shortest path.
○ Single pair shortest path.
○ All-pairs shortest path.

5
Shortest Paths are not Unique
u v
6
3
s 4
2 1
2 7
3
5
6
x y

u v u v
6 6
3 9 3 9
3 3
s 4 s 4
2 1 2 1
0 2 7 0 2 7
3 3
5 5
5 11 5 11
6 6
x y x y 6
How to determine shortest paths?
● We will utilize two observations:
○ Sub-optimality of shortest paths
○ Triangle inequality

7
Sub-optimality of Shortest Paths
● Shortest path from
Philly to DC includes a
path from Wilmington
to Baltimore.
● Therefore, that path
must be the shortest
path between
Wilmington and
Baltimore.

8
Sub-optimality of Shortest Paths
● In plain English: Any sub-path within a shortest path is also a shortest path!

Pij

v1 v2 vi vj vk-1 vk

P1k

● More formally:
○ Given a weighted graph G=(V,E) and weight function w on edges.
○ Let P=<v1,v2,…,vk> be a shortest path from v1 to vk and for any i and j such that 1 ≤ i ≤ j ≤ k.
○ Let Pij=<vi,vi+1,…,vj> be the sub-path of P from vi to vj .
○ Then, Pij is the shortest path from vi to vj .
9
Sub-optimality of Shortest Paths
● Let G=(V,E) be a weighted, directed graph with weight function w.
● Suppose that a shortest path P from source s to a vertex v can be decomposed into a
path P’ and an edge (u,v) as follows:

P’

s u v

● Then the weight of shortest path from s to v is:


○ 𝛿(s,v) = 𝛿(s,u) + w(u,v)

10
Triangle Inequality
● Shortest path from Philly
Museum of Art to
Metropolitan Museum of Art
in NYC is 99 miles and goes
through NJ toll road
● Alternative route that goes
through Princeton (route 1) is
at least as long as the
shortest path.
○ which is 99.1 miles

11
Triangle Inequality
● Let G=(V,E) be a weighted, directed graph with weight function w.
● Then for all edges (u,v), we have
○ 𝛿(s,v) ≤ 𝛿(s,u) + 𝛿(u,v)

𝛿(s,u)
𝛿(u,v)
u
v
s
𝛿(s,v)

● That is to say: since 𝛿(s,v) is the shortest path between s and v, any alternative path
between s and v that goes through an internal node u which does not exist in the path 𝛿
(s,v) would have to be at least as long as 𝛿(s,v) .
12
Relaxation
● Relaxation is the process of comparing two paths and selecting the shorter.
● For each vertex v in V, we maintain an attribute v.d:
○ This is an upper bound on the shortest path from s to v.
○ We call v.d a shortest path estimate.
● Initially, all the shortest path estimates are infinity, except the distance of the start
node s to itself being set to 0.
● As algorithms proceeds, these values get closer and closer to actual value 𝛿(s,v) of
shortest path between s and v.

13
Relaxation
● The process of relaxing an edge (u,v) consists of testing whether we can improve the
shortest known path to v, by adding (u,v) to the shortest known path to u.
● A relaxation step may decrease the shortest path estimate d[v] using the triangle
inequality:

function Relax(u,v,w) 5 2 9 5 2 6
if d[v]> d[u]+w(u,v) then u v u v
d[v]= d[u]+w(u,v) Relax(u,v,w) Relax(u,v,w)
end if
end function
5 2 7 5 2 6
u v u v
14
Bellman-Ford Algorithm
● This is the most basic single-source shortest path algorithm:
○ The algorithm finds the shortest path from source s to every vertex v in the graph.
○ The actual shortest path can be constructed easily as it keeps parent pointers.
○ Starts with an estimate of shortest distance and eventually converges to shortest weight
paths.

15
Bellman-Ford: Example
Relax edges in the order of: (t,x),(t,y),(t,z),(x,t),(y,x),(y,z),(z,x),(z,s),(s,t),(s,y)

t 5 x t 5 x t x t x
5 5
∞ -2 ∞ 6 -2 ∞ 6 -2 11 6 -2 4
s 6 s 6 6 6
-3 -3 s s
-3 -3
0 8 0 8 0 8 0 8
2 7 2 7 2 7 2 7
-4 -4 -4 -4
7 7 7 7
∞ ∞ 7 ∞ 7 2 7 2
9 9 9 9
y z y z y z y z
initialization Iteration 1 Iteration 2

Iteration 3 Iteration 4
t 5 x t 5 x
2 -2 4 2 -2 4
s 6 s 6
-3 -3 Shaded edges represent parent relation
0 8 0 8
2 7 2 7
-4 -4
7 7
7 2 7 -2
9 9
y z y z 16
Bellman-Ford Algorithm
function Bellman-Ford(G, w, s)
for each v in G.V do ● Initialize v.d to infinity for each node v,
v.d = ∞ which will converge to shortest-path
v.p = NIL
end for value.
s.d = 0 ● Relaxation: will relax each edge |V|-1
for i=1 to |G.V|-1 do
for each edge (u,v) in G.E do times.
if v.d > u.d + w(u,v) then ● At the end, test to see if a solution is
v.d = u.d + w(u,v)
v.p = u found
end if ○ Finds a solution if no negative-weight
end for cycles exists.
end for
for each edge (u,v) in G.E do
if v.d > u.d + w(u,v) then
return false
return true
end function
17
Negative Cycles
● If the graph contains a negative cycle, then there wouldn’t exist a shortest path!
● Consider the following example, as we go around the cycle the path we get between v 1
and vk will become shorter.
● A shortest path may exist if no negative cycles exist, although there are negative edges.

𝛿<0

>0 >0 >0 >0

v1 v2 vi vk-1 vk
18
Bellman-Ford Algorithm : Runtime
function Bellman-Ford(G, w, s)
for each v in G.V do
v.d = ∞ O(V)
v.p = NIL ● Initialize takes O(V)
end for ● Relaxation of each edge
s.d = 0
for i=1 to |G.V|-1 do O(V) |V|-1 times takes O(VE)
for each edge (u,v) in G.E do O(E) in total.
if v.d > u.d + w(u,v) then
v.d = u.d + w(u,v) O(VE)
● Negative cycle test at
v.p = u the end takes O(V)
end if
end for ● Total runtime: O(VE)
end for
for each edge (u,v) in G.E do
if v.d > u.d + w(u,v) then
O(E)
return false
return true
end function
19
Bellman-Ford: Correctness
● After |V|-1 iterations, the d[v] represent the shortest path between s and each vertex v:
○ Initially: d[s]=0
○ Let s→v1→v2→ ... →vk denote the shortest path between s and vk. Then:
■ After 1st iteration d[v1] is correct, since d[v1] = d[s]+w(s,v1).
■ After 2nd iteration d[v2] is correct, since d[v2] = d[v1]+w(v1,v2).
■ ...
■ After kth iteration d[vk] is correct, since d[vk] = d[vk-1]+w(vk-1,vk).
○ This holds for all vertices, since the longest path in the graph has length |V|-1.

20
Dijkstra’s Algorithm
● This algorithm works only for the graphs with non-negative edge weights
● The result of this algorithm is similar to BFS if the graph is unweighted.
● Like Prim’s algorithm, uses a priority queue.
● Has better running time than Bellman-Ford.

21
Dijkstra’s Algorithm
function Dijkstra(G, w, s)
for each v in G.V do ● Initialize of v.d for each node v is similar
v.d = ∞ to Bellman-Ford.
v.p = NIL
end for ● Relaxation: each vertex can be subject
s.d = 0 to relaxation as many times as its
Q = V
while Q ≠ ∅ do in-degree.
u = Extract-Min(Q) ● The changes due to relaxation will be
for each vertex v in G.Adj[u] do
if v.d > u.d + w(u,v) then handled by Decrease-Key function.
v.d = u.d + w(u,v)
v.p = u
end if
end for
end while
end function

22
Dijkstra’s Algorithm: Example
t 1 x t 1 x t 1 x
∞ ∞ 10 ∞ 8 14
10 10 10
s 9 s 9 s 9
2 3 2 3 2 3
0 7 4 6 0 7 4 6 0 7 4 6

5 5 5
∞ ∞ 5 ∞ 5 7
2 2 2
y z y z y z

t 1 x t 1 x t 1 x
8 9 8 9 8 13
10 10 10
s 9 s 9 s 9
2 3 2 3 2 3
0 7 4 6 0 7 4 6 0 7 4 6

5 5 5
5 7 5 7 5 7
2 2 2
y z y z y z 23
Dijkstra’s Algorithm: Runtime
function Dijkstra(G, w, s)
for each v in G.V do ● Extract-Min:
v.d = ∞ O(V) ○ |V| times.
v.p = NIL
end for
● Decrease-Key:
s.d = 0 ○ |E| times.
Q = V ● Total:
while Q ≠ ∅ do O(V)
u = Extract-Min(Q) O(logV) O(VlogV) ○ O(VlogV + ElogV)
for each vertex v in G.Adj[u] do
if v.d > u.d + w(u,v) then
v.d = u.d + w(u,v)
v.p = u
end if O(ElogV)
end for
end while
end function

24
Dijkstra’s Algorithm: Correctness
● When adding a new node to path:
○ If all edges have weight w(u,v) > 0 then the node with the current shortest path is correct
○ Making a cycle cannot impact the path weight if w(u,v) > 0
○ If w (u,v) = 0 then distance will not improve and edge will be ignored.
○ If w (u,v) < 0 adding new edges to the shortest path makes it shorter!
○ Dijkstra’s algorithm only works for w(u,v) > 0

25
Negative Weight Edges in Dijkstra’s
● Consider the shortest path starting from s in the following graph using Dijkstra’s.

5 ∞ 5 5 5 5
u -10 u -10 u -10

0 2 ∞ 4 ∞ 0 2 2 4 ∞ 0 2 2 4 6
s v t s v t s v t

5 5 5 5 5 5
u -10 u -10 u -10

0 2
-5
4 6 0 2
-5
4 6 0 2
-5
4 6
s v t s v t s v t

26

You might also like