shortest paths in graphs - ist.tugraz.at

21
Birgit Vogtenhuber Shortest Paths 1 Shortest Paths in Graphs Design & Analysis of Algorithms WS 2019/20 Birgit Vogtenhuber

Upload: others

Post on 17-Apr-2022

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths1

Shortest Paths in Graphs

Design & Analysis of Algorithms WS 2019/20

Birgit Vogtenhuber

Page 2: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths2

Outline

• Problem Definition

• The Algorithm of Dijkstra

• The Algorithm of Floyd and Warshall

Page 3: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths3 iv

Problem Definition

Many algorithms on graphs are based on the calculation of‘distances’ between vertices (examples: driving directionsin road networks, number of state transitions betweendifferent states of a system).

Distance d(u, v) from u ∈ V to v ∈ V in a connectedgraph G = (V,E):length of the shortest path from u to v.• G unweighted: number of edges• G weighted: sum of edge weights

Graph G can be directed or undirected (or mixed).

Goal: Compute distances between all pairs of vertices in G.

Page 4: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths4 xi

Unweighted Graphs

Using breadth-first search, the distance-matrix for agraph G with n vertices and m edges can be computed inΘ(n ·m) time and Θ(n2) space.

Question: How can one compute the distances betweenall pairs of vertices in a connected unweighted graph G ?

Question: Does this also work for weighted graphs ?

12

5u

v

12

Idea: “Adapt” BFS for shortest paths in weighted graphs.

Page 5: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths5 iii

Dijkstra’s Algorithm

Classic shortest path algorithm from Dijkstra [1959]:For a start vertex s, compute shortest paths from sto all v ∈ V (tree structure + length).

Input: A graph G = (V,E,w) with non-negative edgeweights w(u, v) and a vertex s ∈ V .

Output: The distances d(s, v) in G from s to all verticesv ∈ V and the tree with the according shortest paths.

Question: Why do shortest paths from s to all othervertices form a tree?

Page 6: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths5 iv

Dijkstra’s Algorithm

Generic step: Given set T of vertices where for all v ∈ T ,d(s, v) is already computed. Choose a vertex u ∈ V \ Twhose shortest path from s “found so far” is minimal.

⇒ For each vertex v, maintain:⇒ L(v) = length of the shortest⇒ path from s to v “found so far”,⇒ pre(v) = neighbor of v in T⇒ via which this shortest path goes.⇒ (compare to Prim’s MST algorithm).

Classic shortest path algorithm from Dijkstra [1959]:For a start vertex s, compute shortest paths from sto all v ∈ V (tree structure + length).

u

T

sQ

Page 7: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths5 vi

Dijkstra’s Algorithm

L(v) =

d(s, v) if v ∈ T∞ if v is not adjacent to Tshortest path froms to v via T

if v /∈ T , v adjacent to T

Classic shortest path algorithm from Dijkstra [1959]:For a start vertex s, compute shortest paths from sto all v ∈ V (tree structure + length).

u

T

sQ

A priority queue Q containsall vertices that are not yet in T ,organized by their L-values(for example a min-heap;initially contains all vertices).

Page 8: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths6

Dijkstra’s Algorithm

for all v ∈ V do L(v) =∞ odL(s) = 0; pre(s) = nilQ = Vwhile Q 6= 0 dou = MIN(Q)remove u from Q // reorganize Qfor all v ∈ A(u) do // A: adjacency list of G

if L(v) > L(u) + w(u, v) thenL(v) = L(u) + w(u, v) // reorganize Qpre(v) = u

fiod

od

Page 9: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths7 v

Dijkstra’s Algorithm

Runtime analysis for graph with n vertices and m edges:

• Min-heap with n elements:◦ Θ(n) time for initialization Q = V .◦ O(log n) time for removal of the minimum.◦ O(log n) time per updates L-value.

• Processing vertex u with deg(u) neighbors:removal of u from Q plus O(deg(u)) updated L-values.

⇒ Runtime in total for start vertex s:O(n) +O(

∑u∈V (1 + deg(u)) log n)

= O((n+m) log n)) = O(m log n),since the graph is connected.

⇒ Computation of distance matrix in O(nm log n) time.

Page 10: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths7 vii

Dijkstra’s Algorithm

Memory analysis for graph with n vertices and m edges:

• Θ(n+m) = Θ(m) for G,

• Θ(n) for Q,

• Θ(n) for tree T ,

• Θ(n) for lengths L,

• Θ(n2) for distance matrix.

⇒ Θ(m) for shortes path tree and distances from s,

Θ(n2) for computing the whole distance matrix.

Page 11: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths8 iv

Dijkstra’s Algorithm

Correctness. We will show:

1. For all v ∈ T we have L(v) = d(s, v).

2. For each v /∈ T , L(v) is the length of the shortest pathfrom s to v in G that goes only through vertices of T .(or L(v) =∞ if such a path does not exist).

Proof. We use induction on |T |.Induction base: after the first pass, we haveL(s)=d(s, s)=0, L(v)=w(s, v) for all v ∈ A(s),L(v)=∞ for all v 6∈ A(s), and T ={s}.⇒ Conditions 1. and 2. are fulfilled.

Induction step: u is added to T (and removed from Q).

Page 12: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths9 vii

Dijkstra’s Algorithm

• Assume for a contradiction that L(u) > d(s, u)(L(u) < d(s, u) is impossible) and let π be ashortest path from s to u.

⇒ Since L(u) measures the shortest pathfrom s to u via vertices of T ,the path π has vertices outside T .

• Let x be the first vertexon π with x 6∈ T .

⇒ The path from s to x along πis the shortest path from s to x(optimality of partial paths)and goes only via vertices in T .

⇒ L(x) = d(s, x) because of Condition 2.

x

T

u

π

=L(x)

s

d(s, x)

Page 13: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths9 xii

Dijkstra’s Algorithm

• L(x) < L(u) because d(s, x) ≤ d(s, u) < L(u).

• As both u and x are in Q,this is a contradiction toL(u) = minv∈Q{L(v)}.

⇒ L(u) = d(s, u) and henceCondition 1. is maintainedwhen adding u to T .

• Condition 2. is also maintained:When u comes to T , L(v) canonly decrease for v ∈ A(u).

⇒ Dijkstra’s algorithm computesthe distances from s to all other vertices.

x

T

u

π

=L(x)

s

d(s, x)

Page 14: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths10 iv

Dijkstra’s Algorithm

Remarks:

• Note the similarity of Dijkstra’s algorithm with thealgorithm of Prim for computing a minimum spanningtree: only the computation of the priorities (p or L) isdifferent.

• For dense graphs (m = Θ(n2)) the algorithm needsΘ(n3 log n) time to compute the distance matrix.

• If an unsorted list is used for the queue Q, a runtime ofO(

∑v∈V v ∈ V (n+ deg(v)·1)) = O(n2 +m) = O(n2)

for start vertex s and O(n3) for the distance matrix isobtained (independent of m) ⇒ good for dense graphs,bad for sparse graphs (m = Θ(n)), works also for Prim.

Page 15: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths11 v

Dijkstra’s Algorithm

• The algorithm of Dijkstra does in general not work ifsome of the edge weights are negative.

• If the graph has a (possibly trivial) cycle with negativelength then it’s not clear what “shortest path” means(no finite solution minimizes the distance).

• The Bellman-Ford algorithm [1955-1958] can be usedfor graphs with negative edge weights.If a cycle with negative weight can be reached from s,it returns an error. Otherwise the distances from s anda shortest path tree are computed in O(n ·m) time.

Remarks:

Page 16: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths12 iii

The Floyd-Warshall Algorithm

In the Floyd-Warshall algorithm [1962], the distancematrix is calculated directly. The underlying observationsare similar to those in dynamic programming.

Consider a weighted graph G = (V,E,w), V = v1, ..., vn,with non-negative edge weights, and a weight matrixw(i, j), 1 ≤ i, j ≤ n, defined by

w(i, j) =

w(vi, vj) if (vi, vj) ∈ E0 if i = j∞ otherwise

We compute a sequence of matrices w1, ..., wn from wwith wk(i, j) = min{wk−1(i, j), wk−1(i, k) + wk−1(k, j)}and w0 = w.

Page 17: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths13 iv

The Floyd-Warshall Algorithm

Proof. We show by induction on k that wk(i, j) is thelength of the shortest path from vi to vj via {v1, ..., vk}.Induction base: For k = 0 the statement is true:

• if vivj ∈ E then w0(i, j) = w(vi, vj);

• if vivj 6∈ E then w0(i, j) =∞;

• w0(i, i) = 0.

In all cases, w0(i, j) is the shortest path from vi to vjwithout intermediate vertices.

Claim: wn(i, j) is the distance from vi to vj in G.

Induction step: Assume the statement is correctup to k − 1 and consider wk.

Page 18: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths13 ix

The Floyd-Warshall Algorithm

Observation: The shortest path π from vi to vj viavertices from {v1, ..., vk} may or may not contain vk.

• If π contains vk, then the parts of π from vi to vkand from vk to vj go only via {v1, ..., vk−1}.

⇒ By induction, the lengthsof those parts are stored inwk−1(i, k) and wk−1(k, j).

⇒ Hence the length of π iswk−1(i, k)+wk−1(k, j).

vk

vj

vi

{v1, ..., vk}

from {v1, ..., vk−1}

Page 19: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths13 xiii

The Floyd-Warshall Algorithm

• If π does not contain vk then π goes via {v1, ..., vk−1}.⇒ By induction, the length of π is stored in wk−1(i, j).

• The algorithm takes the minimum of the two consideredpossibilities ⇒ wk(i, j) is the length of π in both cases.

⇒ wn(i, j) is the length of theshortest path from vi to vjthan can go via all verticesof V and hencewn(i, j) = d(vi, vj).

vk

vj

vi

{v1, ..., vk}

from {v1, ..., vk−1}

Page 20: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths14 ii

The Floyd-Warshall Algorithm

w0 = wfor k = 1 to n do

for i = 1 to n dofor j = 1 to n dowk(i, j) = min{wk−1(i, j), wk−1(i, k) + wk−1(k, j)}

odod

od

Requirements for G with n vertices and m edges:

• Runtime: Θ(n3)

• Memory: Θ(n2)

Pseudocode:

Page 21: Shortest Paths in Graphs - ist.tugraz.at

Birgit Vogtenhuber Shortest Paths15 iv

The Floyd-Warshall Algorithm

Remarks:

• The Floyd-Warshall algorithm also works if the graph isdisconnected (if not every vertex can be reached fromevery other vertex). The distance between suchvertices is set to ∞ in the matrix wn.

• The Floyd-Warshall algorithm can also be used forgraphs with negative edge weights. Then an additionalcheck for the existence of (possibly trivial) cycles withnegative length is needed. A graph has a (possiblytrivial) cycle with negative length if and only if thematrix wn contains negative entries in its diagonal.