Lecture I: Shortest Path Algorithms - Computer Science

Background
Shortest Path Algorithms
Setting:
Lecture I: Shortest Path Algorithms
Dr Kieran T. Herley
Department of Computer Science
University College Cork
directed graph,
real edge “weights”
October 2016
Let the length of a path be the sum of its edge weights and let
δ(s, u) = length of shortest path from s to u
Reduces to edge distance if all weights are 1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
1/1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
Background
October 2016
2/1
Floyd-Warshall Algorithm
Example
Graphs and Matrices
Single source shortest path problem Given a weighted graph G and a
designated node s ∈ G , determine δ(s, u) for each u ∈ G .
u δ(s, u)
s
0
a
8
b
9
c
7
d
5
All pairs shortest path problem Given a weighted graph G , determine
δ(x, y ) for each x, y ∈ G .
A Graph and its matrix representation (nodes listed alpahetically
a = 1 etc.)
0 3
8 ∞ −4
∞ 0 ∞ 1
7
∞ 4
0 ∞ ∞
2 ∞ −5 0 ∞
∞ ∞ ∞ 6
0
wi,j

if i = j
 0
weight(i, j) if i 6= j and (i, j) ∈ E
=

∞
if i =
6 j and (i, j) 6∈ E
No negative cycles allowed– otherwise shortest path notion not well
defined
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
3/1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
4/1
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Some Terminology
A useful identity
k-path–
path all of whose intermediate nodes are numbered less than or equal
to k;
(the start/end nodes may have numbers greater than k).
Definition
Consider shortest k-path from i to j :
(k)
`i,j = length of shortest k-path joining i to j.
either it passes through node k
(once): 2- i.e. consists of
Claim
β = shortest (k − 1)-path
from i to k
node k
γ = shortest (k − 1)-path
from k to j
(
(k)
`i,j
=
wi,j
if k = 0
(k−1) (k−1)
(k−1)
min{`i,j , `i,k + `k,j } if k > 0
or it does not pass through node
k: 3 α = shortest (k − 1)-path
from i to j
Best path is π is better of α or β · k · γ
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
5/1
KH (21/10/16)
Floyd-Warshall Algorithm
Lecture I: Shortest Path Algorithms
October 2016
6/1
Floyd-Warshall Algorithm
Observation
Floyd-Warshall Algorithm
(k)
Visualize the set of `i,j quantities as three-dimensional grid with
Algorithm FLOYD−WARSHALL(G):
W = matrix of edge weights
for i ←1 to n do
for j ←1 to n do
L[ i , j , 0] ←W[i, j ]
(k)
“point” `i,j having “co-ordinates” (i, j, k).
(
(k)
`i,j
=
Notice:
KH
wi,j
if k = 0
(k−1) (k−1)
(k−1)
min{`i,j , `i,k + `k,j } if k > 0
(k)
each horizontal “slice” represents values `i,j for some fixed k
the quantities in the bottom slice (k = 0) depend only on edge weights
(k)
(21/10/16)
I: Shortest
Path Algorithms
October
each quantity `i,j Lecture
on slice
k depends
only on quantities on
the2016
slice 7 / 1
below
for k ←1 to n do
for i ←1 to n do
for j ←1 to n do
if L[ i , j , k−1] < L[i, k, k−1] + L[k, j , k−1]
L[ i , j , k] ←L[i , j , k−1]
else
L[ i , j , k] ←L[i , k, k−1] + L[k, j , k−1]
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
8/1
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Notes
Transitive Closure
The transitive closure of G = (V , E ) the the graph G ∗ = (v , E ∗ ) such
that there is an edge in G ∗ from u ti v if and only if there is a path in
G from u to v .
Algorithm:
Length of shortest i-to-j path in L[i, j, n].
Negative weight OK, but not negative cycles.
Running time: O(n3 )
Give weight 1 to each edge in G ;
Run Floyd-Warshall algorithm;
Each L[i, j, n] 6= ∞ denotes path in G , so add corresponding edge (i, j)
to E ∗ .
Can get by with 2D arrays (two for alternate slices)
Can be modified to produce paths (not just lengths)
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
9/1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
Dijkstra’s Algorithm
Single-source shortest path problem (SSSP)
t
Path sequence of
edge-connected
vertices
x
3
6
s
4
9
7
1
2
5
4
3
y
Single source shortest path problem: Given a weighted graph G
and a designated node s ∈ G , determine δ(s, u) for each u ∈ G .
Could use Floyd-Warshall, but is there a more efficient way?
October 2016
z
Path length sum of edge
lengths
Shortest path path between
endpoints with
minimum total
length
Problem
(SSSP) Given graph G and source vertex s, calculate length of shortest
path from source s to each vertex in G.
Disallow negative edges
Lecture I: Shortest Path Algorithms
10 / 1
Dijkstra’s Algorithm
Single-Source Shortest Path Problem
KH (21/10/16)
October 2016
11 / 1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
12 / 1
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Dijkstra’s Algorithm
t
Idea
x
t
3
6
s
4
1
2
5
4
s
0
3
y
5
6
9
7
4
1
2
y
5
3
Begin with a crude “estimate” d[u] for δ(s, u):
0 if u = s
d[u] =
∞ otherwise
8
9
7
4
z
x
3
4
7
z
Refine estimates using (carefully chosen) sequence of the following
edge operations (i.e. (u, v ) must be an edge):
Inputs (left):
G : graph with nonnegative (important!) edges
s: “start” node
Outputs (right): Compute for each node v , δ(s, v ) = length shortest path
from s to v (∞ if none). Shown inside nodes.
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
13 / 1
Algorithm Relax(u, v):
if d[v] > d[u] + weight(u, v) then
d[v] ←d[u] + weight(u, v)
Ultimately (we hope) d[u] = δ(s, u), for all u.
KH (21/10/16)
Dijkstra’s Algorithm
October 2016
14 / 1
October 2016
16 / 1
Dijkstra’s Algorithm
Notes
Dijkstra’s Algorithm
Algorithm Relax(u, v):
if d[v] > d[u] + weight(u, v) then
d[v] ←d[u] + weight(u, v)
Observation
Through any sequence of Relax(u, v ) steps
Algorithm Dijkstra(G, s ):
Initialize d(s) to 0
Initialize d(x) = INFTY for all x 6= s
Create priority queue Q and place each
node x in Q with key value d(x)
d[x] is non-increasing, and
If d[x] 6= ∞, then there is a path of length d[x] in G from s to x.
Algorithm Relax(u, v ):
if d[v] > d[u] + weight(u, v) then
d[v] ←d[u] + weight(u, v)
KH (21/10/16)
Lecture I: Shortest Path Algorithms
Lecture I: Shortest Path Algorithms
October 2016
while Q is not empty do
u ←Q.remove min element()
for each v in G.neighbours(u) do
Relax(u, v)
15 / 1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Priority queue refresher
D’s algorithm in action
Q vertex
x
t
What Container abstraction holding key, value pairs
Operations
6
insert Add new key value item to container
min Return the value with the smallest key (ties
broken arb.)
remove min Return and remove the value with the smallest
key
Implementation Heap-ordered tree stored in array; insert, remove min
O(log n) time.
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
s
0
4
3
∞
56
7
1
∞
13
8
9
2
non-Q vertex
5
3
∞
4
4
∞
7
y
u vertex
z
Algorithm Dijkstra(G, s ):
. . .
while Q is not empty do
u ←Q.remove min element()
for each v in G.neighbours(u) do
Relax(u, v)
17 / 1
KH (21/10/16)
Dijkstra’s Algorithm
Lecture I: Shortest Path Algorithms
October 2016
18 / 1
Dijkstra’s Algorithm
Aside
Running time of D’s algorithm
Algorithm Relax(u, v ):
if d[v] > d[u] + weight(u, v) then
d[v] ←d[u] + weight(u, v)
Relax involves changes to d[] i.e. priority queue keys.
New priority queue operation:
reduce key(x, k) Replace key of item x with smaller key k
Implementation note: Effectively “helper method” heap decrease key
used in insert operation
Running time: O(log(queue size))
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
19 / 1
Algorithm Dijkstra(G, s ):
/∗ Initialization stuff ∗/
while Q is not empty do
u ←Q.remove min element()
for each v in G.neighbours(u) do
Relax(u, v)
KH (21/10/16)
n, m = num. nodes, edges
Priority queue operations:
O((n + m) log n) (over)
Non-priority queue stuff:
O(n + m)
Total running time:
O((n + m) log n)
Lecture I: Shortest Path Algorithms
October 2016
20 / 1
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Notes
Claim 1
Priority queue operations
op
no.
insert
n
isEmpty
n
remove min element n
replaceKey
m
Non-priority queue stuff
cost per
O(log n)
O(1)
O(log n)
O(log n)
subtotal
O(n log n)
O(n)
O(n log n)
O(m log n)
Definition
We say x is settled once d(x) = δ(s, x).
Claim
If u is settled and s → · · · → u → v is a shortest path to v , then following
Relax(u, v ) v is also settled
n iterations in all
for fixed u, for-loop takes O(#edges leaving u)
time
X
O(n) +
O(#edges leaving u) = O(n + m)
| {z }
u∈V
{z
}
while |
for
Before: Relax(u, v ), d(u) = δ(s, u)
After: Relax(u, v ), d(v ) ≤ d(u) + w (u, v )
This must equal δ(s, v ): shortest path assumption
Total
O((n + m) log n) + O(n + m) = O((n + m) log n)
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
21 / 1
KH (21/10/16)
Dijkstra’s Algorithm
Lecture I: Shortest Path Algorithms
October 2016
22 / 1
Dijkstra’s Algorithm
Claim 2
Claim 2 cont’d
Claim
Node u is settled before being removed from Q.
Claim
Node u is settled before being removed from Q.
d(u)
Proof.
Suppose this were not true
≤ d(y ) by
remove min elements choice
of u
= δ(s, y ) –
Let u be the first node removed from Q that was not settled
x not in Q so
d(x) = δ(s, x);
Relax(x, y ) operation
ensures that d(y ) = δ(s, y )
Let y be first node along shortest path from s to u that is in Q and x
be its predecessor
≤ δ(s, u) since s-to-y a prefix
of s-to-u and edge-weights are
non-negative
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
23 / 1
KH (21/10/16)
≤ d(u)
Lecture I: Shortest Path Algorithms
October 2016
Hence d(u) = δ(s, u)
24 / 1
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Constructing the Paths
SP Algorithms and Network Routing
Minor tweak to Relax to record shortest-path predecessors so algorithms
constructs paths not just their length.
Algorithm
if d[v]
d[v]
p[v]
Relax(u, v):
> d[u] + weight(u, v) then
←d[u] + weight(u, v)
←u
At conclusion, p(x) indicates parent of x in a (shortest-path) tree rooted
at s. The root to leaf path to x is a shortest path.
network of hosts/switches(nodes) and links (edges)
edge weight – “desirability” of using edge
Determine for/at each node, the lowest cost route to each destination
Would like approach to be
distributed
resilient to failures/changes
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
25 / 1
KH (21/10/16)
Dijkstra’s Algorithm
Lecture I: Shortest Path Algorithms
October 2016
26 / 1
Dijkstra’s Algorithm
Idea 1
Idea 2
Each node determines its neighbours/edges and costs and injects into
into network. (Re-injects following changes)
Nodes exchange this information among themselves
Each node x runs Dijkstra locally to establish best paths from x to
other network nodes
Maintains table of (dest., cost, next-hop) entries for routing decisions
Each node gets complete “map” of the network
Information re-computed when network map changes
Changes get reflected in maps as new info replaces old
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
27 / 1
KH (21/10/16)
Lecture I: Shortest Path Algorithms
October 2016
28 / 1