Professional Documents
Culture Documents
Graphs
[Part 1]
1
Lecture Content
1. Graph Basics
1.1 Definitions and Terminologies
1.2 Data Structures Used to Store Graphs
2. Graph Traversal
2.1 Depth-First Search (DFS)
2.2. Breadth-First Search (BFS)
2
Lecture Content
3. Topological Sorting
Course prerequisites
Topologically sorted order: C F B A E D G H
3
1. Graph Basics
4
1. Graph Basics
5
1. Graph Basics
6
1. Graph Basics
7
1.1 Definitions and Terminologies
8
1.1 Definitions and Terminologies
9
1.1 Definitions and Terminologies
10
1.1 Definitions and Terminologies
12
1.1 Definitions and Terminologies
13
1.1 Definitions and Terminologies
14
1.2 Data Structures Used to Store Graphs
15
Adjacency Matrix
16
Adjacency Matrix
17
Adjacency Matrix
18
Adjacency List
19
Lecture Content
1. Graph Basics
1.1 Definitions and Terminologies
1.2 Data Structures Used to Store Graphs
2. Graph Traversal
2.1 Depth-First Search (DFS)
2.2. Breadth-First Search (BFS)
20
2. Graph Traversal
21
2.1 Depth-First Search (DFS)
22
2.1 Depth-First Search (DFS)
24
2.1 Depth-First Search (DFS)
6. if w exists then
7. Push(w, S)
8. mark w visited
9. predfn ← predfn + 1
10. else
11. Pop(S); postdfn ← postdfn + 1
12. end if
13. end while
25
2.1 Depth-First Search (DFS)
26
2.1 Depth-First Search (DFS)
27
2.1 Depth-First Search (DFS)
28
2.1 Depth-First Search (DFS)
30
2.1 Depth-First Search (DFS)
31
2.2 Breadth-First Search (BFS)
33
2.2 Breadth-First Search (BFS)
36
2.2 Breadth-First Search (BFS)
37
2.2 Breadth-First Search (BFS)
38
2.2 Breadth-First Search (BFS)
39
3. Topological Sorting
40
3. Topological Sorting
41
3. Topological Sorting
42
Exercises
43
Exercises
44
Exercises
45
References
46
References
47
CS161, Winter 2011 Handout #16
1 The Algorithm
Goal of Lecture: to give a linear-time (i.e., O(m+n)-time) algorithm that computes the strongly connected
components of a directed graph.
The algorithm we present is essentially two passes of depth-first search, plus some extremely clever
additional book-keeping. The algorithm is described in a top-down fashion in Figures 2–4.
Input: a directed graph G = (V, E), in adjacency list representation. Assume that the vertices V are labeled
1, 2, 3, . . . , n.
1. Let Grev denote the graph G after the orientation of all arcs have been reversed.
2. Run the DFS-Loop subroutine on Grev , processing vertices according to the given order, to obtain a
finishing time f (v) for each vertex v ∈ V .
3. Run the DFS-Loop subroutine on G, processing vertices in decreasing order of f (v), to assign a leader
to each vertex v ∈ V .
4. The strongly connected components of G correspond to vertices of G that share a common leader.
Figure 2: The top level of our SCC algorithm. The f -values and leaders are computed in the first and second
calls to DFS-Loop, respectively (see below).
1
Input: a directed graph G = (V, E), in adjacency list representation.
1. Initialize a global variable t to 0.
[This keeps track of the number of vertices that have been fully explored.]
2. Initialize a global variable s to NULL.
[This keeps track of the vertex from which the last DFS call was invoked.]
3. For i = n downto 1:
[In the first call, vertices are labeled 1, 2, . . . , n arbitrarily. In the second call, vertices are labeled by
their f (v)-values from the first call.]
Input: a directed graph G = (V, E), in adjacency list representation, and a source vertex i ∈ V .
1. Mark i as explored.
[It remains explored for the entire duration of the DFS-Loop call.]
2. Set leader(i) := s
3. For each arc (i, j) ∈ G:
4. t + +
5. Set f (i) := t
Figure 4: The DFS subroutine. The f -values only need to be computed during the first call to DFS-Loop, and
the leader values only need to be computed during the second call to DFS-Loop.
2
As we’ve seen, each invocation of DFS-Loop can be implemented in linear time. You should think
about how to implement the remaining details of the algorithm so that its overall running time is linear (i.e.,
(O(m + n))).
2 An Example
But why on earth should this algorithm work? An example should increase its plausibility (though it
certainly doesn’t constitute a proof of correctness). Figure 5(a) displays a reversed graph Grev , with its
vertices numbered arbitrarily, and the f -values computed in the first call to DFS-Loop. In more detail, the
first DFS is initiated at node 9. The search must proceed next to node 6. DFS then has to make a choice
between two different adjacent nodes; we have shown the f -values that ensue when DFS visits node 3 before
node 8.1 When DFS visits node 3 it gets stuck; at this point node 3 is assigned a finishing time of 1. DFS
backtracks to node 6, proceeds to node 8, then node 2, and then node 5. DFS then backtracks all the way
back to node 9, resulting in nodes 5, 2, 8, 6, and 9 receiving the finishing times 2, 3, 4, 5, and 6, respectively.
Execution returns to DFS-Loop, and the next (and final) call to DFS begins at node 7.
Figure 5(b) shows the original graph (with all arcs now unreversed), with nodes labeled with their finishing
times. The magic of the algorithm is now evident, as the SCCs of G present themselves to us in order: the
first call to DFS discovers the nodes 7–9 (with leader 9); the second the nodes 1, 5, and 6 (with leader 6);
and the third the remaining three nodes (with leader 4).
f (5) = 2
8 1 3
4 3 2
f (4) = 8 f (3) = 1 f (2) = 3 leader = 9 leader = 4
leader = 6
(a) First DFS-Loop on Grev (b) Second DFS-Loop on G
Figure 5: Example execution of the strongly connected components algorithm. In (a), nodes are labeled
arbitrarily and their finishing times are shown. In (b), nodes are labeled by their finishing times and their
leaders are shown.
3 Proof of Correctness
3.1 The Acyclic Meta-Graph of SCCs
First, observe that the strongly connected components of a directed graph form an acyclic “meta-graph”,
where the meta-nodes correspond to the SCCs C1 , . . . , Ck , and there is an arc Ch → Cℓ with h 6= ℓ if and
only if there is at least one arc (i, j) in G with i ∈ Ch and j ∈ Cℓ . This directed graph must be acyclic:
since within a SCC you can get from anywhere to anywhere else on a directed path, in a purported directed
cycle of SCCs you can get from every node in a constituent SCC to every other node of every other SCC
in the cycle. Thus the purported cycle of SCCs is actually just a single SCC. Summarizing, every directed
graph has a useful “two-tier” structure: zooming out, one sees a DAG on the SCCs of the graph; zooming
in on a particular SCC exposes its finer-grained structure. For example, the meta-graphs corresponding to
the directed graphs in Figures 1 and 5(b) are shown in Figure 6.
3
C2
C1 C4
C3
C3 C2 C1
(a) SCC graph for Figure 1 (b) SCC graph for Figure 5(b)
Figure 6: The DAGs of the SCCs of the graphs in Figures 1 and 5(b), respectively.
Key Lemma: Consider two “adjacent” strongly connected components of a graph G: components C1
and C2 such that there is an arc (i, j) of G with i ∈ C1 and j ∈ C2 . Let f (v) denote the finishing time of
vertex v in some execution of DFS-Loop on the reversed graph Grev . Then
Proof of Key Lemma: Consider two adjacent SCCs C1 and C2 , as they appear in the reversed graph Grev
— where there is an arc (j, i), with j ∈ C2 and i ∈ C1 (Figure 7). Because the equivalence relation defining
the SCCs is symmetric, G and Grev have the same SCCs; thus C1 and C2 are also SCCs of Grev . Let v
denote the first vertex of C1 ∪ C2 visited by DFS-Loop in Grev . There are now two cases.
First, suppose that v ∈ C1 (Figure 7(a)). Since there is no non-trivial cycle of SCCs (Section 3.1), there
is no directed path from v to C2 in Grev . Since DFS discovers everything reachable and nothing more, it
will finish exploring all vertices in C1 without reaching any vertices in C2 . Thus, every finishing time in C1
will be smaller that every finishing time in C2 , and this is even stronger than the assertion of the lemma.
(Cf., the left and middle SCCs in Figure 5.)
Second, suppose that v ∈ C2 (Figure 7(b)). Since DFS discovers everything reachable and nothing more,
the call to DFS at v will finish exploring all of the vertices in C1 ∪ C2 before ending. Thus, the finishing
time of v is the largest amongst vertices in C1 ∪ C2 , and in particular is larger than all finishing times in C1 .
(Cf., the middle and right SCCs in Figure 5.) This completes the proof.
C1 C2 C1 C2
i j i j
v v
(a) All f -values in C1 smaller than in C2 (b) v has the largest f -value in C1 ∪ C2
Figure 7: Proof of Key Lemma. Vertex v is the first in C1 ∪ C2 visited during the execution of DFS-Loop
on Grev .
4
on G, processing vertices in decreasing order of finishing times, the successive calls to DFS peel off the SCCs
of the graph one at a time, like layers of an onion.
We now formally prove correctness of our algorithm for computing strongly connected components.
Consider the execution of DFS-Loop on G. We claim that whenever DFS is called on a vertex v, the
vertices explored — and assigned a common leader — by this call are precisely those in v’s SCC in G. Since
DFS-Loop eventually explores every vertex, this claim implies that the SCCs of G are precisely the groups
of vertices that are assigned a common leader.
We proceed by induction. Let S denote the vertices already explored by previous calls to DFS (initially
empty). Inductively, the set S is the union of zero or more SCCs of G. Suppose DFS is called on a vertex v
and let C denote v’s SCC in G. Since the SCCs of a graph are disjoint, S is the union of SCCs of G,
and v ∈ / S, no vertices of C lie in S. Thus, this call to DFS will explore, at the least, all vertices of C.
By the Key Lemma, every outgoing arc (i, j) from C leads to some SCC C ′ that contains a vertex w with
a finishing time larger than f (v). Since vertices are processed in decreasing order of finishing time, w has
already been explored and belongs to S; since S is the union of SCCs, it must contain all of C ′ . Summarizing,
every outgoing arc from C leads directly to a vertex that has already been explored. Thus this call to DFS
explores the vertices of C and nothing else. This completes the inductive step and the proof of correctness.
5
Lecture 7: Minimum Spanning Trees and
Prim’s Algorithm
CLRS Chapter 23
– The algorithm
– Correctness
1
Spanning Trees
Example:
a
b
a
b
d
d
c e
c
e
Graph spanning tree 1
a
b
a
b
d
d
c
e
c
e
2
Spanning Trees
Question: Given a connected graph , how can you
find a spanning tree of ?
3
Weighted Graphs
Example:
a
b
a
b
7 32 10
32
10
d
d
c 9
23
c 23
e
e 9
weighted graph Tree 1. w=74
a
b
b
32 a
7 10
7 32
d 23
23
d
c
e
c e
9
Tree 2, w=71 Tree 3, w=72
Minimum spanning tree
4
Minimum Spanning Trees
Example:
a
b
a
b
7 32 10
32
10
d
d
c 9
23
c 23
e
e 9
weighted graph Tree 1. w=74
a
b
b
32 a
7 10
7 32
d 23
d 23
c
e
c
e
9
Tree 2, w=71 Tree 3, w=72
Minimum spanning tree
5
Minimum Spanning Trees
Example:
1 1 1
2 2 2 2
24 67 24 67 24 67
100
weighted graph MST1 MST2
6
Minimum Spanning Tree Problem
7
Generic Algorithm for MST problem
Generic-MST(G, w)
Let A=EMPTY;
while A does not form a spanning tree
find an edge (u, v) that is safe for A
add (u, v) to A
return A
8
How to find a safe edge
Cross An edge crosses the cut
9
How to find a safe edge
Lemma
10
Proof
1. Let , where is a MST. Suppose
.
11
3. Since , and are on opposite sides of the cut
have .
y y
A A
x x
v v
u u
a cut respects A
12
5. The weight of is
7. Since is also a subset of (a MST),
is safe for .
13
Prim’s Algorithm
14
Prim’s Algorithm : How to grow a tree
Grow a Tree
Running time is
15
More Details
Step 0: Choose any element ; set and
. (Take as the root of our spanning tree.)
Step 2: If , then stop & output (minimum)
b 24 h b 24 h
a 20 12 a 20 12
26 26
12 12
c 16 g i c 16 g i
r 23 r
14 14 23
f f
8 8
new
d e d e
new edge
16
Prim’s Algorithm
Worked Example
10
b e
4 8 7 6
a 9 d 5 g Connected graph
9
8 2 2
c f
1
10 Step 0
b e
4 8 7 6 S={a}
a 9 d 5 g
9 V \ S = {b,c,d,e,f,g}
8 2 2
lightest edge = {a,b}
c f
1
17
Prim’s Algorithm
10
b e Step 1.1 before
4 8 7 6 S={a}
a 9 d 5 g V \ S = {b,c,d,e,f,g}
9 A={}
8 2 2 lightest edge = {a,b}
c f
1
18
Prim’s Algorithm
10
b e Step 1.2 before
4 8 7 6 S={a,b}
a 9 d 5 g V \ S = {c,d,e,f,g}
9 A={{a,b}}
8 2 2 lightest edge = {b,d}, {a,c}
c f
1
19
Prim’s Algorithm
10
b e Step 1.3 before
4 8 7 6
S={a,b,d}
a 9 d 5 g V \ S = {c,e,f,g}
9 A={{a,b},{b,d}}
8 2 2 lightest edge = {d,c}
c f
1
20
Prim’s Algorithm
10
b e Step 1.4 before
4 8 7 6
S={a,b,c,d}
a 9 d 5 g
9 V \ S = {e,f,g}
8 2 2 A={{a,b},{b,d},{c,d}}
c f lightest edge = {c,f}
1
21
Prim’s Algorithm
10
b e Step 1.5 before
4 8 7 6 S={a,b,c,d,f}
a 9 d 5 g V \ S = {e,g}
9 A={{a,b},{b,d},{c,d},{c,f}}
8 2 2 lightest edge = {f,g}
c f
1
22
Prim’s Algorithm
10
b e Step 1.6 before
4 8 7 6 S={a,b,c,d,f,g}
a 9 d 5 g V \ S = {e}
9 A={{a,b},{b,d},{c,d},{c,f},
8 2 2 {f,g}}
c f
1 lightest edge = {f,e}
23
Recall Idea of Prim’s Algorithm
Step 0: Choose any element and set and .
(Take as the root of our spanning tree.)
Step 1: Find a lightest edge
such that one endpoint is in and
the other is in . Add this edge to and its (other)
endpoint to .
Step 2: If , then stop and output the minimum span-
ning tree
.
Otherwise go to Step 1.
Questions:
24
Prim’s Algorithm
Answer:
(a) Use a priority queue to find the lightest edge.
(b) Use pred[ ] to update .
25
Reviewing Priority Queues
insert( ):
Insert with the key value in .
u = extractMin():
Extract the item with the minimum key value in .
decreaseKey( - ):
Decrease ’s key value to - .
26
Using a Priority Queue to Find the Lightest Edge
is the weight of the lightest edge
from to any vertex in , and
is the endpoint of this edge in .
The array is used to build the MST tree.
b 24 h b 24 h
a 20 12 a 20 12
26 26
12 12
c 16 g i c 16 g i
r 23 r
14 14 23
f f
8 8
d e d e
new edge
key[f] = 8, pred[f] = e
key[i] = infinity, pred[i] = nil key[i] = 23, pred[i] = f
key[g] = 16, pred[g] = c After adding the new edge
key[h] = 24, pred[h] = b and vertex f, update the key[v]
and pred[v] for each vertex v
f has the minimum key adjacent to f
27
Description of Prim’s Algorithm
Remark: is given by adjacency lists. The vertices in
are stored in a priority queue with key=value of lightest edge to
vertex in .
Prim(
)
for
each initialize
;
;
; ! start at root
;
" "
new
"
PriQueue( ); put vertices in
while(
"$#
is nonempty) until all vertices in MST
u= extraxtMin(); )(* lightest edge
&
% '
for
each (
)
if-(( . %!
)&&( %!
,+ )) %!
%/
%!
; - . new lightest edge
"$#
decreaseKey( % %!
);
0
%!
;
1 2
"
When the algorithm terminates, and the MST is
3 0 #
% 4/%&
%!
$
The pred pointers define the MST as an inverted tree
rooted at .
28
Example for Running Prim’s Algorithm
a e
3
1 10
4
3
b d
4 1
2
5
c f
u a b c d e f
key[u]
pred[u]
29
Analysis of Prim’s Algorithm
Let and . The data structure PriQueue
supports the following two operations: (See CLRS)
30
Analysis of Prim’s Algorithm – Continued
Prim(G, w, r) {
for each (u in V)
{
key[u] = +infinity; 2n
color[u] = white;
}
key[r] = 0; 1
pred[r] = nil; 1
Q = new PriQueue(V); n
32
Lecture 8: Kruskal’s MST Algorithm
CLRS Chapter 23
Kruskal’s algorithm
Another, but different, greedy MST algorithm
1
Idea of Kruskal’s Algorithm
In each step add the cheapest edge that does not cre-
ate a cycle.
10
a b a b
9
5
3 12 c c
e d 2 e d
7
original graph forest MST
3
Outline of Kruskal’s Algorithm
Step 0: Set and , the set of all edges.
Step 2: If , stop and output the minimal span-
ning tree
. Otherwise go to Step 1.
Remark: Will see later, after each step,
is a
subgraph of a MST.
4
Outline of Kruskal’s Algorithm
Implementation Questions:
5
How to Choose the Edge of Least Weight
Question:
How does algorithm choose edge with mini-
mum weight?
Answer: Start by sorting edges in in order of in-
creasing weight.
Walk through the edges in this order.
(Once edge causes a cycle it will always cause a cycle so it
can be thrown away.)
6
How to Check for Cycles
Create-Set( ):
Create a set containing the single element .
Find-Set( ):
Find the set containing the element .
Union( ):
Merge the sets respectively containing and
into a common set.
8
Kruskal’s Algorithm: the Details
Sort in increasing order by weight ;
/* After sorting
!#" */
;
for (each in ) CREATE-SET( );
for %$ &$ '$ from 1 to do
if (FIND-SET( $ ) != FIND-SET( $ ) )
(
add $ $ to ;
UNION( $ $ );
)
return(A);
9
Why Kruskal’s Algorithm is correct?
1. Let denote the tree of the forest
that contains . Consider the cut .
3. Since adding to does not induce a cy-
cle, crosses the cut. Moreover, since
is currently the smallest edge, is the light
edge crossing the cut. This completes the cor-
rectness proof of Kruskal’s Algorithm.
10
Why Kruskal’s Algorithm is correct?
A’=(V’, E’)
u
G=(V, E)
11
Single Source Shortest Paths
Given a weighted graph G= (V,E) where the weights are >0.
A source vertex, vo belong to V.
Find the shortest path from vo to all other nodes in G.
Shortest paths are generated in increasing order: 1,2,3,… ..
Dijkstra Algorithm
S: Set of vertices (including vo) whose final shortest paths from the source
vo have already been determined.
For each node wV-S,
Dist (w): the length of the shortest path starting from vo going through only
vertices which are in S and ending at w.
The next path is generated as follows:
It's the path of a vertex u which has Dist (u) minimum among all vertices in
V-S
Put u in S.
Dist (w) for w in V-S may be decreased going though u.
Algorithm:
Example:
2 1
A B
5 2 E
Iteration N DB DC DD DE DF
Initial {A} 2 3 5
1 {A,B} 2 3 3 4
2 {A,B, C} 2 3 7(A-C-D) 3 4(no change because
of same cost A-C-F)
2 1
A B
5 2 E
3
F
1 3 2
C D
4
2 1
A B
5 2 E
3
F
1 3 2
C D
4
2 1
A B
5 2 E
3
F
1 3 2
C D
4
2 1
A B
5 2 E
3
F
1 3 2
C D
4
2 1
A B
5 2 E
3
F
1 3 2
C D
4
2 1
A B
5 2 E
3
F
1 3 2
C D
4
Time complexity: O(v2) when adjacency matrix if the input is represented using
adjacency list it can be reduced to O((e+v) log v) with the help of binary heap.
Drawback:
Dijkstra Algorithm will fail when there is negative weight cycle in the graph.
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Shortest Paths
Dijkstra Bellman-Ford Floyd All-pairs paths
1 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
2 Dijkstra’s algorithm
3 Bellman-Ford algorithm
5 Floyd’s algorithm
2 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3
F 1 F
3
8
C D C D
2 3
E E
4 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
5 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Dijkstra’s Algorithm
algorithm Dijkstra( weighted digraph (G, c), node s ∈ V (G) )
array colour[n] = {WHITE, . . . , WHITE}
array dist[n] = {c[s, 0], . . . , c[s, n − 1]}
colour[s] ← BLACK
while there is a WHITE node do
pick a WHITE node u, such that dist[u] is minimum
colour[u] ← BLACK
for each x adjacent to u do
if colour[x] = WHITE then
dist[x] ← min dist[x], dist[u] + c[u, x]
end if
end for
end while
return dist
end
7 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b BLACK dist[x]
List S a b c d e
2 a 0 3 8 ∞ ∞
a
2 ab 0 3 8 5 ∞
3
abd 0 3 7 5 10
1 d abcd 0 3 7 5 9
8 2 abcde 0 3 7 5 9
7 5
c
2
e
8 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b BLACK dist[x]
List S a b c d e
2 a 0 3 8 ∞ ∞
a
2 ab 0 3 8 5 ∞
3
abd 0 3 7 5 10
1 d abcd 0 3 7 5 9
8 2 abcde 0 3 7 5 9
7 5
c
2
e
8 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b BLACK dist[x]
List S a b c d e
2 a 0 3 8 ∞ ∞
a
2 ab 0 3 8 5 ∞
3
abd 0 3 7 5 10
1 d abcd 0 3 7 5 9
8 2 abcde 0 3 7 5 9
7 5
c
2
e
8 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b BLACK dist[x]
List S a b c d e
2 a 0 3 8 ∞ ∞
a
2 ab 0 3 8 5 ∞
3
abd 0 3 7 5 10
1 d abcd 0 3 7 5 9
8 2 abcde 0 3 7 5 9
7 5
c
2
e
8 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b BLACK dist[x]
List S a b c d e
2 a 0 3 8 ∞ ∞
a
2 ab 0 3 8 5 ∞
3
abd 0 3 7 5 10
1 d abcd 0 3 7 5 9
8 2 abcde 0 3 7 5 9
7 5
c
2
e
8 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
u
y
γ1
s
Sk
11 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
γ1
12 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6
F E D Node u A B C D E F
0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
A B
7
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6 Node u A B C D E F
F E D 0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
A B
7
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6 Node u A B C D E F
F E D 0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
while-loop:
9 10 WHITE B, C, D, E, F : min dist[B]
colour[B] ← BLACK
for x ∈ V (G)
A B dist[x] ←
7 min dist[x], dist[B] + c[B, x]
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6 Node u A B C D E F
F E D 0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
while-loop:
9 10 WHITE C, D, E, F : min dist[C]
colour[C] ← BLACK;
for x ∈ V (G)
A B dist[x] ←
7 min dist[x], dist[C] + c[C, x]
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6 Node u A B C D E F
F E D 0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
while-loop:
9 10 WHITE D, E, F : min dist[F ]
colour[F ] ← BLACK;
for x ∈ V (G)
A B dist[x] ←
7 min dist[x], dist[F ] + c[F, x]
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6 Node u A B C D E F
F E D 0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
while-loop:
9 10 WHITE D, E: min dist[D]
colour[D] ← BLACK;
for x ∈ V (G)
A B dist[x] ←
7 min dist[x], dist[D] + c[D, x]
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
9 6 Node u A B C D E F
F E D 0 7 9 ∞ ∞ 14
A 0 7 9 ∞ ∞ 14
2 11 A B 0 7 9 22 ∞ 14
A B C 0 7 9 20 ∞ 11
A B C F 0 7 9 20 20 11
A B C DF 0 7 9 20 20 11
14 C 15 A B C DEF 0 7 9 20 20 11
while-loop:
9 10 WHITE E: min dist[E]
colour[E] ← BLACK;
for x ∈ V (G)
A B dist[x] ←
7 min dist[x], dist[E] + c[E, x]
13 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
t ← τ + c(u, x)
colour[x] ← GREY
yes
Q.insert(x, t) colour[x] = WHITE?
no
yes yes
Q.decreaseKey(x, t) Q.getKey(x) > t? colour[x] = GREY?
no no
14 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Initialisation:
Priority queue Q = {akey=0 }
v∈V a b c d e f g
keyv 0
dist[v] − − − − − − −
15 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u ← a; t1 ← keya = 0; x ∈ {b, c, d}
x ← b: t2 = t1 + cost(a, b) = 2; Q = {a0 , b2 }
v∈V a b c d e f g
keyv 0 2
dist[v] − − − − − − −
16 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u = a; t1 = keya = 0; x ∈ {b, c, d}
x ← c: t2 = t1 + cost(a, c) = 3; Q = {a0 , b2 , c3 }
v∈V a b c d e f g
keyv 0 2 3
dist[v] − − − − − − −
17 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u = a; t1 = keya = 0; x ∈ {b, c, d}
x ← d: t2 = t1 + cost(a, d) = 3; Q = {a0 , b2 , c3 , d3 }
v∈V a b c d e f g
keyv 0 2 3 3
dist[v] − − − − − − −
18 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = a
dist[a] ← t1 = 0; Q = {b2 , c3 , d3 }
v∈V a b c d e f g
keyv 0 2 3 3
dist[v] 0 − − − − − −
19 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u ← b; t1 ← keyb = 2; x ∈ {c, e}
x ← c: t2 = t1 + cost(b, c) = 2 + 4 = 6; keyc = 3 < t2 = 6
v∈V a b c d e f g
keyv 0 2 3 3
dist[v] 0 − − − − − −
20 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u = b; t1 = keyb = 2; x ∈ {c, e}
x ← e: t2 = t1 + cost(b, e) = 2 + 3 = 5; Q = {b2 , c3 , d3 , e5 }
v∈V a b c d e f g
keyv 0 2 3 3 5
dist[v] 0 − − − − − −
21 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = b
dist[b] ← t1 = 2; Q = {c3 , d3 , e5 }
v∈V a b c d e f g
keyv 0 2 3 3 5
dist[v] 0 2 − − − − −
22 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u ← c; t1 ← keyc = 3; x ∈ {d, e, f }
x ← d: t2 = t1 + cost(c, d) = 3 + 5 = 8; keyd = 3 < t2 = 8
v∈V a b c d e f g
keyv 0 2 3 3 5
dist[v] 0 2 − − − − −
23 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u = c; t1 = keyc = 3; x ∈ {d, e, f }
x ← e: t2 = t1 + cost(c, d) = 3 + 1 = 4; keye = 5 < t2 = 4; keye ← 4
v∈V a b c d e f g
keyv 0 2 3 3 4
dist[v] 0 2 − − − − −
24 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u = c; t1 = keyc = 3; x ∈ {d, e, f }
x ← f : t2 = t1 + cost(c, f ) = 3 + 6 = 9; Q = {c3 , d3 , e4 , f9 }
v∈V a b c d e f g
keyv 0 2 3 3 4 9
dist[v] 0 2 − − − − −
25 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = c
dist[c] ← t1 = 3; Q = {d3 , e4 , f9 }
v∈V a b c d e f g
keyv 0 2 3 3 4 9
dist[v] 0 2 3 − − − −
26 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u ← d; t1 ← keyd = 3; x ∈ {f }
x ← f : t2 = t1 + cost(d, f ) = 3 + 7 = 10; keyf = 9 < t2 = 10
v∈V a b c d e f g
keyv 0 2 3 3 4 9
dist[v] 0 2 3 − − − −
27 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = d
dist[d] ← t1 = 3; Q = {e4 , f9 }
v∈V a b c d e f g
keyv 0 2 3 3 4 9
dist[v] 0 2 3 3 − − −
28 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u ← e; t1 ← keye = 4; x ∈ {f }
x ← f : t2 = t1 + cost(e, f ) = 4 + 8 = 12; keyf = 9 < t2 = 12
v∈V a b c d e f g
keyv 0 2 3 3 4 9
dist[v] 0 2 3 3 − − −
29 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = e
dist[e] ← t1 = 4; Q = {f9 }
v∈V a b c d e f g
keyv 0 2 3 3 4 9
dist[v] 0 2 3 3 4 − −
30 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
u ← f ; t1 ← keyf = 9; x ∈ {g}
x ← g: t2 = t1 + cost(f, g) = 9 + 9 = 18; Q = {f9 , g18 }
v∈V a b c d e f g
keyv 0 2 3 3 4 9 18
dist[v] 0 2 3 3 4 − −
31 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = f
dist[f ] ← t1 = 9; Q = {g18 }
v∈V a b c d e f g
keyv 0 2 3 3 4 9 18
dist[v] 0 2 3 3 4 9 −
32 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
7
3 5
2 c f g
6 9
8
4 1
b e
3
Completing the while-loop for u = g
dist[g] ← t1 = 18; no adjacent verices for g; empty Q = {}
v∈V a b c d e f g
keyv 0 2 3 3 4 9 18
dist[v] 0 2 3 3 4 9 18
33 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
34 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Replacing the two nested for-loops by the nodes x, v ∈ V (G) with a single
for-loop by the arcs (x, v) ∈ E(G).
Time complexity: Θ(mn) using adjacency lists vs. Θ(n3 ) using an adjacency
matrix.
35 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Bellman-Ford Algorithm
Theorem 6.9
If a graph G contains no negative weight cycles, then after the ith
iteration of the outer for-loop, the element dist[v] contains the
minimum weight of a path to v for all nodes v with level at most i.
36 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
as required at level i.
37 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
a b c d e
2 0 0 ∞ ∞ ∞ ∞
a
2 1 0 3 −1 ∞ ∞
-2
2 0 0 −1 3 5
1 d 3 0 0 −1 2 0
-1 2 4 0 0 −1 2 −1
4 -3
c
6
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
3
a b c d e
2 0 0 ∞ ∞ ∞ ∞
a
2 1 0 3 −1 ∞ ∞
-2
2 0 0 −1 3 5
1 ∞ d 3 0 0 −1 2 0
-1 2 4 0 0 −1 2 −1
-1 4 -3
c
6
∞
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
0
a b c d e
2 0 0 ∞ ∞ ∞ ∞
a
2 1 0 3 −1 ∞ ∞
-2
2 0 0 −1 3 5
1 3 d 3 0 0 −1 2 0
-1 2 4 0 0 −1 2 −1
-1 4 -3
c
6
5
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
0
a b c d e
2 0 0 ∞ ∞ ∞ ∞
a
2 1 0 3 −1 ∞ ∞
-2
2 0 0 −1 3 5
1 2 d 3 0 0 −1 2 0
-1 2 4 0 0 −1 2 −1
-1 4 -3
c
6
-1
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
0
a b c d e
2 0 0 ∞ ∞ ∞ ∞
a
2 1 0 3 −1 ∞ ∞
-2
2 0 0 −1 3 5
1 2 d 3 0 0 −1 2 0
-1 2 4 0 0 −1 2 −1
-1 4 -3
c
6
-1
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Iteration i = 0
x, v Distance d[v] ← min{d[v], d[x] + c(x, v)} a b c d e
0 ∞ ∞ ∞ ∞
a, b d[b] ← min{∞, 0 + 3} = 3 0 3 ∞ ∞ ∞
a, c d[c] ← min{∞, 0 − 1} = −1 0 3 −1 ∞ ∞
b, a d[a] ← min{0, 3 + 2} = 0 0 3 −1 ∞ ∞
b, d d[d] ← min{∞, 3 + 2} = 5 0 3 −1 5 ∞
c, b d[b] ← min{3, −1 + 1} = 0 0 0 −1 5 ∞
c, d d[d] ← min{5, −1 + 4} = 3 0 0 −1 3 ∞
c, e d[e] ← min{∞, −1 + 6} = 5 0 0 −1 3 5
d, b d[b] ← min{0, 3 − 2} = 0 0 0 −1 3 5
d, c d[c] ← min{−1, 3 + 2} = −1 0 0 −1 3 5
d, e d[e] ← min{5, 3 − 3} = 0 0 0 −1 3 0
39 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Arc (x, v): a,b a,c b,a b,d c,b c,d c,e d,b d,c d,e
c(x, v): 3 −1 2 2 1 4 6 −2 2 −3
Iteration i = 1
x, v Distance d[v] ← min{d[v], d[x] + c(x, v)} a b c d e
0 0 −1 3 0
a, b d[b] ← min{0, 0 + 3} = 0 0 0 −1 3 0
a, c d[c] ← min{−1, 0 − 1} = −1 0 0 −1 3 0
b, a d[a] ← min{0, 0 + 2} = 0 0 0 −1 3 0
b, d d[d] ← min{3, 0 + 2} = 2 0 0 −1 2 0
c, b d[b] ← min{0, −1 + 1} = 0 0 0 −1 2 0
c, d d[d] ← min{2, −1 + 4} = 2 0 0 −1 2 0
c, e d[e] ← min{0, −1 + 6} = 0 0 0 −1 2 0
d, b d[b] ← min{0, 2 − 2} = 0 0 0 −1 2 0
d, c d[c] ← min{−1, 2 + 2} = −1 0 0 −1 2 0
d, e d[e] ← min{0, 2 − 3} = −1 0 0 −1 2 −1
39 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Arc (x, v): a,b a,c b,a b,d c,b c,d c,e d,b d,c d,e
c(x, v): 3 −1 2 2 1 4 6 −2 2 −3
Iteration i = 2..4
x, v Distance d[v] ← min{d[v], d[x] + c(x, v)} a b c d e
0 0 −1 2 −1
a, b d[b] ← min{0, 0 + 3} = 0 0 0 −1 2 −1
a, c d[c] ← min{−1, 0 − 1} = −1 0 0 −1 2 −1
b, a d[a] ← min{0, 0 + 2} = 0 0 0 −1 2 −1
b, d d[d] ← min{2, 0 + 2} = 2 0 0 −1 2 −1
c, b d[b] ← min{0, −1 + 1} = 0 0 0 −1 2 −1
c, d d[d] ← min{2, −1 + 4} = 2 0 0 −1 2 −1
c, e d[e] ← min{−1, −1 + 6} = −1 0 0 −1 2 −1
d, b d[b] ← min{0, 3 − 2} = 0 0 0 −1 2 −1
d, c d[c] ← min{−1, 3 + 2} = −1 0 0 −1 2 −1
d, e d[e] ← min{−1, 3 − 3} = −1 0 0 −1 2 −1
39 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
40 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Given a weighted digraph (G, c), determine for each pair of nodes
u, v ∈ V (G) (the length of) a minimum weight path from u to v.
Convenient output: a distance matrix D = D[u, v] u,v∈V (G)
• Time complexity Θ(nAn,m ) of computing the matrix D by
finding the single-source shortest paths (SSSP) from each
node as the source in turn.
• An=|V (G)|,m=|E(G)| – the complexity of the SSSP algorithm.
41 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Floyd’s Algorithm
3 1 0 1 2 3 4
−1 ∞ ∞
0 0 3
2 1 2 0 ∞ 2 ∞
0 2 2 ∞ 1 0 4 6
-2
3 ∞ −2 2 0 −3
1 4 ∞ ∞ ∞ ∞ 0
2 3
-1
Adjacency/cost matrix c[u, v]
4 -3 0
2 0
6 0
4 0
44 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 0 1 2 3 4
0 0 3 −1 ∞ ∞
2 1 2 0 1 2 ∞
0 2 2 ∞ 1 0 4 6
-2
3 ∞ −2 2 0 −3
1 4 ∞ ∞ ∞ ∞ 0
2 3
-1
Distance matrix D0 [u, v]
-3 D0 [1, 2] = min{∞, 2c[1,0] − 1c[0,1] } = 1
2 4
0
6 0
4 0
45 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 0 1 2 3 4
0 −1 5 ∞
0 3
2 1 2 0 1 2 ∞
0 2 2 3 1 0 3 6
-2
3 0 −2 −1 0 −3
1 4 ∞ ∞ ∞ ∞ 0
2 3
-1
Distance matrix D1 [u, v]
-3 D1 [0, 3] = min{∞, 3D0 [0,1] + 2D0 [1,3] } = 5
2 4
D1 [2, 3] = min{4, 1D0 [2,1] + 2D0 [1,3] } = 3
6 D1 [3, 2] = min{2, −2D0 [3,1] + 1D0 [1,2] } = −1
4 0
46 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 0 1 2 3 4
0 0 2 5
0 −1
2 1 2 0 1 2 7
0 2 2 3 1 0 3 6
-2
3 0 −2 −1 0 −3
1 4 ∞ ∞ ∞ ∞ 0
2 3
-1
Distance matrix D2 [u, v]
-3 D2 [0, 1] = min{3, −1D1 [0,2] + 1D1 [2,1] } = 0
2 4 D2 [0, 3] = min{5, −1D1 [0,2] + 3D1 [2,3] } = 2
6 D2 [0, 4] = min{∞, −1D1 [0,2] + 6D1 [2,4] } = 5
4 D2 [1, 4] = min{∞, 1D1 [1,2] + 6D1 [2,4] } = 7
47 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 0 1 2 3 4
0 0 −1 −1
0 2
2 1 2 0 1 2 −1
0 2 2 3 1 0 3 0
-2
3 0 −2 −1 0 −3
1 4 ∞ ∞ ∞ ∞ 0
2 3
-1
Distance matrix D3 [u, v]
-3 D3 [0, 4] = min{5, 2D2 [0,3] − 3D2 [3,4] } = −1
2 4
D3 [1, 4] = min{7, 2D1 [1,3] − 3D1 [3,4] } = −1
6 D3 [2, 4] = min{6, 3D1 [2,3] − 3D1 [3,4] } = 0
4 0
48 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 0 1 2 3 4
−1 −1
0 0 0 2
2 1 2 0 1 2 −1
0 2 2 3 1 0 3 0
-2
3 0 −2 −1 0 −3
1 4 ∞ ∞ ∞ ∞ 0
2 3
-1
Final distance matrix D ≡ D4 [u, v]
4 -3 0
2 0
6 0
4 0
49 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Theorem 6.12: At the bottom of the outer for-loop, for all nodes u and v,
D[u, v] contains the minimum length of all paths from u to v that are
restricted to using only intermediate nodes that have been seen in the
outer for-loop.
When algorithm terminates, all nodes have been seen and D[u, v] is the length
of the shortest u-to-v path.
Notation: Sk – the set of nodes seen after k passes through this loop; Sk -path
– one with all intermediate nodes in Sk ; Dk – the corresponding value of D.
Induction on the outer for-loop:
• Base case: k = 0; S0 = ∅, and the result holds.
• Induction hypothesis: It holds after k ≥ 0 times through the loop.
• Inductive step: To show that Dk+1 [u, v] after k + 1 passes
through this loop is the minimum length of an u-to-v Sk+1 -path.
50 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Inductive step: S
Suppose that x is the last node seen in the loop, so Sk+1 = Sk {x}.
• Fix an arbitrary pair of nodes u, v ∈ V (G) and let L be the
min-length of an u-to-v Sk+1 -path, so that obviously
L ≤ Dk+1 [u, v].
• To show that also Dk+1 [u, v] ≤ L, choose an u-to-v Sk+1 -path γ of
length L. If x ∈
/ γ, the result follows from the induction hypothesis.
• If x ∈ γ, let γ1 and γ2 be, respectively, the u-to-x and x-to-v
subpaths. Then γ1 and γ2 are Sk -paths and by the inductive
hypothesis,
L ≥ |γ1 | + |γ2 | ≥ Dk [u, x] + Dk [x, v] ≥ Dk+1 [u, v]
Non-negativity of the weights is not used in the proof, and Floyd’s algorithm
works for negative weights (but negative weight cycles should not be present).
51 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
a d
3
7
3 5
2 c f g
6 9
8
4 1
b e
3
52 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Initialisation: c(u,v)]
z
}| {
a 0 2 3 3 ∞ ∞ ∞
b
2 0 4 ∞ 3 ∞ ∞
c 3 4 0 5 1 6 ∞
D[u, v] u,v∈V (G) ← d
3 ∞ 5 0 ∞ 7 ∞
e
∞ 3 1 ∞ 0 8 ∞
f ∞ ∞ 6 7 8 0 9
g ∞ ∞ ∞ ∞ ∞ 9 0
a b c d e f g
for x ∈ V = {a, b, c, d, e, f, g} do
for u ∈ V = {a, b, c, d, e, f, g} do
for v ∈ V = {a, b, c, d, e, f, g} do
D[u, v] ← min {D[u, v], D[u, x] + D[x, v]}
end for
end for
end for
53 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g
a b c d e f
a 0 2 3 3 ∞ ∞ ∞
f f 4 5 3 ∞ ∞
2
b 0
c
3
4 0 5 1 6 ∞
e e
3
d 5 5 0 ∞ 7 ∞
e ∞ 3 1 ∞ 0 8 ∞
d d
f ∞ ∞ 6 7 8 0 9
g ∞ ∞ ∞ ∞ ∞ 9 0
c c | {z }
D[u, v] ← min {D[u, v], D[u, a] + D[a, v]} ;
b b (u, v) ∈ V 2
E.g.,
a a
D[b, d] ← min{D[b, d], D[b, a] + D[a, d]}
= min{∞, 2 + 3} = 5
a
54 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g
a b c d e f
a 0 2 3 3 5 ∞ ∞
f f 4 5 3 ∞ ∞
2
b 0
c
3
4 0 5 1 6 ∞
e e
3
d 5 5 0 8 7 ∞
e 5 3 1 8 0 8 ∞
d d
f ∞ ∞ 6 7 8 0 9
g ∞ ∞ ∞ ∞ ∞ 9 0
c c | {z }
D[u, v] ← min {D[u, v], D[u, b] + D[b, v]} ;
b b (u, v) ∈ V 2
E.g.,
a a
D[a, e] ← min{D[a, e], D[a, b] + D[b, e]}
= min{∞, 2 + 3} = 5
b
55 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g
a b c d e f
a 0 2 3 3 4 9 ∞
f f 4 5 3 10 ∞
2
b 0
c
3
4 0 5 1 6 ∞
e e
3
d 5 5 0 6 7 ∞
e 4 3 1 6 0 7 ∞
d d
f 9 10 6 7 7 0 9
g ∞ ∞ ∞ ∞ ∞ 9 0
c c | {z }
D[u, v] ← min {D[u, v], D[u, c] + D[c, v]} ;
b b (u, v) ∈ V 2
E.g.,
a a
D[a, f ] ← min{D[a, f ], D[a, c] + D[c, f ]}
= min{∞, 3 + 6} = 9
c
56 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g
a b c d e f
a 0 2 3 3 4 9 ∞
f f 4 5 3 10 ∞
2
b 0
c
3
4 0 5 1 6 ∞
e e
3
d 5 5 0 8 7 ∞
e 4 3 1 8 0 7 ∞
d d
f 9 10 6 7 7 0 9
g ∞ ∞ ∞ ∞ ∞ 9 0
c c | {z }
D[u, v] ← min {D[u, v], D[u, d] + D[d, v]} ;
b b (u, v) ∈ V 2
E.g.,
a a
D[a, f ] ← min{D[a, f ], D[a, d] + D[d, f ]}
= min{9, 3 + 7} = 9
d
57 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g
a b c d e f
a 0 2 3 3 4 9 ∞
f f 4 5 3 10 ∞
2
b 0
c
3
4 0 5 1 6 ∞
e e
3
d 5 5 0 8 7 ∞
e 4 3 1 8 0 7 ∞
d d
f 9 10 6 7 7 0 9
g ∞ ∞ ∞ ∞ ∞ 9 0
c c | {z }
D[u, v] ← min {D[u, v], D[u, e] + D[e, v]} ;
b b (u, v) ∈ V 2
E.g.,
a a
D[b, f ] ← min{D[b, f ], D[b, e] + D[e, f ]}
= min{9, 3 + 7} = 9
e
58 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g
a b c d e f
a 0 2 3 3 4 9 18
f f b 2 0 4 5 3 10 19
c
3 4 0 5 1 6 15
e e
d 3 5 5 0 8 7 16
e 4 3 1 8 0 7 16
d d
f 9 10 6 7 7 0 9
g 18 19 15 16 16 9 0
c c | {z }
D[u, v] ← min {D[u, v], D[u, f ] + D[f, v]} ;
b b (u, v) ∈ V 2
E.g.,
a a
D[a, g] ← min{D[a, g], D[a, f ] + D[f, g]}
= min{∞, 9 + 9} = 18
f
59 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
60 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
60 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
for k from 1 to n do
for i from 1 to n do
for j from 1 to n do
if D[i, j] > D[i, k] + D[k, j] then
D[i, j] ← D[i, k] + D[k, j]; Π[i, j] ← Π[k, j]
end if
end for
end for
end for
61 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 1 0 3 8 ∞ −4
2 ∞ 0 ∞ 1 7
3 4 (0)
D = 3
∞ 4 0 ∞ ∞
7 1 4 2 ∞ −5 0 ∞
8
1 3 5 ∞ ∞ ∞ 6 0
2
1 2 3 4 5
−5
−4 1 NIL 1 1 NIL 1
2 NIL
NIL NIL 2 2
Π(0) = 3
NIL 3 NIL NIL NIL
5 4 4 4 NIL 4 NIL NIL
6 5 NIL NIL NIL 5 NIL
62 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 1 0 3 8 ∞ −4
2 ∞ 0 ∞ 1 7
3 4 (1)
D = 3
∞ 4 0 ∞ ∞
7 1 4 2 5 −5 0 −2
8
1 3 5 ∞ ∞ ∞ 6 0
2
1 2 3 4 5
−5
−4 1 NIL 1 1 NIL 1
2 NIL
NIL NIL 2 2
Π(1) = 3
NIL 3 NIL NIL NIL
5 4 4 4 1 4 NIL 1
6 5 NIL NIL NIL 5 NIL
63 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 1 0 3 8 4 −4
2 ∞ 0 ∞ 1 7
3 4 (2)
D = 3
∞ 4 0 5 11
7 1 4 2 5 −5 0 −2
8
1 3 5 ∞ ∞ ∞ 6 0
2
1 2 3 4 5
−5
−4 1 NIL 1 1 2 1
2 NIL
NIL NIL 2 2
Π(2) = 3
NIL 3 NIL 2 2
5 4 4 4 1 4 NIL 1
6 5 NIL NIL NIL 5 NIL
64 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 1 0 3 8 4 −4
2 ∞ 0 ∞ 1 7
3 4 (3)
D = 3
∞ 4 0 5 11
7 1 4 2 −1 −5 0 −2
8
1 3 5 ∞ ∞ ∞ 6 0
2
1 2 3 4 5
−5
−4 1 NIL 1 1 2 1
2 NIL
NIL NIL 2 2
Π(3) = 3
NIL 3 NIL 2 2
5 4 4 4 3 4 NIL 1
6 5 NIL NIL NIL 5 NIL
65 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 1 0 3 −1 4 −4
2 3 0 −4 1 −1
3 4
D(4) 7
= 3 4 0 5 3
7 1 4 2 −1 −5 0 −2
8
1 3 5 8 5 1 6 0
2
1 2 3 4 5
−5
−4 1 NIL 1 4 2 1
2 4 NIL 4 2 1
Π(4) = 3
4 3 NIL 2 1
5 4 4 4 3 4 NIL 1
6 5 4 3 4 5 NIL
66 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 1 0 1 −3 2 −4
2 3 0 −4 1 −1
3 4
D(5) 7
= 3 4 0 5 3
7 1 4 2 −1 −5 0 −2
8
1 3 5 8 5 1 6 0
2
1 2 3 4 5
−5
−4 1 NIL 3 4 5 1
2 4 NIL 4 2 1
Π(5) = 3
4 3 NIL 2 1
5 4 4 4 3 4 NIL 1
6 5 4 3 4 5 NIL
67 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
algorithm PrintPath( Π, i, j )
if i = j then print i
else
if πi,j = NIL then print “no path from i to j”
else
PrintPath( Π, i, πi,j )
print j
end if
end if
68 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
1 NIL 3 4 5 1
2 4 NIL 4 2 1
Π(5) = 3
4 3 NIL 2 1
4 4 3 4 NIL 1
5 4 3 4 5 NIL
5 7
c 2 d
δ(s, s) = 0, δ(s, a) = 8, δ(s, b) = 9, δ(s, c) = 5, δ(s, d) = 7
DAG-Shortest-Paths(G , w , s)
(1) Initialize(G , s)
(2) Initialize array AdjR
(3) foreach u ∈ G .V
(4) foreach v ∈ G .Adj[u]
(5) Add u to AdjR [v ]
(6) Topologically sort the vertices of G
(7) foreach v ∈ G .V in topologically sorted order
(8) foreach u ∈ AdjR [v ]
(9) Relax(u, v , w )
6 1
t 5 s 2 a 7 b -1 c -2 d
3 4 2
5 7
c 2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a b
Q:sabcd 10
9
s 0 2 3 4 6
5 7
c 2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a b
Q:abcd 10
9
s 0 2 3 4 6
5 7
c 2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 10 b
Q:abcd 10
9
s 0 2 3 4 6
5 7
c 2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 10 b
Q:abcd 10
9
s 0 2 3 4 6
5 7
c 5
2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 10 b
Q:abd 10
9
s 0 2 3 4 6
5 7
c 5
2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 b
Q:abd 10
9
s 0 2 3 4 6
5 7
c 5
2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 14 b
Q:abd 10
9
s 0 2 3 4 6
5 7
c 5
2 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 14 b
Q:abd 10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 14 b
Q:ab 10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 13 b
Q:ab 10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 13 b
Q:b 10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 9 b
Q:b 10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 9 b
Q:∅ 10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u] 1
(6) Relax(u, v , w )
a 8 9 b
10
9
s 0 2 3 4 6
5 7
c 5
2
7 d
Single source shortest paths algorithms
General graphs
Dijsktra(G , w , s) // weights are non-negative
(1) Initialize(G , s)
(2) Build a priority queue Q on the vertices (keys are d values)
(3) while Q is not empty
(4) u ← ExtractMin(Q)
(5) foreach v ∈ G .Adj[u]
(6) Relax(u, v , w )
Relax(u, v , w )
(1) if u.d + w (u, v ) < v .d
(2) DecreaseKey(Q, v , u.d + w (u, v ))
(3) v .π ← u
Line 1: Θ(V )
Line 2: Θ(V )
Line 4: O(V log V )
Line 6: O(E log V )
Total: O((V + E ) log V )
Single source shortest paths algorithms
Correctness
s x y u
P1 P2
Single source shortest paths algorithms
Correctness
s x y u
P1 P2
Single source shortest paths algorithms