WEEKS 9 and 10
Graphs II
Unweighted Shortest Paths, Dijkstra’s Algorithm
Graphs with negative costs, Acyclic Graphs
CE222 – Data Structures & Algorithms II
Chapter 9.3
(based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006)
Shortest-Path Algorithms
• The input is a weighted graph: associated with
each edge (vi, vj) is a cost ci,j.
• The cost of a path v1v2...vN is ∑ci,i+1 for i in
[1..N-1] weighted path length
• The unweighted path length is merely the
number of edges on the path, namely, N-1.
CE 222-Data Structures & Algorithms II,
2
Izmir University of Economics
Shortest-Path Algorithms
Single-Source Shortest-Path Problem:
Given as input a weighted graph G=(V, E), and
a distinguished vertex, s, find the shortest
weighted path from s to every other vertex in
G.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 3
Negative Cost Cycles
• In the graph to the left, the shortest path from v1 to v6 has a
cost of 6 and the path itself is v1v4v7v6. The shortest
unweighted path has 2 edges.
• In the graph to the right, we have a negative cost. The path
from v5 to v4 has cost 1, but a shorter path exists by following
the loop v5v4v2v5v4 which has cost -5. This path is still not the
shortest, because we could stay in the loop arbitrarily long.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 4
Shortest Path Length: Problems
We will examine 4 algorithms to solve four versions of
the problem
1. Unweighted shortest path O(|E|+|V|)
2. Weighted shortest path without negative edges
O(|E|log|V|) using queues
3. Weighted shortest path with negative edges
O(|E| . |V|)
4. Weighted shortest path of acyclic graphs linear time
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 5
Unweighted Shortest Paths
• Using some vertex, s, which is an input parameter,
find the shortest path from s to all other vertices in
an unweighted graph. Assume s=v3.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 6
Unweighted Shortest Paths
• Algorithm: find vertices that are at distance 1,
2, ... N-1 by processing vertices in layers
(breadth-first search)
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 7
Unweighted Shortest Paths
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 8
Unweighted Shortest Paths-Implementation
For each vertex v
1. The distance from s in the entry dv
2. The bookkeeping variable (path) which
will allow us to print the actual path pv
3. A boolean variable to check is the vertex
is processed or not known class Vertex {
List adj;
bool known;
int dist;
Vertex path; }
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 9
Unweighted Shortest Paths
• Complexity O(|V|2)
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 10
Unweighted Shortest Paths - Improvement
CE 222-Data Structures & Algorithms II, Izmir 11
University of Economics
Unweighted Shortest Paths - Improvement
• At any point in time there
are only two types of
unknown vertices that
have dv≠∞. Some have dv =
currDist and the rest have
dv = currDist +1.
• We can make use of a
queue data structure.
• O(|E|+|V|)
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 12
Weighted Shortest Path Dijkstra’s
Algorithm
• With weighted shortest path,distance dv is tentative.
It turns out to be the shortest path length from s to v
using only known vertices as intermediates.
• Greedy algorithm: proceeds in stages doing the best
at each stage.
• Dijkstra’s algorithm selects a vertex v with smallest dv
among all unknown vertices and declares it known.
Remainder of the stage consists of updating the
values dw for all edges (v, w).
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 13
Dijkstra’s Algorithm - Example
►
►
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 14
Dijkstra’s Algorithm - Example
►
►
►
• A proof by contradiction will show that this
algorithm always works as long as no edge has
a negative cost.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 15
Dijkstra’s Algorithm Example – Stages I
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 16
Dijkstra’s Algorithm Example – Stages II
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 17
Dijkstra’s Algorithm - Pseudocode
• If the vertices are
sequentially scanned to
find minimum dv, each
phase will take O(|V|) to
find the minimum, thus
O(|V|2) over the course of
the algorithm.
• The time for updates is
constant and at most one
update per edge for a total
of O(|E|).
• Therefore the total time
spent is O(|V|2+|E|).
• If the graph is dense,
OPTIMAL.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 18
Dijkstra’s Algorithm-What if the graph is
sparse?
• If the graph is sparse |E|=θ(|V|), algorithm is too
slow. The distances of vertices need to be kept in a
priority queue.
• Selection of vertex with minimum distance via
deleteMin, and updates via decreaseKey operation.
Hence; O(|E|log|V|+|V|log|V|)
• find operations are not supported, so you need to be
able to maintain locations of di in the heap and
update them as they change.
• Alternative: insert w and dw with every update.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 19
Graphs with negative edge costs
• Dijkstra’s algorithm does not work with
negative edge costs. Once a vertex u is known,
it is possible that from some other unknown
vertex v, there is a path back to u that is very
negative.
• Algorithm: A combination of weighted and
unweighted algorithms. Forget about the
concept of known vertices.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 20
Graphs with negative edge costs - I
• O(|E|*|V|)
• Each vertex can dequeue at
most O(|V|) times.
(Why? Algorithm computes
shortest paths with at most 0, 1,
..., |V|-1 edges in this order).
Hence, the result!
• If negative cost cycles, then each
vertex should be checked to
have been dequeued at most
|V| times.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 21
Acyclic Graphs
• If the graph is known to be acyclic, the order
in which vertices are declared known, can be
set to be the topological order.
• Running time = O(|V|+|E|)
• This selection rule works because when a
vertex is selected, its distance can no longer
be lowered, since by topological ordering rule
it has no incoming edges emanating from
unknown nodes.
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 22
Acyclic Graphs - Example
CE 222-Data Structures & Algorithms II, Izmir
University of Economics 23
Homework Assignments
• 9.5, 9.7, 9.10, 9.40, 9.42, 9.44, 9.46, 9.47, 9.52
• You are requested to study and solve the
exercises. Note that these are for you to
practice only. You are not to deliver the results
to me.
Izmir University of Economics 24