40
CSE 2331 / 5331 CSE 2331/5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.

CSE 780: Design and Analysis of Algorithms

  • Upload
    tomai

  • View
    33

  • Download
    6

Embed Size (px)

DESCRIPTION

CSE 780: Design and Analysis of Algorithms. Lecture 12: Augmenting Data Structures. Balanced Binary Search Tree. Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor. Also support Select o peration ? . Augment Tree Structure. Select ( T, k ) Goal: - PowerPoint PPT Presentation

Citation preview

Page 1: CSE 780: Design and Analysis of Algorithms

CSE 2331 / 5331

CSE 2331/5331

Topic 12:Shortest Path

Basics

Dijkstra AlgorithmRelaxationBellman-Ford Alg.

Page 2: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Shortest Path

Given a graph G = (V, E) with weight function w : E --> R

Shortest path weight:

Page 3: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Various Problems

Single-source shortest-paths problem Given source node s to all nodes from V

Single-destination shortest-paths problem From all nodes in V to a destination u

Single-pair shortest-path problem Shortest path between u and v

All-pairs shortest-paths problem Shortest paths between all pairs of nodes

Page 4: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331 / 5331

Page 5: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Negative-weight Edges

Some edges may have negative weights

If there is a negative cycle reachable from s: Shortest path is no longer well-defined Example

Otherwise, it is fine

Page 6: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Cycles

Shortest path cannot have cycles inside Negative cycles : already eliminated Positive cycles: can be removed 0-weight cycles: can be removed

A path with no cycles inside is also called a simple path.

No-Cycle Theorem: Given any two vertices of graph, there exists a shortest path between them with no cycle.

Page 7: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Optimal Substructure Property

Proof Proof by contradiction. If there is a shorter path from to , then the path is a

shorter path from to Contradiciton.

Optimal Substructure Property (Theorem): If is a shortest path from to ,

then any sub-path is also a shortest path.

Page 8: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Shortest-paths Tree For every node v V, π[v] is the predecessor of v

in a shortest path from source s to v Nil if does not exist

A shortest-path tree Root is source s Edges are (π[v] , v)

The shortest path between s and v is the unique tree path from root s to v.

Page 9: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Example: directed graph

Page 10: CSE 780: Design and Analysis of Algorithms

Example: undirected graph

Key property: Given shortest path tree rooted at s ( in this example),

one can obtain the shortest path from s to every other vertex connected to it. CSE 2331 / 5331

Page 11: CSE 780: Design and Analysis of Algorithms

Shortest Path Tree Question:

Given a shortest path tree of G rooted at s, how fast can we report the shortest path distance from s to all other vertices in G?

CSE 2331 / 5331

Key property: Given shortest path tree rooted at s ( in this example),

one can obtain the shortest path from s to every other vertex connected to it.

O(V)

The output of shortest-path algorithms usually contains both shortest-path tree and distances.

Page 12: CSE 780: Design and Analysis of Algorithms

Shortest Path Tree Related to minimum spanning tree?

CSE 2331 / 5331

Page 13: CSE 780: Design and Analysis of Algorithms

CSE 780 Algorithms

Goal:

Input: a weighted graph G = (V, E), with positive weights! and a source node vs V

Output: For every vertex v V,

v.distance = (vs , v) v.parent =

Shortest-paths tree induced by v.parent

Page 14: CSE 780: Design and Analysis of Algorithms

Breadth-First Search

Recall BFS: Shortest path for unweighted graph (i.e, each edge has

weight 1).

Would the same idea work for weighted graph?

CSE 2331 / 5331

Page 15: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331 / 5331

We can no longer guarantee that at the time the algorithm first discovers a node, the distance is shortest.

BFS is only for shortest number of edges to reach a node!

How to guarantee that when we first reach a node, the distance is shortest?

Page 16: CSE 780: Design and Analysis of Algorithms

Dijkstra Algorithm

CSE 2331 / 5331

Page 17: CSE 780: Design and Analysis of Algorithms

Intuition: If Then is the shortest distance to reach through . Note, this does not have to be the same as shortest

distance to

CSE 2331 / 5331

Page 18: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331 / 5331

Page 19: CSE 780: Design and Analysis of Algorithms

Correctness

Invariance: At the beginning of the While-loop, all vertices already

discovered have correct shortest distance value. Prove that this invariance is maintained:

Base case: in the first iteration, only is discovered, and this invariance holds.

Inductive step: If the invariance holds at the beginning of the -th iteration, then it holds at the end of -th iteration (i.e, it holds at the beginning of -th iteration).

CSE 2331 / 5331

Page 20: CSE 780: Design and Analysis of Algorithms

Proof of Induction Step Let as identified in Line 5 of the algorithm.

Let P be the shortest path from to . is the weight of the path .

Proof that any other path from to has larger weight. Consider any other path from to . Let be the first edge in that connects a vertex from to a vertex

in . Argue that the weight of subpath from to is at least . Hence the weight of is larger than that of .

Done.

CSE 2331 / 5331

Page 21: CSE 780: Design and Analysis of Algorithms

CSE 2331 / 5331

Page 22: CSE 780: Design and Analysis of Algorithms

Why do we require that the weights are all positive?

Example.

CSE 2331 / 5331

Page 23: CSE 780: Design and Analysis of Algorithms

Running Time Analysis

CSE 2331 / 5331

Page 24: CSE 780: Design and Analysis of Algorithms

Running Time Analysis

Naïve implementation: Spend time to identify in Line 5. Total time:

How to identify more efficiently? First improvement: Storing cost at vertices.

.distance: current estimate of shortest path weight from to

CSE 2331 / 5331

Page 25: CSE 780: Design and Analysis of Algorithms

Storing Cost at Vertices

CSE 2331 / 5331

Page 26: CSE 780: Design and Analysis of Algorithms

Running Time Analysis

First improvement: Spend time to identify in Line 5. Total time: Note: the weight stored at is NOT the smallest weight

of edge connecting to any visited vertex That is how Prim’s MST algorithm works.

How to identify more efficiently? Second improvement: Use Priority Queue to store /

maintain vertex costs! .distance: current estimate of shortest path weight from to

CSE 2331 / 5331

Page 27: CSE 780: Design and Analysis of Algorithms

Second Improvement: Priority Queue

CSE 2331 / 5331

Page 28: CSE 780: Design and Analysis of Algorithms

Running Time Analysis Priority Queue: Q;

Total size: # Q.insert:

Total time: # Q.DeleteMin:

Total time: # Q.IsNotEmpty() and # Q.MinKey(): ,

Total time: # Q.DecreaseKey:

Total time:

CSE 2331 / 5331

Total time:

Page 29: CSE 780: Design and Analysis of Algorithms

Remarks Similar idea as breadth first search:

Greedy type of algorithm Guarantees that when we first discover a node, the

distance is the correct shortest path weight Similar to Prim’s Alg for MST:

But the cost at each vertex is defined differently. Also works for directed graphs But require weights to be positive

Can be improved to by using a better implementation of priority queue (Fibonacci heap)

CSE 2331 / 5331

Page 30: CSE 780: Design and Analysis of Algorithms

A more general algorithm

CSE 2331 / 5331

Page 31: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Shortest Path

Given a graph G = (V, E) with weight function w : E --> R

Shortest path weight:

Page 32: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Goal:

Input: directed weighted graph G = (V, E), source node s V

Output: For every vertex v V,

d[v] = (s, v) π[v]

Shortest-paths tree induced by π[v]

Page 33: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Previously

Dijkstra Algorithm: Compute shortest paths from a source node when the

input graph has no negative edge weights!

Now:

A single source shortest path algorithm for arbitrary edge weight!

Page 34: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Relaxation

Maintain shortest-path estimate d[v] for each node d[v]: initialize

Algorithms will repeatedly apply Relax.Differ in the order of Relax operation

Intuition:Do we have a shorter path

if use edge (u,v) ?

Page 35: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Shortest-paths Properties Triangle inequality

For (u, v) E, (s, v) ≤ (s, u) + w(u, v)

Equality holds when u is predecessor of v.

Upper-bound property If start with d[v] = and apply Relax operations Always have d[v] ≥ (s, v) Once d[v] = (s, v), never changes

Proof by contradiction

Page 36: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Properties cont.

Convergence property Suppose s u -> v is the shortest path from s to v Currently d[u] = (s, u) Relax (u,v) will set d[v] = (s, v)

Proof: follow from Relaxation defintion

Path-relaxation property Given a shortest path Apply Relax in order Then, afterwards,

Proof: Follow from above property

Page 37: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Bellman-Ford Algorithm Allow negative weights Follow the frame work that first, initialize:

Then apply a set of Relax compute d[v] and π[v] Return False if there exists negative cycles

Page 38: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Pseudo-code

Time complexity:O(VE)

Page 39: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Example

Page 40: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Correctness Why d[v] = (s, v) for every node v ?

Proof: Use path-relaxation property

Return True (or False) iff there is (or is no) negative cycles ? If there is no negative cycle: obvious

use triangle inequality If there is negative cycle:

Proof by contradiction