Top Banner
Shortest Path Algorithms
26

Shortest Path Algorithms. Definitions Variants Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Jan 13, 2016

Download

Documents

Polly Davidson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Shortest Path Algorithms

Page 2: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Definitions In a shortest path problem, we are given

a weighted, directed graph with weight function mapping edges to real-valued weights.

A path of length from a vertex to a vertex is a sequence such that , , and for

Page 3: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Definitions The length of a path is the number of

edges in the path. The path contains the vertices and the

edges The weight of path is the sum of the

weights of its constituent edges:

Page 4: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Definitions We define the shortest-path from to as

the path with the minimal weight from to .

If there is no path from to , the weight of the shortest path is defined to be .

Page 5: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Variants Single-source shortest-paths

problem: Given a graph, finding a shortest path from a given source vertex to all the vertices in the graph (Our problem)

Single-destination shortest-paths problem: If you reverse the directions of all edges, you can solve it as a single-source problem.

Page 6: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Variants Single-pair shortest-path problem:

Find a shortest-path from a given vertex to another vertex . If we solve the single-source problem with source vertex , we solve this problem as well. (No better algorithm for this problem specifically has been developed)

Page 7: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Variants All-pairs shortest-path problems:

Find a shortest part from each vertex to each vertex. Although this problem can be solved by running a single-source algorithm once from each vertex, it can usually be solved faster.

Page 8: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Structure of a Shortest Path Subpaths of shortest paths are shortest

paths. (WHY?) Negative-weight edges? Cycles? Negative-weight cycles?

Page 9: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Representing Shortest Paths Given a graph we maintain for each

vertex a predecessor that is either another vertex or NIL.

Most shortest path algorithms set the values so that the chain of predecessors originating at a vertex runs backwards along a shortest path from to . (Here we are looking for a shortest path originating from )

Page 10: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

InitializationInput: Vertex Set Source Vertex Output: Distance to Source Vector , Predecessor Vector INITIALIZE-SINGLE-SOURCE (V,s)1 for each vertex {2 3 }4

Page 11: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Relaxation Relaxing an edge is testing whether we

can improve the shortest path to found so far by going through and, if so, updating and .

A relaxation step may decrease the value of the distance to , and update its predecessor .

Page 12: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

RelaxationInput: Output: Distance to Source Vector , Predecessor Vector

RELAX ()1 If {2 3 }

Page 13: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

The Bellman-Ford Algorithm The Bellman-Ford Algorithm solves the

single-source shortest-path problems in which the edge weights may be negative.

It also alerts us if there is a negative-weight cycle that is reachable from the source.

If there is no such cycle, the algorithm produces the shortest paths and their weights.

Page 14: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Bellman-Ford1 INITIALIZE-SINGLE-SOURCE 2 to -1 {3 each edge {4 RELAX } }5 each edge 6 if {7 return FALSE }8 Return TRUE

Page 15: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Example and Implementation Issues

Page 16: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Correctness Lemma:Let be a weighted, directed graph with source and weight function , and assume that contains no negative-cycles that are reachable from . Then, after -1 iterations of the for loop of lines 2-4 of BELLMAN-FORD, is the length of the shortest-path from to for each vertex reachable from .

Page 17: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Computational Complexity

O (|𝑉||𝐸|)

Page 18: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Dijkstra’s Algorithm Dijkstra’s algorithm solves the single-

source shortest-path problem on a weighted, directed graph in with nonnegative edge weights.

for each edge With a good implementation the running

time of Dijkstra’s algorithm is lower than that of Bellman-Ford algorithm.

Page 19: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Dijkstra’s Algorithm Dijkstra’s Algorithm maintains a set of

vertices whose final shortest-path distance from the source s have already been determined.

The algorithm repeatedly selects the vertex with the minimum shortest-path estimate, adds to , and relaxes all edges leaving .

Page 20: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

D1 INITIALIZE-SINGLE-SOURCE 2 3 4 while {567 for each vertex {8 RELAX } }

Page 21: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Example

Page 22: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Implementation Issues

Page 23: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Approach Because Dijkstra’s algorithm always

chooses the closest vertex in to add to the set , we say that it uses a “greedy” strategy.

Not all greedy strategies yield optimal results.

But in this case it does

Page 24: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Correctness Theorem: Dijkstra’s algorithm, run on a

weighted, directed graph with a nonnegative weight function terminates with a distance vector that consists of the shortest distances from the source to each vertex.

Page 25: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Proof: We use the following loop invariant: At the start of each iteration of the

while loop of lines 4-8, distance vector has the shortest distances for each vertex in . Initialization: trivially true Maintenance: Termination:

Page 26: Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.

Computational Complexity The computational complexity of

Dijkstra’s algorithm depends on how is implemented.

By a Fibonacci heap implementation it is possible to obtain a running time of