



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Fall 2001;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




CS231 Algorithms Handout # 37’ Prof. Lyn Turbak December 13, 2001 Wellesley College
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:
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:
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:
Relaxation
General shortest path algorithms maintain for each vertex v in the graph:
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.
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:
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.