single-source shortest paths - university of missouri–st....

22
Single-Source Shortest Paths [for ‘directed’ weighted graphs] Course: CS 5130 - Advanced Data Structures and Algorithms Instructor: Dr. Badri Adhikari

Upload: others

Post on 19-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Single-Source Shortest Paths[for ‘directed’ weighted graphs]

Course: CS 5130 - Advanced Data Structures and Algorithms Instructor: Dr. Badri Adhikari

Page 2: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Announcement: Next upside-down class (Oct 11th)Implement Kruskal’s algorithm, and Dijkstra’s algorithm for a graph represented using adjacency-lists.

Test your implementation using various standard inputs.

Refer to ‘Practice Set 3’ for examples.

During the upside-down class you will be provided a new graph/graphs where you will apply your implementation to get an answer. Bring your laptops (configured and ready to code), notes, and, everything you need.

Page 3: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Shortest-paths problemIn a shortest-path problem, we are given a weighted, directed graph G = (V, E), with a weight function w:E→R mapping edges to real-valued weights.

The weight w(p) of a path p = <v0, v1, …, vk> is the sum of the weights of its constituent edges:

The shortest-path weight δ(u,v) from u to v by:

A shortest path from a vertex u to vertex v is then defined as path p with weight w(p) = δ(u,v)

Page 4: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

VariantsCan a single-source shortest-paths algorithm solve the following?

(a) Single-destination shortest-paths problem(b) All-pairs shortest paths problem

Page 5: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Optimal substructure of a shortest pathLemma: Given a weighted, directed graph G = (V, E) with a weight function w:E→R, let p = <v0, v1, …, vk> be a shortest path from v0 to vk and, for any i and j such that 0 ≤ i ≤ j ≤ k, let pij = <vi, vi+1, …, vj> be the shortest subpath of p from vertex vi to vertex vj. Then, pij is a shortest path from vi to vj.

Proof: Decompose p into p0i + pij + pjk. Then we have w(p) = w(p0i) + w(pij) + w(pjk).Let’s assume that pij is not the shortest path.In such a case we have a path p’ij such that w(p’ij) < w(pij).This leads to w’(p’0k) < w(p0k). This contradicts that p is a shortest path.

Page 6: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Negative-weight edgesSome edges can have negative weights.

If a graph has negative weights (for some edges) reachable from s, the shortest-path weight δ(s,v) remains well defined, even if it has negative value.

However, when a graph has negative-weight cycle reachable from s, the shortest-path weights are not well defined.

We can always find a path with lower weight by following the proposed “shortest” path and then traversing the negative-weight cycle. If there is a negative-weight cycle on some path from s to v, we define δ(s,v) = -∞.

Dijkstra’s algorithm assumes that all weights are nonnegative. Bellman-Ford algorithm allows negative weight edges.

Page 7: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Can a shortest path contain a cycle?It cannot contain a negative weight cycle.

Nor can it contain a positive weight cycle. Because we can get a shorter path by removing such a cycle.

This implies it can only contain a 0-weight cycle. But, we can remove a 0-weight cycle from any path and produce another path whose weight is same. We can repeatedly remove all 0-weight cycles.

Therefore, when we are finding shortest paths, we can assume that they have no cycles. Thus for a graph with |V| vertices, we restrict our attention to shortest paths of at most |V|-1 edges.

Page 8: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Representing shortest pathsWe often wish to compute not only shortest-path weights, but the vertices on shortest paths as well, i.e. we are interested in the predecessor subgraph G = (V , E ).

V = {v ∈ V : v. ≠ NIL} U {s}

E = {(v. , v) ∈ E : v ∈ V - {s}}

A shortest-paths tree rooted at s is a directed subgraph G’=(V’,E’), where V’⊆ V and E’⊆ E, such that:

1. V’ is the set of vertices reachable from s in G,2. G’ forms a rooted tree with root s, and3. For all v ∈ V’, the unique simple path from s to v in G’ is a

shortest path from s to v in G. Multiple shortest paths

Page 9: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Relaxation

For each vertex v, we maintain an attribute v.d, which is an upper bound on the weight of a shortest path from source s to v. We call v.d a shortest-path estimate.

The process of relaxing an edge consists of testing whether we can improve the shortest path to v found so far by going through u and if so, updating v.d and v. .

A relaxation step may decrease the value of the shortest-path estimate v.d and update v’s predecessor attribute .

Examples ofrelax(u,v,w)

Page 10: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Properties of shortest paths and relaxationTriangle inequality: For any edge (u,v) ∈ E, we have δ(s,v) ≤ δ(s,u)+w(u,v).Upper-bound property: We always have v.d ≥ δ(s,v) for all vertices v ∈ V, and once v.d achieves the value δ(s,v), it never changes.No-path property: If there is no path from s to v, then we always have v.d = δ(s,v) = ∞.Convergence property: If s ↝ u → v is a shortest path in G for some u,v ∈ V, and if u.d = δ(s,u) at any time prior to relaxing edge (u,v) then v.d = δ(s,v) at all times afterward.Path-relaxation property: If p = <v0, v1, v2, …, vk> is a shortest path from s = v0 to vk, and we relax the edges of p in the order (v0, v1), (v1,v2), …, (vk-1, vk), then vk.d = δ(s,vk). This property holds regardless of any other relaxation steps that occur, even if they are intermixed with relaxations of the edges of p.Predecessor-subgraph property: Once v.d = δ(s,v) for all v ∈ V, the predecessor subgraph is a shortest-paths tree rooted at s.

Page 11: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Bellman-Ford algorithm vs Dijkstra’s algorithmBFA● A general solution to the single-source shortest-paths problem● Remarkably simple● Edges can have negative weights● Detects whether a negative-weight cycle is reachable from the source

DA● Runs faster● No negative edge weights

Page 12: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Bellman-Ford algorithmMaintain v.d for each vertex, which estimates the shortest path from s to v.

The algorithm relaxes edges, progressively decreasing an estimate v.d until it achieves the actual shortest-path weight δ(s,v).

Finally, it returns a boolean value indicating whether or not there is a negative-weight cycle that is reachable from the source.

If such a negative-weight cycle exists, the algorithm indicates that no solution exists.

For an undirected graph, replace each edge with two directed edges.

Page 13: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Classwork/AssignmentA. In how many executions of the ‘outer for loop’

will the Bellman-Ford algorithm find the shortest-paths, if you consider edges(a) by non-decreasing weights?(b) by non-increasing weights?(c) in the following order: (t,x), (t,y), (t,z),

(x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)?B. What is the running time of the B-F

algorithm?C. Can we conclude that selecting edges by

non-increasing/non-decreasing weights will make the algorithm converge faster?

Page 14: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Single-source shortest paths in DAGWe can compute shortest paths from a single source in Θ(V+E) time for a weighted dag (directed acyclic graph).

Shortest paths are always defined in a dag, since no negative-weight cycles exist - negative-weight edges can be present.

At first topologically sort the dag to impose a linear ordering on the vertices. (If the dag contains a path from vertex u to v, then u precedes v in the topological sort).

Shortest path can be obtained by making just one pass over the vertices in the topologically sorted order.

Note: Bellman-Ford works on directed cyclic graphs as well, this algorithm is only for directed acyclic graphs (dag).

Page 15: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Single-source shortest paths in DAG

Classwork: Obtain shortest path with ‘r’ as the starting vertex.

Page 16: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Shortest path for DAG - An example applicationDetermining critical paths in PERT (program evaluation and review technique) chart analysis.

Edges represent jobs to be performed, and edge weights represent the times required to perform particular jobs.

If edge (u,v) enters vertex v and edge (v, x) leaves v, then job (u,v) must be performed before job (v,x).

A path through this directed acyclic graph (dag) represents a sequence of jobs that must be performed in a particular order.

A critical path is a longest path through the dag, corresponding to the longest time to perform any sequence of jobs.

The weight of a critical path provides a lower bound on the total time to perform all the jobs.

A critical path can be found by negating the edge weights and running DAG-SHORTEST-PATHS()

Buy ingredients (30 min)

Mix frosting (5 min)

...

A PERT chart

Corresponding DAG

Page 17: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

ClassworkFind the critical path:

A critical path can be found by negating the edge weights and running DAG-SHORTEST-PATHS()

1

2

Buy ingredients (30 min)

3

4 5

6

Mix frosting (5 min)Heat oven (15 min)

Mix batter(5 min)

Grease pan(1 min)

7

Bake cake (30 min)Frost

(10 min)8

TOPOLOGICAL-SORT(G)- Identify the vertices that have no

incoming edges (say I is the set)- For each vertex in I, call DFS(G) and

compute finish times v.f for each vertex- As each vertex’s finish time is

computed, insert it onto the front of a linked list

- Return the linked list of vertices

Page 18: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Dijkstra’s algorithmDijkstra’s algorithm solves the single-source shortest-paths problem on a weighted, directed graph G = (V, E) for the case in which all edge weights are nonnegative.

The algorithm maintains a set S of vertices whose final shortest-path weights from the source s have already been determined. (Just like the Prim’s algorithm.)

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

It uses a min-priority queue Q of vertices, keyed by their d values.

It runs like the breadth-first algorithm (explores breadth first).

Page 19: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Dijkstra’s algorithm

Page 20: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Classwork

Page 21: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

Running time of Dijkstra’s algorithmO(V * time for EXTRACT-MIN)

= O(V * lgV) if priority queue implemented using Fibonacci heap.

= O(V * V) if priority queue implemented using simple array.

Total running time = O(V lgV + E)

Page 22: Single-Source Shortest Paths - University of Missouri–St. Louisumsl.edu/~adhikarib/cs4130-fall2017/slides/07 - Single... · 2017. 12. 6. · Single-source shortest paths in DAG

SummaryThe Bellman-Ford algorithm works on directed cyclic and acyclic graphs and on graphs that have negative weight cycles.

The algorithm for shortest path in a DAG (that uses topological sorting) works only for acyclic graphs. Weights may be negative.

Dijkstra’s algorithm works on weighted directed graphs. There may be cycles in the graph but all the weights should be non-negative.