05 CME4422 ShortestPathAlgorithms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Shortest Path Algorithms

CME4422 – Introduction to Graph Theory

Asst. Prof. Dr. Feriştah DALKILIÇ


feristah@cs.deu.edu.tr
Outline

◘ Single-Source Shortest Path Algorithms


– Dijkstra’s Algorithm
– Bellman-Ford Algorithm
◘ All-Pairs Shortest-Paths Algorithms
– Floyd-Warshall Algorithm
Introduction

Generalization of BFS to handle weighted graphs


◘ Direct Graph G = (V, E), edge weight w : E → R
◘ In BFS w(e)=1 for all e  E.

Weight of path p = v1 → v2 → … → vk is

k −1
w( p ) =  w(vi , vi +1 ) = sum of edge weights on path p
i =1
Shortest Path

Shortest Path = Path of minimum weight


p
δ(u,v)= min{w(p) : u v}; if there is a path from u to v,
 otherwise.
Shortest-Path Variants
◘ Single-source shortest-paths problem: Find the shortest
path from s to each vertex v. (e.g. BFS)

◘ Single-destination shortest-paths problem: Find a


shortest path to a given destination vertex t from each
vertex v.

◘ Single-pair shortest-path problem: Find a shortest path


from u to v for given vertices u and v.

◘ All-pairs shortest-paths problem: Find a shortest path


from u to v for every pair of vertices u and v.
Applications of SP Algorithms

◘ Find directions between physical locations (e.g., Google


Maps)
◘ Internet routing protocols (e.g., find a route over modems
connect to computer)
◘ Find the degree of separation of two persons in a social
network
Negative-weight edges

◘ If we have a negative-weight cycle, just keep going


around it, and get w(s, v) = −∞ for all v on the cycle.
◘ There is no problem, as long as no negative-weight
cycles are reachable from the source.
Examples of negative-weight edges

◘ Logistic network
where the weight w(i,j) of an edge ij is the cost to go from vertex i to vertex j. If you made
a business agreement with other companies to transport their products, w(i,j) would be a
profit instead of a cost, so you can interpret this weight as a negative cost.
◘ Traffic congestion
where the weights represent traffic conditions in a map (more negative, more
unfavorable) - we could then use this representation to compute optimal distances.
◘ Chemistry
where the weights can be used to represent the heat produced during a chemical
reaction. Nodes: compounds, Edge euv: if compound v can be obtained from u. (You
produce 4 kJ to convert s to a and 2 kJ to convert a to t. You need 5 kJ to get back s
from t.)
◘ Currency exchange market
where nodes represent nations and whose edges represent the percentage gain or loss
of transferring one currency for another. National currencies are sometimes overvalued
or undervalued. The cheapest path from one node to another represents the cheapest
way to transform one currency into another.
Cycles

Shortest paths can’t contain cycles:


– Negative-weight cycles: already ruled out.
– Positive-weight cycles: we can get a shorter path by
omitting the cycle.
– Zero-weight cycles: no reason to use them, assume
that our solutions won’t use them.
Optimal Substructure Property
Theorem: Any subpath of a shortest path is also a
shortest path

Proof: By cut and paste.


pux pxy pyv
u x y v

Suppose this path p is a shortest path from u to v.


Then
δ(u,v) = w(p) = w(pux) + w(pxy) + w(pyv).
Graph Initialization

◘ All the shortest-paths algorithms start with INIT-SINGLE-SOURCE.

INIT(G, s) v.d = δ(s,v)


for each v  V do v.π = predecessor of on a
v.d = ∞ shortest path from s.
v.π = NIL
s.d = 0

◘ Can we improve the shortest-path estimate for v by going


through u and taking (u,v)?
Relaxation

u v
RELAX(u, v) 5
2
9
if v.d > u.d + w(u,v)
Relax(u,v)
v.d = u.d + w(u,v)
5 7
v.π = u 2

u v
2
5 6

No Relaxation

5 6
2
Properties of Relaxation

Triangle Inequality:
For a given vertex s  V and for every edge (u,v)  E,
δ(s,v) ≤ δ(s,u) + w(u,v)

Upper-bound property:
We always have v.d ≥ δ(s,v) for all vertices v  V .
Once v.d achieves the δ(s,v) value, it never changes.

No-path property:
If there is no path from s to v, then always have v.d =δ(s,v)=∞
Properties of Relaxation
Covergence property:
If s u → v be a shortest path from s to v for some u,v  V
and
if u.d=δ(s, u) at ay time proior to Relax(u,v) then v.d=δ(s, v)
all times after Relax(u,v)

Predecessor-subgraph property:
Once v.d=δ(s, v) for all v  V, the predecessor-subgraph is a
shortest-paths tree rooted at s.
Single-Source Shortest Path Algorithms

◘ Dijkstra’s Algorithm:
– greedy approach
– similar to BFS
– works for no negative-weight edges

◘ Bellman-Ford Algorithm:
– edge weights can be negative
Dijkstra’s Algorithm

• No negative-weight edges DIJKSTRA (G, w, s)


INIT (G, s)
S=
• Weighted version of BFS Q = G.V //insert all vertices into Q
while Q  
• Keys are shortest-path u = EXTRACT-MIN (Q)
weights: v.d S = S  {u}
for each v  G.Adj[u]
RELAX (u, v)

• Two sets of vertices:


S: vertices whose final SP-path weights are found.
Q: priority queue = V – S

• Repeatedly selects u in Q with minimum SP estimate


(greedy choice).
Example
u v
1
 

10
9
2 3
s 0 4 6

5 7
 
2
x y

s x u v y
𝑑𝑖𝑠𝑡(𝑢) 0 ∞ ∞ ∞ ∞ 𝑑𝑖𝑠𝑡(𝑢)
Q 𝜋(𝑢) NULL NULL NULL NULL NULL S 𝜋(𝑢)
Example
u v
1
10 

10
9
2 3
s 0 4 6

5 7
5 
2
x y

x u v y s
𝑑𝑖𝑠𝑡(𝑢) 5 10 ∞ ∞ 𝑑𝑖𝑠𝑡(𝑢) 0
Q 𝜋(𝑢) s s NULL NULL
S 𝜋(𝑢) NULL
Example
u v
1
8 14

10
9
2 3
s 0 4 6

5 7
5 7
2
x y

y u v s x
𝑑𝑖𝑠𝑡(𝑢) 𝟕 8 14 𝑑𝑖𝑠𝑡(𝑢) 0 5
Q 𝜋(𝑢) x x x
S 𝜋(𝑢) NULL s
Example
u v
1
8 13

10
9
2 3
s 0 4 6

5 7
5 7
2
x y

u v s x y
𝑑𝑖𝑠𝑡(𝑢) 𝟖 13 𝑑𝑖𝑠𝑡(𝑢) 0 5 7
Q 𝜋(𝑢) x y
S 𝜋(𝑢) NULL s x
Example
u v
1
8 9

10
9
2 3
s 0 4 6

5 7
5 7
2
x y

v s x y u
𝑑𝑖𝑠𝑡(𝑢) 𝟗 𝑑𝑖𝑠𝑡(𝑢) 0 5 7 8
Q 𝜋(𝑢) u
S 𝜋(𝑢) NULL s x x
Example
u v
1
8 9

10
9
2 3
s 0 4 6

5 7
5 7
2
x y

s x y u v
𝑑𝑖𝑠𝑡(𝑢) 𝑑𝑖𝑠𝑡(𝑢) 0 5 7 8 9
Q 𝜋(𝑢) S 𝜋(𝑢) NULL s x x u
Backtracking for the shortest path
u v
1
8 9 Backtracking from v
v→u→x→s
10
9 Shortest path from s to v
2 3
s 0 4 6 p=s→x→u→v

5 7 w(p) = 9
5 7
2
x y
s x y u v
shortest path lengths
𝑑𝑖𝑠𝑡(𝑢) 0 5 7 8 9
S 𝜋(𝑢) NULL s x x u
from s to other vertices
Example 2

∞ 30 ∞ 70 69

77
0 ∞ 36 78

∞ 50 49 48 ∞ 59 58
Analysis
◘ Running time depends on implementation of priority queue.
◘ INIT (G,s): Θ(V) time
◘ While loop:
– EXTRACT-MIN(Q) executed |V| times
– RELAX (u,v) executed |E| times

◘ Total Time :
– O(V2) using linear array for priority queue.
– O((V + E) lgV) = O(E lgV) using binary heap.
Negative weighted graph

◘ Dijkstra’s Algorithm works for non-negative weighted edges.


◘ Bellman-Ford Algorithm works for negative weights!
5
t x
-2

6 -3

8 7
s 2
-4
7

9
y z
Bellman-Ford Algorithm

Initialize v.d=∞, v.π =NIL


for v  V
if v.d > u.d + w(u,v)
v.d = u.d + w(u,v), v.π = u

Check for negaive-weight


cycles.

The algorithm returns TRUE iff the graph contains no


negative-weight cycles that are reachable from the source s.
Example

5
t x

-2  i s t y x z
-3 0 0    
6
8 7
s 0 2
-4
7
 
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example

5
t x
6
-2  i s t y x z
-3 0 0    
6
1 0 6s 7 s  
8 7
s 0 2
-4
7
7 
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example

5
t x
6
-2 4 i s t y x z
-3 0 0    
6
1 0 6s 7 s  
8 7
s 0 2 2 0 6s 7 s 4y 2 t
-4
7
7 2
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example

5
t x
2
-2 4 i s t y x z
-3 0 0    
6
1 0 6s 7 s  
8 7
s 0 2 2 0 6s 7 s 4y 2 t
-4 3 0 2x 7 s 4y 2 t
7
7 2
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example

5
t x
2
-2 4 i s t y x z
-3 0 0    
6
1 0 6s 7 s  
8 7
s 0 2 2 0 6s 7 s 4y 2 t
-4 3 0 2x 7 s 4y 2 t
7 4 0 2 x 7 s 4 y -2 t
7 -2
9 shortest path lengths
y z from s to other vertices
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Backtracking for the shortest path

5
t x
2
-2 4 i s t y x z
-3 4 0 2 x 7 s 4 y -2 t
6
8 7
s 0 2
Backtracking from z
-4 z→t→x→y→s
7
7 -2 Shortest path from s to z
9
y z p=s→y→x→t→z

w(p) = -2
Analysis

O(V)

O(V.E)

O(E)

• Total Run-time : O(V+VE+E) = O(VE)


• Bellman Ford's algorithm and Dijkstra's algorithm are very similar in
structure. While Dijkstra looks only to the immediate neighbors of a vertex,
Bellman goes through each edge in every iteration.
• Bellman-Ford Algorithm is not as efficient as Dijkstra’s Algorithm, but it
can work on negative weighted edges!
Routing Problem in Mazes

The corresponding
A 8x8 original maze The simplified graph
graph representation

DC Liaw, CC Kuo, HT Lee, 'A Study of Point-to-Point Routing Problem in Mazes', International Journal of Engineering and Applied Sciences (IJEAS)
ISSN: 2394-3661, Volume-6, Issue-7, July 2019.
All-Pairs Shortest-Paths Algorithms

◘ Suppose that we wish to


– compute the shortest driving time between all pairs of n cities,
– find transitive closure of directed graphs,
– finding the path with the maximal flow between two vertices,
– testing whether an undirected graph is bipartite.
◘ The Floyd-Warshall Algorithm is mostly used to determine
the shortest route between every pair of vertices in the
graph.
Floyd-Warshall Algorithm

◘ Floyd-Warshall algorithm computes the cost matrix of the


shortest paths between all pairs of vertices of a directed
graph G = (V, E) in O(|V |3) time and O(|V |2) space.
◘ The natural representation for a graph in the Floyd-
Warshall algorithm is an adjacency matrix.
◘ The matrix C[i, j] holds the cost of edge (i, j).
◘ For the algorithm to work correctly, it is important that
there are no negative cost cycles in the graph.
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Input: A directed graph G = (V, E), where V = {1, 2, … , n}, and a cost matrix C[i; j].
Output: D[1..n, 1..n] where D[i, j] is the cost of the shortest path from i to j, and
S[1..n, 1..n] where S[i, j] is the intermediate vertex between i and j.

procedure FloydWarshall(G) {
for i := 1 to n do
for j := 1 to n do
D[i, j] := C[i, j];
S[i ,j] := 0
for i := 1 to n do
D[i; i] := 0;
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
if D[i, k] + D[k, j] < D[i, j] then
S[i, j] := k;
D[i, j] := D[i, k] + D[k, j];
}
Example

Initial Step:
Example

Step-1:
Example

Step-2:
Example

Step-3:
Example

Step-4:
Example

◘ From matrices D(4) and S(4), we can determine the shortest route between every
pair of nodes.
(4)
◘ The shortest distance from node 1 to node 4 is 𝑑14 ═ 1 unit.
(4)
𝑠14 ═ 0 indicates that there is no intermediate node between 1 and 4.
(4)
◘ The shortest distance from node 2 to node 4 is 𝑑24 ═ 6 unit.
(4) (4) (4)
𝑠24 ═ 3 yields the route 2–3–4. 𝑠23 ═ 0, 𝑠34 ═ 1 yields the route 3–1–4,
(4) (4)
𝑠31 ═ 0, 𝑠14 ═ 0. The combined result gives the shortest path 2–3–1–4.
Transitive Closure

◘ In some problems we may just want to know whether there exists a path
from vertex i to vertex j of length one or more in a graph G = (V, E). We
call this the problem of computing the transitive closure of G.
◘ Given a directed graph G = (V, E) with adjacency matrix A, we compute
boolean matrix T (the transitive closure of the adjacency matrix A) such
that T[i, j] is 1 if there is a path from i to j of length 1 or more, and 0
otherwise.
◘ The transitive-closure algorithm is similar to the Floyd-Warshall algorithm
except that it uses the boolean operation and to conclude that if there is
a path from i to k and one from k to j, then there is a path from i to j.
Questions?

Thank you for listening!

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