Lecture 19
Lecture 19
1200 A
C
1000
2500 B
Fig - 1
• In Fig-1 Edges AB & BC give the minimal spanning
tree to find the best routing from A to C. It is
normally known as minimum-cost spanning tree
Fig - 2
GROWING A MINIMUM
SPANNING TREE
• Let undirected graph G = (V, E)
E is a set of edges
V is a set of vertices
• Generic algorithm is a greedy algorithm which
grows the minimum spanning tree (MST), one edge
at a time
• The algorithm manages a set of edges A and it is a
subset of some minimum spanning tree
• At each step, we determine an edge (U, V) that can
be added to A without violating this invariant, in the
sense that AU {(U,V)} is also a subset of MST. Such
an edge is called a safe edge for A.
Growing a minimum spanning tree
GENERIC-MST (G, w)
A ← Ø {initialization}
While A does not form a spanning tree do
find an edge (u, v) that is safe for A Adds only safe
A ← AU {(u, v)} edges
return A {returns a minimum spanning tree A}
• Lines 2- 4 add only safe edges
• A contains all the safe edges forming a MST
• This greedy strategy is captured by the “generic” algorithm, which
grows the minimum spanning tree one edge at a time. The algorithm
manages a set of edges A, maintaining the following loop invariant
• Prior to each iteration, A is a subset of some minimum spanning tree.
We call such an edge a safe edge for A, since it can be safely added to
A.
• The tricky part is of course, finding a safe edge.
GROWING A MINIMUM SPANNING TREE
Working of the algorithm on a connected graph G = (V, E)
A = { } {A set is always acyclic}
{forest contains IVI trees, one for each
vertex. ;9 trees in this case}
While A does not form a spanning tree do IVI -1
times executes
a (a,b) safe edge
A = {(a,b)}
b (b,c) safe edge
A = {(a,b), (b,c)}
c (c,d) safe edge
A = {(a,b), (b,c), (c,d)}
c (c,f) safe edge
A = {(a,b), (b,c), (c,d), (c,f)}
c (c,i) safe edge
A = {(a,b), (b,c), (c,d), (c,f), (c,i)}
5
d (d,e) safe edge
A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e)}
f (f,g) safe edge
A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e), (f,g)}
g (g,h) safe edge
A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e), (f,g),
g,h)}
Now forest contains only 1 tree (9-8) so while loop is terminated
A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e), (f,g), (g,h)}
8 7
b c d
4 9
2
11 4 14 e
a i
7 6
8 10
h g f
1 2
a 7
4 1
11
i
8 7 b 6
c d
S 4
b
9
2
g
2
11 4 14 e 8
a i d
7 6
8 10 7
9 c
2
V-S h
1
g
2
f
e 14 4
10 f
(a) S V-S
(b)
• A cut (S, V-S) of an undirected graph G=(V, E) is a partition
of V. An edge (u, v) crosses the cut (S, V-S) if one of its
endpoints is in S and the other in V-S. An edge is a light
edge crossing the cut (S, V-S) if its weight is the minimum
of any edge crossing the cut. A cut respects a set A of edges
if no edge in A crosses the cut.
• Figure shows two ways of viewing a cut (S,V-S) of the
graph.
• In Figure (a), the vertices in the set S are shown in black and
those in V-S are shown in white.
• The edges crossing the cut are those connecting white
vertices with black vertices. The edge (d, c) is the unique
light edge crossing the cut.
• A subset A of the edges is shaded; note that the cut (S,V-S)
respects A, since no edge of A crosses the cut.
• Figure (b) shows the same graph with the vertices in the set
S on the left and the vertices in the set V-S on the right. An
edge crosses the cut if it connects a vertex on the left with a
vertex on the right.
Kruskal’s algorithm
• In Kruskal’s algorithm, the set A is a forest. The safe edge added
to A is always a least-weight edge in the graph that connects two
distinct components.
• Kruskal’s algorithm is based directly on the generic minimum-
spanning tree algorithm.
• It finds a safe edge to add to the growing forest by finding, of all
the edges that connect any two trees in the forest, an edge (u, v)
of least weight.
• Kruskal’s algorithm is a greedy algorithm, because at each step it
adds to the forest an edge of least possible weight.
• We can determine whether two vertices u and v belong to the
same tree by testing whether FIND-SET (u) equals FIND-SET
(v).
• The combining of trees is accomplished by the UNION procedure
MST-KRUSKAL (G, w)
A ← Ø {empty-initialization}
for each vertex v ε V[G] do
Make-Set (v) {creates a set of V trees-initialization}
Sort the edges of E into nondecreasing order by weight w {0(E
lg E)}
for each edge (u, v) ε E, taken in nondecreasing order by
weight do
if FIND-SET(u) ≠ FIND-SET(v) then
{if endpoints u & v belong to the same tree, edge is discarded
otherwise it is added to A as two trees are merged}
A ← A U {(u, v)} {edge is added in A}
union (u, v) {the vertices in the two trees are merged}
return A
EXECUTION OF KRUSKAL’S ALGORITHM
1. A={}
2. V[G] = {a,b,c,d,e,f,g,h, i}
E(G) = (a,b), (a,h), (b,c), (b,h), (c,i), (c,d), (c,f), (d,e),
(d,f), (f,g), (f,e), (g,h), (g,i), (h,i)
3. |V| Trees {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}
(1) (2) (2) (4) (4)
4. Sorted Set E = {g,h}, {c,i}, {f,g}, {a,b}, {c,f}
(6) (7) (7) (8) (8) (9) (10) (11) (14)
8 7 8 7
b c d b c d
4 9 4 9
2 2
(c) a 11 i 4 14 e (d) a 11 i 4 14 e
8 7 6 8 7 6
10 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
(e) a 11 i 4 14 e
(f) a 11 i 4 14 e
8 7 6 8 7 6
10 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
(g) a 11 i 4 14 e (h) a 11 i 4 14 e
8 7 6 8 7 6
10 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
(i) a 11 i 4 14 e (j) a 11 i 4 14 e
7 6 7 6
8 10 8 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
4 4
(k) a 11 i 14 e
(l)
a 11 i 14 e
7 6 7 6
8 10 8 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
(m) a 11 i 4 14 e
(n) a 11 i 4 14 e
7 6 7 6
8 10 8 10
h g f h g f
1 2 1 2
Prim’s algorithm
• In Prim’s algorithm, the set A forms a single tree. The safe
edge added to A is always a least-weight edge connecting
the tree to a vertex not in the tree.
• Prim’s algorithm has the property that the edges in the set
always form a single tree.
• The tree starts from an arbitrary root vertex r and grows until
the tree spans all the vertices in V.
• At each step, a light edge is added to the tree A that connects
A to an isolated vertex GA=(V, A). This rule adds only edges
that are safe, therefore, when the algorithm terminates, the
edges in A form a minimum spanning tree.
• This strategy is greedy since the tree is augmented at each
step with an edge that contributes the minimum amount
possible to the tree’s weight.
The root vertex is a, shaded edges are in
the tree being grown, and the vertices in the
tree are shown in black. At each step of the
algorithm, the vertices in the tree determine
a cut of the graph, and a light edge crossing
the cut is added to the tree. In the second
step, for example, the algorithm has a
choice of adding either edge (b, c) or edge
(a, h) to the tree since both are light edges
crossing the cut.
Prim’s Algorithm
MST-PRIM (G, w, r)
for each u ε V[G] do
key [u] ← ∞ {sets the key of each vertex to ∞ except r}
∏[u] ← NIL {sets the parent of each vertex to NIL}
key [r] ← 0 {key of the root r is set to 0 – first vertex to be processed}
Q ← V[G] {initialises Q to contain all the vertices}
while Q ≠ Ø do
u ← EXTRACT-MIN(Q) {Identified a vertex u ε Q incident on a light edge
crossing the cut (V-Q,Q) with the exception of the first iteration where u = r.
Removing u from the set Q adds it to the set V-Q of vertices in the tree,
adding (u, ∏[u]) to A.}
for each v ε Adj [u] do {every vertex v adjacent to u but not in the tree}
if v ε Q and w(u, v) < key [v] then {weight of edge is less than key[v]}
∏[v] ← u {names the parent of v}
key [v] ← w (u, v) {minimum of edge connecting to v is w(u,v)}
Execution of Prim’s Algorithm
• Root vertex = r
• Graph = G
• Weight = w
• Min-priority queue = Q
(keeps all vertices which are not in tree)
• Key[v] is the min weight of an edge connecting v to a vertex in
the tree
• Key [v] = ∞ if there is no such edge
• ∏ [v] field names the parent of V in the tree
• The set A is kept implicitly as:
– A = {(v, ∏[v])} : v ∈ V – {r} – Q}
– When the algorithm terminates, the min-priority queue Q is
empty so the min spanning tree A for G is thus
A = {(v, ∏[v]) : v ∈ V – (r)}
Execution of Prim’s Algorithm
• Root vertex = r
• Graph = G
• Weight = w
• Min-priority queue = Q
(keeps all vertices which are not in tree)
• Key[v] is the min weight of an edge connecting v to a vertex in
the tree
• Key [v] = ∞ if there is no such edge
• ∏ [v] field names the parent of V in the tree
• The set A is kept implicitly as:
– A = {(v, ∏[v])} : v ∈ V – {r} – Q}
– When the algorithm terminates, the min-priority queue Q is
empty so the min spanning tree A for G is thus
A = {(v, ∏[v]) : v ∈ V – (r)}
EXECUTION OF
PRIM’S ALGORITHM
For Loop
V{G} = {a,b,c,d,e,f,g,h,i}
key[a] = ∞, key [b] = ∞, key [c] = ∞ As no vertex has
been connected to
key[d] = ∞, key [e] = ∞, key[f] = ∞ tree
key[g] = ∞, key[h] = ∞, key [i] = ∞
parent of [a] = Nil, parent of [b] = Nil,
parent of [c] = Nil, parent of [d] = Nil,
parent of [e] = Nil, parent of [f] = Nil,
parent of [g] = Nil, parent of [h] = Nil,
parent of [i] = Nil,
key [a] = 0 {not vertex}
Vertices in G = Q = {a,b,c,d,e,f,g,h,i}
while Q ≠ φ u Adj {u} For each V ε Adj u
if ∏{V} Key {V}
1 T a b,h T a key[b] = 4, key[h] = 8
2 T b c,h T b key[c] = 8
3 T c d,f,i T c key[d] = 7, key[f] = 4, key[i] = 2
4 T i g,h T i key[g] = 6, key [h] = 7
5 T f d,e,g T f key[e] = 10, key [g] = 2
6 T g f,h,i T g key [h] = 1
7 T h a,b,g,i F - -
8 T d c,e,f T d key [e] = 9
9 T e d,f F - -
8 10 8 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
4 4
(c) a 11 i 14 e
(d) a 11 i 14 e
8 7 6 8 7 6
10 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
(e) a 11 i 4 14 e
(f) a 11 i 4 14 e
8 7 6 8 7 6
10 10
h g f h g f
1 2 1 2
8 7 8 7
b c d b c d
4 9 4 9
2 2
4 4
(g) a 11 i 14 e
(h) a 11 i 14 e
8 7 6 8 7 6
10 10
h g f h g f
1 2 1 2
8 7
b c d
4 9
2
(i) a 11 i 4 14 e
8 7 6
10
h g f
1 2
DIJKSTRA’S ALGORITHM
• It solves the single-source shortest-paths
problem on a weighted, directed graph
G = (V,E) for the case in which all edge
weights are non-negative.
∴ w(u,v) > 0 for each edge (u,v) ε E
• It maintains a set S of vertices whose final
shortest-path weights from the source s
have already been determined.
• It repeatedly selects the vertex u ε V – S with
the minimum shortest-path estimate, adds u
to S and relaxes all edges leaving u.
• Q is the minimum-priority queue of vertices,
keyed by their d values.
DIJKSTRA’S ALGORITHM
Diskstra (G, w, s)
Initialize-Single-Source(G,s) {Initializes d and Π values}
s ← φ {Initializes the set S to empty set}
Q ← V [G] {Q contains all the vertices in V}
While Q ≠ φ do {Repeat until Q is not empty, iterates |V| times}
u ← Extract-min(Q) {maintains Q = V-S at the start of
each iteration; u vertex, shortest-path estimate, is extracted from
Q = V-S}
S ← SU{u} {vertex u is added to S}
for each vertex v ε Adj{u} do
Relax (u,v,w) {for each adjacent vertex of u, relaxes each
edge leaving u and updates d[v] and Π{V} values if the
shortest-path to v can be improved by going through u}
DIJKSTRA’S ALGORITHM
Initialize-Single-Source (G,s)
for each vertex v ε V [G] do
d [v] ← ∞ {initialization of shortest path estimate}
Π[v] ← Nil {sets the parent of each vertex to nil}
d [s] ← 0 {sets the shortest path estimate for
source vertex}
2
5 6
u v
2
6>5+2 F => 5 6
unchanged by relaxation
EXECUTION OF DIJKSTRA’S ALGORITHM
(Weighted& directed graph)
V = {s, t, x, z, y}
d [s] = ∞, 0 d [t] = ∞, 10, 8
d [x] = ∞, 14, 13 d [z] = ∞, 7
d [y] = ∞, 5
Π [s] = Nil Π [t] = Nil,s,y Π[x] = Nil, y,z,t
Π[z] = Nil, y Π [y] = Nil,s
s={}
Q = {s,t,x,z,y} Q = V-S
while Q = φ u S Adj Relax (u,v,w)
(Extract-min(Q)) [u] if d[V] Π [V]
1 T s {s} t T 10 s
y T 5 s
2 T y {s,y} t T 8 y
x T 14 y
z T 7 y
3 T z {s,y,z} x T 13 z
4 t {s,y,z,t} x T 9 t
5 x {s,y,z,t,x} - - - -
End of while loop
As Dijkstra’s algorithm always chooses the “lightest” or “closest”
vertex in v-s to add to set S, so it uses a greedy strategy.
t x t x
1 1
∞ ∞ 10 ∞
10 10
9 9
2 3 2 3
s 0 4 6 s 0 4 6
7 7
5 5 ∞
∞ ∞ 5
2 2
y z y z
(b)
(a)
t x 5 t x
1 1
8 14 8 13
10 10
9 9
2 3 2 3
s 0 4 6 s 0 4 6
7 7
5 5 5 7
5 7 2
2 y z
y z
(d)
(c)
t x
1
8 9
10
9
2 3
s 0 4 6
7
5 5 7
2
y z
(e)
t x t x
6 6
3 9 3 9
3 3
4 4
2 1 2 1
s 0 2 7 s 0 2 7
3 3
5 5
5 11 5 11
6 6
y z y z
(a) (b)
t x
6
3 9
3
4
2 1
s 0 2 7
3
5
5 11
6
y z
(c)
Figure (a) A weighted, directed graph with shortest-path weights from source s.
(b) The shaded edges form a shortest-paths tree rooted at the sources s.
(c) Another shortest-paths tree with the same root.
Bellman-Ford
Algorithm
Bellman-Ford Algorithms
-2
5
6
-3
8
-4 7
7 2
9
Bellman-Ford Algorithm –
Example 2
-2
∞ 5 ∞
6
-3
8
0 7
-4
7 2
∞ 9 ∞
Bellman-Ford Algorithm –
Example 2
-2
6 5 ∞
6
-3
8
0 7
-4
7 2
7 9 ∞
Bellman-Ford Algorithm –
Example 2
-2
6 5 4
6
-3
8
0 7
-4
7 2
7 9 2
Bellman-Ford Algorithm –
Example 2
-2
2 5 4
6
-3
8
0 7
-4
7 2
7 9 2
Bellman-Ford Algorithm –
Example 2
-2
2 5 4
6
-3
8
0 7
-4
7 2
7 9 -2
Bellman-Ford Algorithm -
Complexity
InitializeSingleSource TI(V,E) = Θ(V)
SSSP-BellmanFord
R2
R1 R4
R3
Implementation
Bellman-Ford Example
40
20 ∞ ∞
-30
-10 20
Source 0 -10
50
70 ∞ ∞
-40
-60
Bellman-Ford Example
40
20 20 ∞
-30
-10 20
Source 0 -10
50
70 ∞ ∞
-40
-60
Bellman-Ford Example
40
20 ∞ ∞
-30
-10 20
Source 0 -10
50
70 ∞ ∞
-40
-60
Bellman-Ford Example
40
20 20 ∞
-30
-10 20
Source 0 -10
50
70 ∞ ∞
-40
-60
Bellman-Ford Example
40
20 20 60
-30
-10 20
Source 0 -10
50
70 ∞ ∞
-40
-60
Bellman-Ford Example
40
20 20 60
-30
-10 20
Source 0 -10
50
70 50 ∞
-40
-60
Bellman-Ford Example
40
20 20 60
-30
-10 20
Source 0 -10
50
70 50 100
-40
-60
Bellman-Ford Example
40
20 20 60
-30
-10 20
Source 0 -10
50
70 50 70
-40
-60
Bellman-Ford Example
40
20 20 60
-30
-10 20
Source 0 -10
50
70 30 70
-40
-60
Bellman-Ford Example
40
20 20 50
-30
-10 20
Source 0 -10
50
70 30 70
-40
-60
The algorithm
eventually accumulates
network distances so
that it can maintain a
database of network
topology information.
Distance-vector
algorithms do not allow
a router to know the
exact topology of an
internetwork