You are on page 1of 17

Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph

oduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

1 Introduction

2 Elementary Graph Algorithms


Representation of graphs
Breadth-first search
Depth-first search

3 Minimum Spanning Trees


Introduction
Chapter 5 Growing a minimum spanning tree
Graph Algorithms The algorithms of Kruskal and Prim

4 Single-Source Shortest Paths


Introduction
Yonas Y. The Bellman-Ford algorithm
Algorithm Analysis and Design Dijkstra’s algorithm
School of Electrical and Computer Engineering
1 2
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Representation of graphs

Graphs Representation of graphs


Represents connections between objects.
Given graph G = (V, E).
Examples:
May be either directed or undirected.
Two common ways to represent for algorithms:
1 Adjacency lists.
2 Adjacency matrix.

When expressing the running time of an algorithm, it’s often in


terms of both |V| and |E|.
Figure: The Internet Figure: Maps Figure: Social Networks
In asymptotic notation-and only in asymptotic notation-we’ll
drop the cardinality.
Definition:
A Graph is a collection V of vertices, and a collection E of edges Example: O(V + E).
each of which connects a pair of vertices. 3 4
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Representation of graphs Representation of graphs

Adjacency lists Adjacency matrix

Consists of an array Adj of |V| lists, one for each vertex in V. We assume that the vertices are numbered 1, 2,..., |V| in
some arbitrary manner.
For each u ∈ V, the adjacency list Adj[u] contains all the |V| × |V| matrix A = (aij ) such that
vertices v such that there is an edge (u, v) ∈ E. (
1 if (i,j) ∈ E,
If edges have weights, put the weights in the lists. aij =
0, otherwise.
Weight: w : E→R
Space: Θ(V2 ).
Space: Θ(V + E).
Time: to list all vertices adjacent to u: Θ(V).
Time: to list all vertices adjacent to u: Θ(degree(u)). Time: to determine if (u, v) ∈ E: Θ(1).
Time: to determine if (u, v) ∈ E: O(degree(u)).
Can store weights instead of bits for weighted graph.
5 6
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Representation of graphs Breadth-first search

Breadth-first search

Given a graph G = (V, E) and a distinguished source vertex s

Figure: 5.1 Representations of an undirected graph (with 5 vertices and 7 It systematically explores the edges of G to ”discover” every
edges).
vertex that is reachable from s.

It computes the distance (smallest number of edges) from s


to each reachable vertex.

It also produces a ”breadth-first tree” with root s that


contains all reachable vertices.

Figure: 5.2 Representations of a directed graph (with 6 vertices and 8


edges). 7 8
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Breadth-first search Breadth-first search

Algorithm 1 BFS(G, s)
1: for each vertex u ∈ G.V - {s} do
To keep track of progress, breadth-first search colors each vertex 2: u.color = WHITE
3: u.d = ∞
white, gray, or black. 4: u.π = NIL
5: end for
6: s.color = GRAY
All vertices start out white and may later become gray and 7: s.d = 0
then black. 8: s.π = NIL
9: Q = ∅
10: ENQUEUE(Q, s)
If (u, v) ∈ E and vertex u is black, then vertex v is either 11: while Q 6= ∅ do
gray or black. 12: u = DEQUEUE(Q)
13: for each v ∈ G.Adj[u] do
14: if v.color == WHITE then
Gray vertices may have some adjacent white vertices; they 15: v.color = GRAY
represent the frontier between discovered and undiscovered 16: v.d = u.d + 1
17: v.π = u
vertices. 18: ENQUEUE(Q, v)
19: end if
20: end for
21: u.color = BLACK
22: end while
9 10
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Breadth-first search Breadth-first search

Analysis

After initialization

The overhead for initialization is O(V).

The operations of enqueuing and dequeuing take O(1) time,


and so the total time devoted to queue operations is O(V).

The total time spent in scanning adjacency lists is O(E).

Thus, the total running time of the BFS procedure is O(V + E).

Figure: 5.3 The operation of BFS on an undirected graph. 11 12


Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Depth-first search Depth-first search

Depth-first search

Predecessor subgraph of a depth-first search is slightly differently


The strategy followed by depth-first search is, as its name from that of a breadth-first search: we let Gπ = (V, Eπ ), where
implies, to search ”deeper” in the graph whenever possible.
Eπ = {(v.π, v): v ∈ V and v.π 6= NIL}
Explores edges out of the most recently discovered vertex v.

Once all of v’s edges have been explored, the search The predecessor subgraph of a depth-first search forms a
”backtracks”. depth-first forest comprising several depth-first trees. The
edges in Eπ are tree edges.
This process continues until we have discovered all the
vertices.

13 14
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Depth-first search Depth-first search

Depth-first search also timestamps each vertex.

the first timestamp v.d records when v is first discovered


As in breadth-first search, depth-first search colors vertices during
(and grayed), and
the search to indicate their state.
the second timestamp v.f records when the search finishes
initially white examining v’s adjacency list (and blackens v).

grayed when it is discovered in the search, and


These timestamps are integers between 1 and 2|V|, since there is
blackened when it is finished.
one discovery event and
one finishing event for each of the |V| vertices.
For every vertex u, u.d < u.f.

15 16
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Depth-first search Depth-first search

The following pseudocode is the basic depth-first-search algorithm.


Algorithm 3 DFS-VISIT(G, u)
Algorithm 2 DFS(G) 1: time = time + 1 . white vertex u has just been discovered
1: for each vertex u ∈ G.V do
2: u.d = time
3: u.color = GRAY
2: u.color = WHITE
4: for each vertex v ∈ G.Adj[u] do . explore edge (u, v)
3: u.π = NIL 5: if v.color == WHITE then
4: end for 6: v.π = u
5: time = 0 7: DFS-VISIT(G, v)
6: for each vertex u ∈ G.V do 8: end if
7: if u.color = WHITE then 9: end for
8: DFS-VISIT(G, u) 10: u.color = BLACK . blacken u; it is finished
11: time = time + 1
9: end if
12: u.f = time
10: end for

17 18
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Depth-first search Depth-first search

What is the running time of DFS?

The loops on lines 1-4 and lines 6-10 of DFS take time Θ(V).

During an execution of DFS-VISIT(G, v), the loop on lines


4-9 executes |Adj[v]| times. Thus,
P
|Adj[v]| = Θ(E).
v∈V

The running time of DFS is therefore Θ(V + E).

Figure: 5.4 The progress of the depth-first-search algorithm DFS on a directed graph.
19 20
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Depth-first search Introduction

Classification of edges Minimum Spanning Trees

We can define four edge types in terms of the depth-first forest Gπ Sample problem:
produced by a depth-first search on G:
A town has a set of houses and a set of roads.

1 Tree edges are edges in the depth-first forest Gπ . A road connects 2 and only 2 houses.
A road connecting houses u and v has a repair cost of w(u,
2 Back edges are those edges (u, v) connecting a vertex u to v).
an ancestor v in a depth-first tree.

3 Forward edges are those nontree edges (u, v) connecting a Goal:


vertex u to a descendant v in a depth-first tree.
1 everyone stays connected: can reach every house from all
4 Cross edges are all other edges. Can go between vertices in other houses, and
same depth-first tree or in different depth-first trees. 2 total repair cost is minimum.

21 22
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Introduction Introduction

A spanning tree whose weight is minimum over all spanning trees


is called a minimum spanning tree, or MST.
Model as a graph:

Undirected graph G = (V, E).

Weight w(u, v) on each edge (u, v) ∈ E.

Find T ⊂ E such that

1 T connects all vertices (T is a spanning tree), and


P
2 w(T) = w(u, v) is minimized.
(u, v)∈T

Figure: 5.5 Example of an MST graph.

23 24
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Growing a minimum spanning tree Growing a minimum spanning tree

Growing a minimum spanning tree Building up the solution

We will build a set A of edges.

Some properties of an MST: Initially, A has no edges.


As we add edges to A, maintain a loop invariant:
It has |V| - 1 edges.
Loop invariant: A is a subset of some MST.
It has no cycles.
Add only edges that maintain the invariant.
It might not be unique.
If A is a subset of some MST, an edge (u, v) is safe for A if
and only if A ∪ {(u, v)} is also a subset of some MST. So we
will add only safe edges.

25 26
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Growing a minimum spanning tree Growing a minimum spanning tree

Generic MST algorithm

Use the loop invariant to show that this generic algorithm works.

Algorithm 4 GENERIC-MST(G, w) Initialization: The empty set trivially satisfies the loop
invariant.
1: A = ∅
2: while A does not form a spanning tree do
Maintenance: Since we add only safe edges, A remains a
3: find an edge (u, v) that is safe for A
subset of some MST.
4: A = A ∪ {(u, v)}
5: end while
Termination: All edges added to A are in an MST, so when we
6: return A
stop, A is a spanning tree that is also an MST.

27 28
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Growing a minimum spanning tree Growing a minimum spanning tree

Finding a safe edge

Theorem: Let A be a subset of some MST, (S, V - S) be a cut


that respects A, and (u, v) be a light edge crossing (S, V - S).
Then (u, v) is safe for A.

A cut (S, V - S) is a partition of vertices into disjoint sets.

Edge (u, v) ∈ E crosses cut (S, V - S) if one endpoint is


in S and the other is in S - V.

A cut respects A if and only if no edge in A crosses the cut. Figure: 5.6 Black vertices are in the set S, and white vertices are in V -
S. The edge (d, c) is the unique light edge crossing the cut. A subset A
An edge is a light edge crossing a cut if and only if its weight of the edges is shaded; note that the cut (S, V - S) respects A, since
is minimum over all edges crossing the cut. For a given cut, no edge of A crosses the cut.
there can be > 1 light edge crossing it.

29 30
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim

Kruskal’s algorithm
Algorithm 5 MST-KRUSKAL(G, w)
1: A = ∅
G = (V, E) is a connected, undirected, weighted graph
2: for each vertex u ∈ G.V do
w : E → R.
3: MAKE-SET(v)
Starts with each vertex being its own component. 4: sort the edges of G.E into nondecreasing order by weight w
5: end for
Repeatedly merges two components into one by choosing the 6: for each edges (u, v) ∈ G.E, taken in nondecreasing order by
light edge that connects them (i.e., the light edge crossing the weight do
cut between them). 7: if FIND-SET(u) 6= FIND-SET(v) then
8: A = A ∪ {(u, v)}
Scans the set of edges in monotonically increasing order by
9: UNION(u, v)
weight (greedy).
10: end if
Uses a disjoint-set data structure to determine whether an 11: end for
edge connects vertices in different components. 12: return A

31 32
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim

33 34
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim

Analysis

Initialize A: O(1)

First for loop: |V| MAKE-SETs

Sort E: O(E lg E)
Second for loop: O(E) FIND-SETs and UNIONs

Assuming the implementation of disjoint-set data structure


that uses union by rank and path compression: O(E lg E)

Therefore, the total running time of Kruskal’s algorithm is


O(E lg E) or similarly O(E lg V).

Figure: 5.7 The execution of Kruskals algorithm. Shaded edges belong to


the forest A being grown. 35 36
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim

Prim’s algorithm
How to find the light edge quickly?

Use a priority queue Q:

Builds one tree, so A is always a tree. Each object is a vertex in V - VA .

Starts from an arbitrary ”root” r. Key of v is minimum weight of any edge (u, v), where u ∈
VA .
At each step, find a light edge crossing cut (VA , V − VA ), where
Then the vertex returned by EXTRACT-MIN is v such that there
VA = vertices that A is incident on. Add this edge to A.
exists u ∈ VA and (u, v) is light edge crossing (VA , V − VA ).

Key of v is ∞ if v is not adjacent to any vertices in VA .

37 38
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim

Algorithm 6 MST-PRIM(G, w, r)
The edges of A will form a rooted tree with root r: 1: for each vertex u ∈ G.V do
2: u.key = ∞
r is given as an input to the algorithm, but it can be any 3: u.π = NIL
vertex. 4: end for
5: r.key = 0
Each vertex knows its parent in the tree by the attribute v.π
= parent of v. 6: Q = G.V
7: while Q 6= ∅ do
v.π = NIL if v = r or v has no parent. 8: u = EXTRACT-MIN(Q)
9: for each v ∈ G.adj[u] do
As the algorithm progresses, A = {(v, v.π) : v ∈ V -
10: if v ∈ Q and w(u, v) < v.key then
{r} - Q}.
11: v.π = u
At termination, VA = V ⇒ Q = ∅, so MST is A = {(v, v.π) : 12: v.key = w(u, v)
v ∈ V - {r}}. 13: end if
14: end for
15: end while
39 40
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim

Figure: 5.8 The execution of Prims algorithm.

41 42
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The algorithms of Kruskal and Prim Introduction

Analysis Single-Source Shortest Paths


Depends on how the priority queue is implemented:
How to find the shortest route between two points on a map.
Suppose Q is a binary heap.

We can use the BUILD-MIN-HEAP procedure to perform lines Input:


1-6 in O(V) time. Directed graph G = (V, E)
The body of the while loop executes |V| times, and Weight function w : E→R
Each EXTRACT-MIN operation takes O(lg V) time
Thus, total EXTRACT-MIN is O(V lg V). The weight w(p) of path p = hv0 , v1 , ..., vk i is the sum of the
weights of its constituent edges:
The for loop in lines 9-14 executes O(E) times altogether. k
P
w(p) = w(vi-1 , vi )
Decrease key of r: O(lg V) i=1

Thus the total time is: O(V lg V + E lg V) = O(E lg V).


43 44
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Introduction Introduction

Variants

We define the shortest-path weight δ(u, v) from u to v by


Single-source: Find shortest paths from a given source vertex
( s ∈ V to every vertex v ∈ V.
min{w(p):u v} if there is a path from u to v,
δ(u, v) =
∞, otherwise.
Single-destination: Find shortest paths to a given destination
vertex.
A shortest path from vertex u to vertex v is then defined as any
path p with weight w(p) = δ(u, v). Single-pair: Find shortest path from u to v.

All-pairs: Find shortest path from u to v for all u, v ∈ V.

45 46
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Introduction Introduction

Optimal substructure of a shortest path:

Shortest-path algorithms typically rely on the property that a


shortest path between two vertices contains other shortest paths
within it.

Negative-weight edges:

Some instances of the single-source shortest-paths problem may


include edges whose weights are negative.

If we have a negative-weight cycle, we can just keep going


around it, and get w(s, v) = −∞ for all v on the cycle. Figure: 5.9 Negative edge weights in a directed graph. The shortest-path
weight from source s appears within each vertex.
But OK if the negative-weight cycle is not reachable from the
source.

47 48
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Introduction Introduction

Cycles

Because vertices e and f form a negative-weight cycle


reachable from s, they have shortest-path weights of −∞.
Shortest paths can’t contain cycles:
Because vertex g is reachable from a vertex whose
Already ruled out negative-weight cycles.
shortestpath weight is −∞, it, too, has a shortest-path weight
of −∞. Positive-weight ⇒ we can get a shorter path by omitting
the cycle.
Vertices such as h, i, and j are not reachable from s, and so
their shortest-path weights are ∞, even though they lie on a Zero-weight: no reason to use them ⇒ assume that our
negative-weight cycle. solutions won’t use them.

49 50
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Introduction Introduction

Representing shortest paths Relaxation

For each vertex v ∈ V, we maintain an attribute v.d, which is


Given a graph G = (V, E), we maintain for each vertex v ∈ V a an upper bound on the weight of a shortest path from source
predecessor v.π that is either another vertex or NIL. s to v.

Shortest-paths tree rooted at s is a directed subgraph G’ = (V’, We initialize the shortest-path estimates and predecessors by
E’), where V’ ⊆ V and E’ ⊆ E, such that the following Θ(V)-time procedure:

1 V’ is the set of vertices reachable from s in G,


Algorithm 7 INITIALIZE-SINGLE-SOURCE(G, s)
2 G’ forms a rooted tree with root s, and 1: for each vertex v ∈ G.V do
2: v.d = ∞
3 for all v ∈ V’, the unique simple path from s to v in G’ is a 3: v.π = NIL
shortest path from s to v in G. 4: end for
5: s.d = 0

51 52
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Introduction Introduction

Can we improve the shortest-path estimate for v by going


through u and taking (u, v)?

The following code performs a relaxation step on edge (u, v)


in O(1) time:

Figure: 5.10 Relaxing an edge (u, v) with weight w(u, v) = 2. The


Algorithm 8 RELAX(u, v, w)
shortest-path estimate of each vertex appears within the vertex.
1: if v.d > u.d + w(u, v) then
2: v.d = u.d + w(u, v)
For all the single-source shortest-paths algorithms:
3: v.π = u
4: end if Start by calling INIT-SINGLE-SOURCE,
Then relax edges.
The algorithms differ in the order and how many times they relax
53 each edge. 54
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The Bellman-Ford algorithm The Bellman-Ford algorithm

The Bellman-Ford algorithm


Algorithm 9 BELLMAN-FORD(G, w, s)
Solves the single-source shortest-paths problem in the general case 1: INITIALIZE-SINGLE-SOURCE(G, s)
in which edge weights may be negative. 2: for i = 1 to |G .V |-1 do
3: for each edge (u,v) ∈ G.E do
Computes d[v] and π[v] for all v ∈ V. 4: RELAX(u, v, w)
Returns TRUE if no negative-weight cycles reachable from s, 5: end for
FALSE otherwise. 6: end for
7: for each edge (u,v) ∈ G.E do
8: if v.d > u.d + w(u, v) then
The algorithm relaxes edges, 9: return FALSE
10: end if
progressively decreasing an estimate v.d on the weight of a
11: end for
shortest path,
12: return TRUE
until it achieves the actual shortest-path weight δ(s, v).

55 56
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

The Bellman-Ford algorithm The Bellman-Ford algorithm

The Bellman-Ford algorithm runs in time O(VE),

Initialization takes Θ(V) time

In line 2-6 |V| - 1 passes over the edges ⇒ each take Θ(E)
time

Lines 7-11 takes O(E) time.

57
Figure: 5.11 The execution of the Bellman-Ford algorithm. 58
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Dijkstra’s algorithm Dijkstra’s algorithm

Dijkstra’s algorithm

Have two sets of vertices:


Solves the single-source shortest-paths problem on a weighted,
directed graph G = (V, E) for the case in which all edge weights
S = vertices whose final shortest-path weights are determined,
are nonnegative.
Q = priority queue = V - S.
Essentially a weighted version of breadth-first search.
In the following implementation, we use a min-priority queue Q of
Uses a priority queue. vertices, keyed by their d values.
Keys are shortest-path weights (d[v]).

59 60
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Dijkstra’s algorithm Dijkstra’s algorithm

Algorithm 10 DIJKSTRA(G, w, s)
1: INITIALIZE-SINGLE-SOURCE(G, s)
Line 1 initializes the d and π values in the usual way.
2: S = ∅
Line 2 initializes the set S to the empty set.
3: Q = G.V
4: while Q 6= ∅ do Line 3 initializes the min-priority queue Q.
5: u = EXTRACT-MIN(Q)
6: S = S ∪ {u} Line 5 extracts a vertex u from Q = V - S and line 6 adds it
7: for each vertex v ∈ G.adj[u] do to set S, thereby maintaining the loop invariant.
8: RELAX(u, v, w)
9: end for Then, lines 7-8 relax each edge (u, v).
10: end while

61 62
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Dijkstra’s algorithm Dijkstra’s algorithm

Correctness of Dijkstra’s algorithm

Theorem: Dijkstra’s algorithm, run on a weighted, directed graph


G = (V, E) with nonnegative weight function w and source s,
terminates with u.d = δ(s, u) for all vertices u ∈ V.

Loop invariant: At the start of each iteration of the while


loop, d[v] = δ(s, v) for all v ∈ S.

Initialization: Initially, S = ∅, so trivially true.


Maintenance: Need to show that d[u] = δ(s, u) when u is added
Figure: 5.12 The execution of the Dijkstra’s algorithm. to S in each iteration.
Termination: At end, Q = ∅ ⇒ S = V ⇒ d[v] = δ(s, v) for all v
Dijkstra’s algorithm can be viewed as greedy, since it always ∈ V.
chooses the ”lightest” (”closest”) vertex in V - S to add to S.
63 64
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Dijkstra’s algorithm Dijkstra’s algorithm

Analysis
The running time of Dijkstra’s algorithm depends on how we
implement the min-priority queue.
How fast is Dijkstra’s algorithm?
If binary min-heap is used:
It maintains the min-priority queue Q by calling three priority-queue
operations. Each EXTRACT-MIN operation then takes time O(lg V).
The time to build the binary min-heap is O(V).
1 INSERT (implicit in line 3), Each DECREASE-KEY operation takes time O(lg V).

2 EXTRACT-MIN (line 5), and


The total running time is therefore O((V + E)lg V), which is
3 DECREASE-KEY (implicit in RELAX, which is called in line 8). O(E lg V) if all vertices are reachable from the source.

The algorithm calls both INSERT and EXTRACT-MIN once per Dijkstra’s algorithm resembles both breadth-first search and Prim’s
vertex and it calls DECREASE-KEY at most |E| times overall. algorithm for computing minimum spanning trees.

65 66
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

Dijkstra’s algorithm

End of Chapter 5

Questions?

67

You might also like