Single-Source Shortest Paths - Lecture Notes | CS 231, Study notes of Algorithms and Programming

Material Type: Notes; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Fall 2001;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-u1b
koofers-user-u1b 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS231 Algorithms Handout # 37’
Prof. Lyn Turbak December 13, 2001
Wellesley College
Single-Source Shortest Paths
Note: This is a revised version of Handout #37, which was originally handed out on December 5.
Reading: CLRS Section 22.2, Chapter 24 CLR Section 23.2, Chapter 25
Weighted Graphs and Paths
Aweighted graph is a graph (V,E) together with a weighting function w:EReal .
In a weighted, directed graph (G, w) the weight of a path p=[v0,v
1,... ,v
k]isPk
i=1 w(vi1,v
i).
The shortest-path weight from ato bis δ(a, b)=min{w(p)|ppat hs (a, b)}.
Note that min {} =.
Ashortest path from ato bis any path psuch that w(p)=δ(a, b). (It may not be unique.)
Single-Source Shortest Paths Problem
The single-source shortest path problem: given a weighted directed graph ((V,E),w)and
a source vertex sin V, find a shortest path from sto every vertex of V.
Notes:
If a path has negative weight edges in a cycle, then a shortest path is not defined. Some
algorithms (like the Dijkstra algorithm we will study) assume non-negative weights. Other
algorithms (such as the Bellman-Ford algorithm, which we will not study) can handled neg-
ative weight edges as long as they don’t appear in cycles.
The problem of finding the shortest path between two particular vertices (the single-pair
shortest path problem) may seem easier that the single-source shortest path problem, but
no solution for the single-pair problem is known that is asymptotically faster than a solution
for the single-source problem!
The all-pairs shortest path problem finds the shortest path between every pair of vertices.
We shall not study this problem this semester; see CLRS Chapter 25/CLR Chapter 26 for
details.
1
pf3
pf4
pf5

Partial preview of the text

Download Single-Source Shortest Paths - Lecture Notes | CS 231 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS231 Algorithms Handout # 37’ Prof. Lyn Turbak December 13, 2001 Wellesley College

Single-Source Shortest Paths

Note: This is a revised version of Handout #37, which was originally handed out on December 5.

Reading: CLRS Section 22.2, Chapter 24 CLR Section 23.2, Chapter 25

Weighted Graphs and Paths

A weighted graph is a graph (V, E) together with a weighting function w : E → Real.

In a weighted, directed graph (G, w) the weight of a path p = [v 0 , v 1 ,... , vk] is

∑k i=1 w(vi−^1 , vi).

The shortest-path weight from a to b is δ(a, b) = min{w(p)|p ∈ paths(a, b)}. Note that min {} = ∞.

A shortest path from a to b is any path p such that w(p) = δ(a, b). (It may not be unique.)

Single-Source Shortest Paths Problem

The single-source shortest path problem: given a weighted directed graph ((V, E), w) and a source vertex s in V , find a shortest path from s to every vertex of V.

Notes:

  • If a path has negative weight edges in a cycle, then a shortest path is not defined. Some algorithms (like the Dijkstra algorithm we will study) assume non-negative weights. Other algorithms (such as the Bellman-Ford algorithm, which we will not study) can handled neg- ative weight edges as long as they don’t appear in cycles.
  • The problem of finding the shortest path between two particular vertices (the single-pair shortest path problem) may seem easier that the single-source shortest path problem, but no solution for the single-pair problem is known that is asymptotically faster than a solution for the single-source problem!
  • The all-pairs shortest path problem finds the shortest path between every pair of vertices. We shall not study this problem this semester; see CLRS Chapter 25/CLR Chapter 26 for details.

Breadth First Search

In the simple case where all weights = 1, the single source shortest path problem is known as breadth-first search.

Idea: Expand a ”frontier” outward from the source node s, level by level. A queue (FIFO queue, not priority queue) is used to manage the order in which the nodes are processed. The algorithm uses the following fields:

  • color[v] maintains the current color of the node, which is one of:
    • white = unexplored;
    • gray = frontier node = discovered node whose edges have not been processed
    • black = fully processed = discovered node directly connected; only to other discovered nodes.
  • ds[v] maintains the length of the shortest discovered path from source node s to v.
  • parent[v] maintains the parent of node v in the breadth-first tree rooted at source node s.

BFS(G,s) B Initialization for v in vertices(G) do color[v] ← white B All nodes originally unexplored ds[v] ← ∞ parent[v] ← nil color[s] ← gray ds[s] ← 0 Q ← Enq(s,Empty-Queue) B Loop invariants: B (1) Q contains only gray nodes. B (2) ds[v] is shortest path length from s to v for every non-white v. B (3) (parent[v],v) is an edge in the breadth first tree rooted at s for every non-white v. while not Empty-Queue?(Q) do f ← Deq(Q) B Next frontier node to process. for g in Adj[f] do if color[g] = white then color[g] ← gray ds[g] ← ds[f] + 1 parent[g] ← f Enq(g, Q) color[f] ← black B Color node black when completely processed.

Note that BFS is similar to DFS-Visit except that it uses a FIFO queue rather than a stack to process vertices. Analysis:

  • Each vertex enqueued and dequeued at most once at O(1) time per operation: O(V ).
  • Each edge scanned once: O(E).
  • Total = O(V + E) (= O(E) for a connected graph).

Relaxation

General shortest path algorithms maintain for each vertex v in the graph:

  • a shortest-path estimate ds[v] ≥ δ(s, v);
  • a shortest-path parent parent[v] such that ds[v] = ds[parent[v]] + w(parent[v], v).

The following is an initialization routine for a shortest path algorithm:

Initialize-Single-Source(G,s) for v ←_vertices(G) do ds[v] ← ∞ parent[v] ← nil ds[s] ← 0

Relaxation on edges (a, b) attempts to reduce the shortest-path estimate for b:

Relax((a, b), w) if (ds[a] + w(a,b)) < ds[b] then ds[b] ← (ds[a] + w(a,b)) parent[b] ← a

Shortest path algorithms work by initializing ds[v] as above and then repeatedly relaxing edges until ds[v] = δ(s, v). A shortest-path tree rooted at the source s is induced by the parent fields.

PQ-Decrease-Key

Sometimes it is necessary to modify the key of an element stored in a priority queue. This modi- fication can change the priority of the element relative to the priority of the other elements in the priority queue.

In particular, Dijkstra’s algorithm maintains a priority queue of vertices v ordered from low to high by their estimated distances ds[v] from a source vertex s. The relaxation process described above can sometimes decrease this estimated distance for a vertex, increasing its priority relative to other vertices.

For this purpose, we assume an operation PQ-Decrease-Key(Q, e, knew). Let kold be the current value of the key of element e in a priority queue Q where lower values have higher priority. If knew ≤ kold, then PQ-Decrease-Key decreases the key of element e from kold to knew; otherwise, the key of e remains unchanged.

  • In a complete heap, PQ-Decrease-Key can be implemented by applying a Bubble-Up opera- tion on e in time O(lg(n)).
  • In a leftist heap, PQ-Decrease-Key can be implemented by deleting the element e from the heap and reinserting it, which costs O(lg(n)) time.
  • There is a kind of heap we have not studied — Fibonacci heaps – in which PQ-Insert and PQ-Delete-Min take O(lg(n)) amortized time, but PQ-Decrease-Key takes only O(1) amortized time.

Dijkstra’s Algorithm

Dijkstra’s algorithm solves the single-source shortest path problem in the case where there are no negative weights.

Idea: Grow a shortest-path tree from the source vertex s. Every vertex v maintains a shortest-path estimate ds[v] and parent field that indicates the final edge of a path with this estimate. At each step, add the vertex vmin with the smallest shortest-path estimate to the tree via the edge to its parent. A priority queue can be used to manage extracting the node with the smallest shortest-path estimate. The algorithm works because the lack of negative edges guarantees than any path from s to vmin that passes through other vertices not yet in the shortest-path tree must have a weight greater than that of the path from s to vmin via parent[v].

This algorithm is greedy in the sense that, at each step, it adds the “best” node (node with smallest shortest-path estimate) to the growing shortest-path tree.

Dijkstra(G, w, s) Initialize-Single-Source(G,s) shortest ← {} B vertices at which ds[v] = δ(s, v) PQ ← Build-PQ (vertices[G]) B Loop Invariants: B (1) Q contains (V - shortest) ordered by ds[v]. B (2) ds[v] = δ(s, v) for every v in shortest. B (3) parent[v] is either nil or in shortest. B (4) ds[v] = ds[parent[v]] + w(parent[v],v) for all v with non-nil parent. while not PQ-Empty?(Q) do a ← PQ-Delete-Min(Q) shortest ← shortest ∪ {a} for b in Adj[a] do Relax((a,b), w) PQ-Decrease-Key(Q, b, ds[b])

Analysis:

  • Build-PQ called once on |V | elements
  • PQ-Delete-Min called |V | times
  • Relax and PQ-Decrease-Key called |E| times.

Priority Queue Implementation

Build-PQ |V |·PQ-Extract-Min |E|·PQ-Decrease-Key Total

Unsorted array/list O(V ) O(V 2 ) O(E) O(V 2 ) Complete/leftist heap O(V ) O(V · lg(V )) O(E · lg(V )) O(E · lg(V )) Fibonacci heap O(V ) O(V · lg(V )) O(E) O(V · lg(V ) + E)

The O(E · lg(V )) time for complete/leftist heaps assumes that O(V ) ≤ O(E), which is true for connected graphs and even for most unconnected graphs. Note that a final distance of ∞ indicates a vertex that is in a different connected component from the source vertex s.