82
GREEDY ALGORITHMS Greedy algorithms are typically used to solve optimisation problems solve optimisation problems Greedy algorithms are usually the most straightforward, take decisions on the basis straightforward, take decisions on the basis of information immediately at hand without worrying about the effect these decisions may have in the future They are easy to invent and easy to implement Many problems cannot be solved by these algorithms due to their simplicity

GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 2: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 3: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 4: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

• 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

Page 5: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

• 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.

Page 6: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 7: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 8: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 9: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)}

Page 10: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 11: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

b c d

2

8 7

4 9

a

h

i

g f

e11

7 6

24

8

14

10h g f

1 2

Page 12: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)

Page 13: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

• 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.

Page 14: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 15: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 16: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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}

Page 17: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)}

Page 18: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

• 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

Page 19: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)

Page 20: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

( )

Page 21: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 22: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 23: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)}

Page 24: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)}

Page 25: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)}

Page 26: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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}

Page 27: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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}

Page 28: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)

Page 29: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 30: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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}

Page 31: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 32: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 33: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 34: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 35: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

∞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)

Page 36: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

81

10

t x9

0 4 632

9

75

10

s

752

5

zy75

(e)

Page 37: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 38: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

BellmanBellman FordFordBellmanBellman--Ford Ford AlgorithmAlgorithm

Page 39: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 40: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 41: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 42: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 43: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 44: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 45: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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

Page 46: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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.

Page 47: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 48: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 49: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)

Page 50: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 51: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 52: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 53: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 54: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 55: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 56: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 57: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 58: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 59: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 60: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 61: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 62: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree
Page 63: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford Algorithm –Example 2

6 5-2

6

7-38

47

72

-4

9

Page 64: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford Algorithm –Example 2

∞ ∞6 5-2

0

6

7-38

4

∞ ∞7

72

-4

9

Page 65: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford Algorithm –Example 2

6 ∞6 5-2

0

6

7-38

4

7 ∞7

72

-4

9

Page 66: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford Algorithm –Example 2

6 46 5-2

0

6

7-38

4

7 27

72

-4

9

Page 67: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford Algorithm –Example 2

2 46 5-2

0

6

7-38

4

7 27

72

-4

9

Page 68: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford Algorithm –Example 2

2 46 5-2

0

6

7-38

4

7 -27

72

-4

9

Page 69: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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)

Page 70: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

what route to take?“B”“A”

R2

R1

2

R

R4

R3

Page 71: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

ImplementationImplementationImplementationImplementation

Page 72: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

40

0

∞ ∞20

20-10

-30

-10Source

∞ ∞7050

-40

-60

Page 73: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20

40

0

20 ∞20

20-10

-30

-10Source

∞ ∞7050

-40

-60

Page 74: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

40

0

∞ ∞20

20-10

-30

-10Source

∞ ∞7050

-40

-60

Page 75: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20

40

0

20 ∞20

20-10

-30

-10Source

∞ ∞7050

-40

-60

Page 76: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20 60

40

0

20 6020

20-10

-30

-10Source

∞ ∞7050

-40

-60

Page 77: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20 60

40

0

20 6020

20-10

-30

-10Source

50 ∞7050

-40

-60

Page 78: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20 60

40

0

20 6020

20-10

-30

-10Source

50 1007050

-40

-60

Page 79: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20 60

40

0

20 6020

20-10

-30

-10Source

50 707050

-40

-60

Page 80: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20 60

40

0

20 6020

20-10

-30

-10Source

30 707050

-40

-60

Page 81: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

Bellman-Ford ExampleBellman Ford Example

20 50

40

0

20 5020

20-10

-30

-10Source

30 707050

-40

-60

Page 82: GREEDY ALGORITHMS · • This greedy strategy is captured by theThis greedy strategy is captured by the “generic” algorithm whichalgorithm, which grows the minimum spanning tree

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