Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
GREEDY ALGORITHMS• Greedy algorithms are typically used to
solve optimisation problemssolve optimisation problems• Greedy algorithms are usually the most
straightforward, take decisions on the basisstraightforward, take decisions on the basisof information immediately at hand withoutworrying about the effect these decisionsmay have in the future
• They are easy to invent and easy toimplement
• Many problems cannot be solved by thesealgorithms due to their simplicity
ALGORITHM FOR MAKING CHANGE
• Coins available are : 100 cents, 25 cents, 10 cents,5 cents, and 1 cent,
• Devise an algorithm for paying a given amount to acustomer using the smallest number of coins i.e.,To pa $ 2 89To pay $ 2.89
100 cent coins = 2 = 2.0025 cent coins = 3 = 0 7525 cent coins = 3 = 0.7510 cent coins = 1 = 0.101 cent coins = 4 = 0.04
Total US$ = 2.89• Algorithm will provide optimal solution.
MAKING CHANGE• Algorithm is greedy because at every step it
chooses the largest coins it can, without worryingg , y gwhether this will prove to be a sound decision inthe long runIt ne er changes its decision once the coins ha e• It never changes its decision once the coins havebeen included in the solution
• A spanning tree of a graph g is a subgraph that is aA spanning tree of a graph g is a subgraph that is atree containing all the vertices (nodes) of g. In thecase of a connected network we may want to find a
i i l i t i t ith thminimal spanning tree, a spanning tree with thesmallest possible sum of edge values of the edgesin the tree.
Example
A
2800D
A1200
C
B2500
1000
B2500Fig - 1
• In Fig-1 Edges AB & BC give the minimal spanningIn Fig 1 Edges AB & BC give the minimal spanningtree to find the best routing from A to C. It isnormally known as minimum-cost spanning tree
Let undirected graph => G = (V, E)where V is the set of verticeswhere V is the set of vertices
E is the set of edges• T is acyclic tree and connects all the vertices and itT is acyclic tree and connects all the vertices and it
spans the graph G. It is also called minimum-weightspanning tree. Spanning tree have IVI – I edges.Another spanning tree is shown in Fig 2Another spanning tree is shown in Fig - 2.
b c d8 7
b
i
c d
e112
4
4
14
9
a i e
7 6
4
8
14
10
h g f1 2
Fig - 2
GROWING A MINIMUM SPANNING TREESPANNING TREE
• Let undirected graph G = (V, E)E is a set of edgesE is a set of edgesV is a set of vertices
• Generic algorithm is a greedy algorithm whichGeneric algorithm is a greedy algorithm whichgrows the minimum spanning tree (MST), one edgeat a timeTh l ith t f d A d it i• The algorithm manages a set of edges A and it is asubset of some minimum spanning tree
• At each step, we determine an edge (U, V) that canAt each step, we determine an edge (U, V) that canbe added to A without violating this invariant, in thesense that AU {(U,V)} is also a subset of MST. Suchan edge is called a safe edge for Aan edge is called a safe edge for A.
Growing a minimum spanning treeGENERIC-MST (G, w)
A ← Ø {initialization}While A does not form a spanning tree doWhile A does not form a spanning tree do
find an edge (u, v) that is safe for AA ← AU {(u, v)}
return A {returns a minimum spanning tree A}
Adds only safe 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• 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. W ll h d f d f A i it b f l dd d tWe 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 TREEWorking of the algorithm on a connected graph G = (V E)Working of the algorithm on a connected graph G = (V, E)
A = { } {A set is always acyclic}{forest contains IVI trees, one for eachvertex. ;9 trees in this case}
While A does not form a spanning tree do IVI -1times executestimes executesa (a,b) safe edge
b (b,c) safe edgec (c,d) safe edge
A = {(a,b)}A = {(a,b), (b,c)}A = {(a,b), (b,c), (c,d)}c (c,d) safe edge
c (c,f) safe edgec (c,i) safe edged (d e) safe edge
A {(a,b), (b,c), (c,d)}A = {(a,b), (b,c), (c,d), (c,f)}A = {(a,b), (b,c), (c,d), (c,f), (c,i)}A = {(a b) (b c) (c d) (c f) (c i) (d e)}
5d (d,e) safe edgef (f,g) safe edgeg (g,h) safe edge
A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e)}A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e), (f,g)}A = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e), (f,g),
h)}g,h)}Now forest contains only 1 tree (9-8) so while loop is terminatedA = {(a,b), (b,c), (c,d), (c,f), (c,i), (d,e), (f,g), (g,h)}
b c d8 7
4 9
a i
f
e11
7 6
24
8
14
10h g f
1 2
• A minimum spanning tree for a connected graph.• The weights on edges are shown, and the edges in a
minimum spanning tree are shaded.• The total weight of the tree shown is 37.• This minimum spanning tree is not unique: removing
the edge (b, c) and replacing it with the edge (a, h)yields another spanning tree with weight 37.
b c d
2
8 7
4 9
a
h
i
g f
e11
7 6
24
8
14
10h g f
1 2
h8
i
a
b
1114
7
b
i
c d
e11 24
8 7
4
14
9 g
b
d
2
8
6S
a
h
i
g f
e7
1 2
64
8
14
10 c
d
279
V-S
f
e 14
10
4
S V-S(a)
(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
d i i i S d h h i V S A d i li hendpoints 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 cutif 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• 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 uniquevertices 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 cutrespects 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 aedge 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 twodistinct components.
• Kruskal’s algorithm is based directly on the generic minimum-spanning tree algorithm.spanning tree algorithm.
• It finds a safe edge to add to the growing forest by finding, of allthe edges that connect any two trees in the forest, an edge (u, v)of least weightof least weight.
• Kruskal’s algorithm is a greedy algorithm, because at each step itadds to the forest an edge of least possible weight.
• We can determine whether two vertices u and v belong to thesame 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}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
i h dweight doif FIND-SET(u) ≠ FIND-SET(v) then
{if endpoints u & v belong to the same tree, edge is discarded { p g , gotherwise 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}u o (u, v) { e ve ces e wo ees a e e ged}
return A
EXECUTION OF KRUSKAL’S ALGORITHM1. 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}3. |V| Trees {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}
4 Sorted Set E = {g h} {c i} {f g} {a b} {c f}(1) (2) (2) (4) (4)
4. Sorted Set E = {g,h}, {c,i}, {f,g}, {a,b}, {c,f}
{g i} {c d} {h i} {a h} {b c} {d e} {f e} {b h} {d f}(6) (7) (7) (8) (8) (9) (10) (11) (14)
{g,i}, {c,d}, {h,i}, {a,h},{b,c}, {d,e}, {f,e}, {b,h}, {d,f}
Edge Processed
Connected Components / Collection of Disjoint Sets
Initial Sets {a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}(g,h) {a}, {b}, {c}, {d}, {e}, {f}, {g,h}, {i}(c,i) {a}, {b}, {c,i}, {d}, {e}, {f}, {g,h}(f,g) {a}, {b}, {c,i}, {d}, {e}, {f,g,h}(a,b) {a,b} {c,i}, {d}, {e}, {f,g,h}(c f) {a b} {c f g h i} {d} {e}(c,f) {a,b}, {c,f,g,h,i}, {d}, {e}(g,i) discarded(c,d) {a,b}, {c,d,f,g,h,i}, {e}(h,i) discarded (a,h) {a,b,c,d,f,g,h,i}, {e}(b,c) discarded(d,e) {a,b,c,d,e,f,g,h,i}(f,e) discarded(b h) discarded(b,h) discarded(d,f) discarded
A = {(g,h), (c,i), (f,g), (a,b), (c,f), (c,d), (a,h), (d,e)}
• Figure shows the execution of Kruskal’sgalgorithm on the graph. Shaded edges belongto the forest A being grown. The edges areconsidered by the algorithm in sorted orderby weight.
• An arrow points to the edge underconsideration at each step of the algorithm.consideration at each step of the algorithm.
• If the edge joins two distinct trees in theforest it is added to the forest therebyforest, it is added to the forest, therebymerging the two trees
b c d8 7
4 9b c d
8 7
4 9
Execution of Kruskal’s Algorithm
a
h
i
g f
e117
1 2
6
24
4
814
9
10
a
h
i
g f
e117
1 2
6
24
4
814
9
10
(a) (b)
a
b
i
c d
e112
4
8 7
4
14
9
a
b
i
c d
e112
4
8 7
4
14
9
(c) (d)
h g f
7
1 2
68 10h g f
7
1 2
68 10
a
b
i
c d
f
e117 6
24
8 7
4
814
9
10
a
b
i
c d
f
e117 6
24
8 7
4
814
9
10
(e) (f)
h g f1 2
b c d
2
8 7
4 9
h g f1 2
b c d
2
8 7
4 9
a
h
i
g f
e117
1 2
6
24
814
10
a
h
i
g f
e117
1 2
6
24
814
10
(g) (h)
8 7 8 7
a
b
h
i
g
c d
f
e117 6
24
8 7
4
8
14
9
10
a
b
h
i
g
c d
f
e117 6
24
8 7
4
8
14
9
10
(i) (j)
h g f1 2
b c d
2
8 7
4 9
h g f1 2
b c d
2
8 7
4 9
a
h
i
g f
e117
1 2
6
24
8
14
10
a
h
i
g f
e117
1 2
6
24
8
14
10
(k) (l)
a
b
i
c d
e112
4
8 7
4
14
9
a
b
i
c d
e112
4
8 7
4
14
9
(m) (n)
h g f
7
1 2
68 10
h g f
7
1 2
68 10
( )
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 connectingthe tree to a vertex not in the tree.
• Prim’s algorithm has the property that the edges in the setalways form a single tree.y g
• The tree starts from an arbitrary root vertex r and grows untilthe tree spans all the vertices in V.
• At each step a light edge is added to the tree A that connects• At each step, a light edge is added to the tree A that connectsA to an isolated vertex GA=(V, A). This rule adds only edgesthat are safe, therefore, when the algorithm terminates, theedges in A form a minim m spanning treeedges in A form a minimum spanning tree.
• This strategy is greedy since the tree is augmented at eachstep with an edge that contributes the minimum amountpossible to the tree’s weight.
The root vertex is a shaded edges are inThe root vertex is a, shaded edges are inthe tree being grown, and the vertices in thetree are shown in black At each step of thetree are shown in black. At each step of thealgorithm, the vertices in the tree determinea cut of the graph and a light edge crossinga cut of the graph, and a light edge crossingthe cut is added to the tree. In the secondstep for example the algorithm has astep, for example, the algorithm has achoice of adding either edge (b, c) or edge(a h) to the tree since both are light edges(a, h) to the tree since both are light edgescrossing the cut.
Prim’s AlgorithmPrim’s AlgorithmMST-PRIM (G, w, r)
for h V[G] dofor each u ε V[G] dokey [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.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]}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 y[ ] g g g
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 y[ ] g g g
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 ALGORITHMPRIM S ALGORITHM
For LoopV{G} = {a,b,c,d,e,f,g,h,i}V{G} {a,b,c,d,e,f,g,h,i}
key[a] = ∞, key [b] = ∞, key [c] = ∞key[d] = ∞, key [e] = ∞, key[f] = ∞
As no vertex has been connected totree
key[g] = ∞, key[h] = ∞, key [i] = ∞parent of [a] = Nil, parent of [b] = Nil,parent of [c] = Nil parent of [d] = Nil
tree
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}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 f h i T k [h] 16 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] = 98 T d c,e,f T d key [e] = 9
9 T e d,f F - -
hil l t i t dwhile loop terminatedA = |V-Q| = vertices in the MST = {a,b,c,i,f,g,h,d,e}
a
b
i
c d
e117 6
24
8 7
4
8
14
9
10
a
b
i
c d
e117 6
24
8 7
4
8
14
9
10
(a) (b)
h g f1 2
b c d
24
8 7
4 9
h g f1 2
b c d
24
8 7
4 9
a
h
i
g f
e117
1 2
6
4
8
14
10
b d8 7
a
h
i
g f
e117
1 2
6
4
8
14
10
b d8 7
(c) (d)
a
b
h
i
g
c d
f
e117 6
24
4
8
14
9
10
a
b
h
i
g
c d
f
e117 6
24
4
8
14
9
10
(e) (f)
f1 2
a
b
i
c d
e112
4
8 7
4
14
9
f1 2
a
b
i
c d
e112
4
8 7
4
14
9
(g) (h)
h g f
7
1 2
68 10h g f
7
1 2
68 10
b c d8 7
4 9
(g) ( )
a
h
i
g f
e117
1 2
6
24
8
14
10
(i)
DIJKSTRA’S ALGORITHM• It solves the single-source shortest-paths
problem on a weighted, directed graphp g , g pG = (V,E) for the case in which all edgeweights are non-negative.
( ) 0 f h d ( ) E∴ 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 sshortest-path weights from the source shave already been determined.
• It repeatedly selects the vertex u ε V – S withIt repeatedly selects the vertex u ε V S withthe minimum shortest-path estimate, adds uto S and relaxes all edges leaving u.
• Q is the minimum-priority queue of vertices,keyed by their d values.
DIJKSTRA’S ALGORITHMDiskstra (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}While Q ≠ φ do {Repeat until Q is not empty, iterates |V| times}
u ← Extract-min(Q) {maintains Q = V-S at the start ofeach iteration; u vertex, shortest-path estimate, is extracted fromQ V S}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 eachedge leaving u and updates d[v] and Π{V} values if theshortest-path to v can be improved by going through u}shortest path to v can be improved by going through u}
DIJKSTRA’S ALGORITHMInitialize-Single-Source (G,s)
for each vertex v ε V [G] doo eac e e ε [G] dod [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}
Relax (u,v,w) {may decrease the value of the shortestpath estimate d[v] and update v’sp [ ] p
predecessor field Π(v)}If d[v] > d[u] + w(u,v) then
d[v] ← d[u] + w(u,v)Π[v] ← u
DIJKSTRA’S ALGORITHMu v u v5 9 5 72 25 9 5 7
changed by relaxation
2
9 > 5 + 2 T => d[v] = 5 + 2 = 7parent of v is u
5 625 6u v
6 5 2 F 5 626 > 5 + 2 F => 5 6
unchanged by relaxation
EXECUTION OF DIJKSTRA’S ALGORITHM(Weighted& directed graph)(Weighted& directed graph)
V = {s, t, x, z, y}d [s] = ∞ 0 d [t] = ∞ 10 8d [s] = ∞, 0 d [t] = ∞, 10, 8d [x] = ∞, 14, 13 d [z] = ∞, 7d [y] = ∞, 5Π [s] = Nil Π [t] = Nil,s,y Π[x] = Nil, y,z,t[ ] [ ] , ,y [ ] , y, ,Π[z] = Nil, y Π [y] = Nil,ss = { }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 { } t T 101 T s {s} t T 10 s
y T 5 s
2 T y {s y} t T 8 y2 T y {s,y} t T 8 y
x T 14 y
z T 7 yz T 7 y
3 T z {s,y,z} x T 13 z
4 t {s,y,z,t} x T 9 t{ y }
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.
∞1t x
∞ 101t x
∞
0 4 6329
7
10
s 0 4 6329
75
10
s
25
zy∞∞ 2
5
zy∞
(a) (b)
5
58
1
910
t x14 8
1
910
t x13
0 4 6329
75
s
75
0
2
4 6329
75
s
752
zy2
zy
(c) (d)
81
10
t x9
0 4 632
9
75
10
s
752
5
zy75
(e)
3 96t x
3 96t x
0
9
2 7124
3
3
s 0
9
2 7124
3
3
s
5 116
5
zy
5 116
5
zy
(a) (b)
3 96
124
3
t x(a) (b)
5
0
116
2 712
35
s
zy
(c)
Figure (a) A weighted, directed graph with shortest-path weights from source s. g ( ) g , g p p g(b) The shaded edges form a shortest-paths tree rooted at the sources s. (c) Another shortest-paths tree with the same root.
BellmanBellman FordFordBellmanBellman--Ford Ford AlgorithmAlgorithm
BellmanBellman--Ford AlgorithmsFord Algorithmsgg
• Simple and efficient algorithm to findSimple and efficient algorithm to find the shortest paths
• Shortest Path is from a single sourcevertex to all other vertexesvertex to all other vertexes
• Used for a weighted, directed graphg , g p
• Weights may be negative
BellmanBellman--Ford AlgorithmsFord Algorithms• Bellman-Ford algorithm returns a Boolean
value indicating whether or not there is avalue indicating whether or not there is a negative-weight cycle that is reachable from the source
• If there is such cycle, the algorithm indicates that no solution exists
• If there is no such cycle, the algorithm produces the shortest paths and their weights
• Like Dijkstra’s algorithm, the Bellman-Ford algorithm uses the technique of relaxation
OperationOperationOperation Operation • The algorithm initializes the distance
to the source vertex to 0 and all other vertexes to ∞
• It then does V-1 passes (V is the number of vertexes) over all edges) grelaxing, or updating, the distance to the destination of each edgeg
• Finally it checks each edge again to detect negative weight cycles, indetect negative weight cycles, in which case it returns false
BellmanBellman--Ford Algorithm Ford Algorithm -- OperationOperation• Initialize d and Π• For each vertex, v, in V• d[v] = ∞ (is the shortest path estimate)
• Π[v]= Nil (is the predecessor)[ ] ( p )• Source distance, d[s] = 0• Repeat the following process |V| – 1 times:• For each edge (u v) in E relax on the edge (u v)• For each edge (u, v) in E, relax on the edge (u, v)• Check for the negative cycle:• If there exists an edge (u, v) such that
d[v] > d[u] + w[u,v]then there exists a negative cycle containing (u, v)there exists a negative cycle containing (u, v)
• Initial estimates are all ∞• No connections
AlgorithmBellmanFord(graph (G,w), vertex s)
I iti li Si l S (G )InitializeSingleSource(G, s)for i ← 1 to |V[G] − 1|do for each edge(u,v) ∈ E[G]
do Relax(u,v,w)For each vertex v ∈ V[G]
for each edge(u,v) ∈ E[G]do if d[v] > d[u] + w(u,v)
do d[v] ←∞[ ] [ ] ( , )
then return falsereturn true
Π[v] ←Nilreturn trued[s] ←0
Algorithm
BellmanFord(graph (G,w), vertex s)InitializeSingleSource(G, s)for i ← 1 to |V[G] − 1|| [ ] |do for each edge(u,v) ∈ E[G]
do Relax(u,v,w)do Relax(u,v,w)for each edge(u,v) ∈ E[G]
do if d[v] > d[u] + w(u v)do if d[v] > d[u] + w(u,v)then return false
t treturn true
RelaxationRelaxation
• For Each Vertex v Є V d[v] is maintainedmaintained
• d[v] is a shortest path estimate• Relax(u,v,w)
If d[v] > d[u]+w(u,v)If d[v] d[u] w(u,v)then d[v] ←d[u]+w(u,v)
Π[ ]Π[v] ←u
RelaxationRelaxationRelaxationRelaxation
• Progressively decreasing an estimate d[v] on the weight of a shortest path from the g psource s to each other vertex v Є V until it achieves the actual shortest-path weightachieves the actual shortest path weight
• The algorithm returns TRUE if and only if the graph contains no negative weightthe graph contains no negative-weight cycles that are reachable from the source.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
RunningRunning--Time Time
Each of the |V| 1 passes• Each of the |V|-1 passes: O(E)O(E)
• “For” loop: O(E)For loop: O(E)
• Overall-running-time: O(VE)Overall running time: O(VE)
Bellman-Ford Algorithm –Example 2
6 5-2
6
7-38
47
72
-4
9
Bellman-Ford Algorithm –Example 2
∞ ∞6 5-2
0
6
7-38
4
∞ ∞7
72
-4
9
Bellman-Ford Algorithm –Example 2
6 ∞6 5-2
0
6
7-38
4
7 ∞7
72
-4
9
Bellman-Ford Algorithm –Example 2
6 46 5-2
0
6
7-38
4
7 27
72
-4
9
Bellman-Ford Algorithm –Example 2
2 46 5-2
0
6
7-38
4
7 27
72
-4
9
Bellman-Ford Algorithm –Example 2
2 46 5-2
0
6
7-38
4
7 -27
72
-4
9
Bellman-Ford Algorithm -Complexity
I iti li Si l S T (V E) (V)InitializeSingleSource TI(V,E) = Θ(V)
Relax TR(V,E) = Θ(1)?R( , ) ( )
SSSP-BellmanFord
T(V,E) = TI(V,E) + V E TR(V,E) + E == Θ(V) + V E Θ(1) + E = ( ) ( )= Θ(V E)
what route to take?“B”“A”
R2
R1
2
R
R4
R3
ImplementationImplementationImplementationImplementation
Bellman-Ford ExampleBellman Ford Example
40
0
∞ ∞20
20-10
-30
-10Source
∞ ∞7050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20
40
0
20 ∞20
20-10
-30
-10Source
∞ ∞7050
-40
-60
Bellman-Ford ExampleBellman Ford Example
40
0
∞ ∞20
20-10
-30
-10Source
∞ ∞7050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20
40
0
20 ∞20
20-10
-30
-10Source
∞ ∞7050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20 60
40
0
20 6020
20-10
-30
-10Source
∞ ∞7050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20 60
40
0
20 6020
20-10
-30
-10Source
50 ∞7050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20 60
40
0
20 6020
20-10
-30
-10Source
50 1007050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20 60
40
0
20 6020
20-10
-30
-10Source
50 707050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20 60
40
0
20 6020
20-10
-30
-10Source
30 707050
-40
-60
Bellman-Ford ExampleBellman Ford Example
20 50
40
0
20 5020
20-10
-30
-10Source
30 707050
-40
-60
The algorithm eventually accumulates network distances so e wo d s ces sothat it can maintain a database of network topology informationtopology information.
Distance-vector algorithms do not allow a router to know the exact topology of an p gyinternetwork
Distance-vector-based routing algorithms (also known as Bellman-Ford algorithms) pass periodic copies of a routing table from router to router These regular updates between routers communicate topologyrouter. These regular updates between routers communicate topology changes