Greedy Algorithms ms Jeevani Goone*llake University of - - PowerPoint PPT Presentation

greedy algorithms ms jeevani goone llake university of
SMART_READER_LITE
LIVE PREVIEW

Greedy Algorithms ms Jeevani Goone*llake University of - - PowerPoint PPT Presentation

Greedy Algorithms ms Jeevani Goone*llake University of Colombo School of Compu*ng Sri Lanka Jeevani Goone*llake (University of Colombo - Sri


slide-1
SLIDE 1

¡ ¡ ¡ ¡ ¡ ¡Greedy ¡

¡Algorithms ms

Jeevani ¡Goone*llake ¡ University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 1 ¡

slide-2
SLIDE 2

Th The ¡Gr e ¡Greed eedy ¡Ap ¡Approa

  • ach

ch

  • Greedy ¡ ¡algorithms ¡ ¡ ¡are ¡ ¡ ¡typically ¡ ¡applied ¡ ¡ ¡to ¡ ¡ ¡op#miza#on ¡ ¡

¡ ¡problems. ¡An ¡op*miza*on ¡problem ¡requires ¡to ¡find ¡a ¡solu*on ¡ ¡ ¡with ¡the ¡op*mal ¡(minimum ¡or ¡maximum) ¡value. ¡ ¡

¡

¡

Please ¡also ¡refer ¡to ¡Frank ¡Drew’s ¡slide ¡o4 ¡on ¡MST. ¡

¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 2 ¡

slide-3
SLIDE 3

Greedy ¡ ¡Algorithms ms

  • A ¡greedy ¡algorithm ¡obtains ¡an ¡op*mal ¡solu*on ¡to ¡a ¡problem ¡

by ¡making ¡a ¡sequence ¡of ¡choices. ¡At ¡each ¡decision ¡point, ¡the ¡ algorithm ¡makes ¡the ¡choice ¡that ¡seems ¡best ¡at ¡the ¡moment. ¡ ¡

  • ­‑ ¡That ¡is, ¡it ¡makes ¡a ¡locally ¡op*mal ¡choice ¡in ¡the ¡hope ¡that ¡

this ¡choice ¡will ¡lead ¡to ¡a ¡globally ¡op*mal ¡solu*on. ¡ ¡ ¡

  • Greedy ¡algorithms ¡do ¡not ¡always ¡yield ¡op*mal ¡solu*ons, ¡but ¡

for ¡many ¡problems ¡they ¡do. ¡ ¡

3 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-4
SLIDE 4

Key ¡ ¡Ingredients ¡ ¡for ¡ ¡a ¡ ¡Greedy ¡ ¡Algorithm m

  • Key ¡ ingredients ¡ for ¡ a ¡ greedy ¡ algorithm ¡ to ¡ solve ¡ a ¡ par*cular ¡
  • p*miza*on ¡problem ¡are: ¡ ¡
  • Greedy-­‑choice ¡property ¡and ¡ ¡
  • Op*mal ¡substructure ¡ ¡
  • If ¡it ¡is ¡possible ¡to ¡demonstrate ¡that ¡the ¡problem ¡has ¡these ¡proper*es, ¡

then ¡developing ¡a ¡greedy ¡algorithm ¡for ¡the ¡problem ¡is ¡possible. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 4 ¡

slide-5
SLIDE 5

Gr Greed eedy-­‑ch

  • ­‑choi
  • ice ¡p

ce ¡prop

  • per

erty

  • In ¡greedy ¡algorithm, ¡we ¡make ¡whatever ¡choice ¡seems ¡best ¡at ¡the ¡

moment ¡and ¡then ¡solve ¡the ¡sub-­‑problem ¡that ¡remains. ¡ ¡

  • The ¡choice ¡made ¡by ¡a ¡greedy ¡algorithm ¡may ¡depend ¡on ¡choices ¡so ¡

far, ¡but ¡it ¡cannot ¡depend ¡on ¡any ¡future ¡choices ¡or ¡on ¡the ¡solu*ons ¡ to ¡sub-­‑problems. ¡

  • A ¡greedy ¡strategy ¡usually ¡progresses ¡in ¡a ¡top-­‑down ¡fashion, ¡making ¡
  • ne ¡ greedy ¡ choice ¡ aVer ¡ another, ¡ reducing ¡ each ¡ given ¡ problem ¡

instance ¡to ¡a ¡smaller ¡one. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 5 ¡

slide-6
SLIDE 6

Op;ma mal ¡ ¡Substructure ¡ ¡Property

  • A ¡problem ¡exhibits ¡op#mal ¡substructure ¡if ¡an ¡op*mal ¡solu*on ¡to ¡

the ¡problem ¡contains ¡within ¡it ¡op*mal ¡solu*ons ¡to ¡sub-­‑problems. ¡

  • It ¡is ¡assumed ¡that ¡we ¡arrived ¡at ¡a ¡sub-­‑problem ¡by ¡having ¡made ¡the ¡

greedy ¡choice ¡in ¡the ¡original ¡problem. ¡ ¡

  • All ¡that ¡is ¡necessary ¡to ¡do ¡is, ¡argue ¡that ¡an ¡op*mal ¡solu*on ¡to ¡the ¡

sub-­‑problem, ¡ combined ¡ with ¡ the ¡ greedy ¡ choice ¡ already ¡ made, ¡ yields ¡an ¡op*mal ¡solu*on ¡to ¡the ¡original ¡problem. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 6 ¡

slide-7
SLIDE 7

0/ 0/1 ¡ ¡K 1 ¡ ¡Knap napsac sack ¡pr k ¡problem blem

¡ ¡ ¡Problem ¡statement ¡: ¡A ¡thief ¡robbing ¡a ¡store ¡finds ¡n ¡items. ¡ ¡ ¡ ¡The ¡ith ¡item ¡-­‑ ¡worth ¡vi ¡rupees ¡, ¡weighs ¡wi ¡kg ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡where ¡vi ¡and ¡wi ¡are ¡integers. ¡ ¡ ¡ ¡ ¡He ¡wants ¡to ¡take ¡as ¡valuable ¡load ¡as ¡possible ¡but ¡can ¡carry ¡at ¡most ¡W ¡kg ¡ in ¡his ¡knapsack ¡for ¡some ¡integer ¡W. ¡ ¡ ¡ ¡ ¡(Note ¡: ¡Items ¡can ¡be ¡taken ¡or ¡le2 ¡behind ¡but ¡cannot ¡take ¡frac8ons ¡of ¡ items ¡or ¡take ¡items ¡more ¡than ¡once) ¡ Which

items should I take?

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-8
SLIDE 8

0/ 0/1 ¡ ¡K 1 ¡ ¡Knap napsac sack ¡pr k ¡problem blem

  • This ¡problem ¡exhibits ¡op*mal ¡sub ¡structure ¡property. ¡ ¡
  • Obeying ¡a ¡greedy ¡strategy ¡the ¡thief ¡begins ¡by ¡taking ¡the ¡item ¡with ¡

the ¡greatest ¡value ¡per ¡kg. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • When ¡ the ¡ supply ¡ of ¡ an ¡ item ¡ is ¡ exhausted ¡ and ¡ he ¡ can ¡ s*ll ¡ carry ¡

more, ¡he ¡takes ¡much ¡as ¡possible ¡of ¡the ¡item ¡with ¡the ¡next ¡greatest ¡ value ¡per ¡kg. ¡ ¡ ¡ ¡ ¡

  • Need ¡to ¡sort ¡the ¡items ¡by ¡value ¡which ¡would ¡take ¡*me ¡O(n ¡log ¡n). ¡

8

Maximize subject to

is the number of copies of the item xi ϵ {0.1}

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-9
SLIDE 9

0/ 0/1 ¡ ¡K 1 ¡ ¡Knap napsac sack ¡pr k ¡problem blem

¡Suppose ¡(n ¡= ¡3); ¡(i ¡= ¡1, ¡2, ¡3); ¡ ¡(v1 ¡= ¡6, ¡v2 ¡= ¡10, ¡v3 ¡= ¡12); ¡ ¡ ¡ ¡ ¡(w1 ¡= ¡1, ¡w2 ¡= ¡2, ¡w3 ¡= ¡3) ¡and ¡W ¡= ¡5. ¡

¡ ¡ ¡ ¡ ¡

¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Item ¡1 ¡is ¡the ¡most ¡valuable ¡per ¡kg, ¡but ¡any ¡solu*on ¡with ¡item ¡1 ¡

is ¡ sub ¡ op*mal. ¡ Therefore ¡ a ¡ greedy ¡ strategy ¡ does ¡ not ¡ work ¡ for ¡ the ¡0/1 ¡knapsack ¡problem. ¡

9

1 2 5 6 10 12 knapsack 1 1 2 2 3 3 3 16 18 22

6 5 4

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-10
SLIDE 10

Fr Frac;onal ¡ ¡Knapsack ¡ ¡Problem ¡ ¡

¡ ¡ ¡If ¡the ¡thief ¡can ¡take ¡frac*ons ¡of ¡items ¡(for ¡example ¡gold ¡dust ¡against ¡ gold ¡ingot) ¡Then ¡the ¡greedy ¡algorithm ¡will ¡yield ¡an ¡op*mal ¡solu*on. ¡

10

1 2 5 6 10 12 knapsack 1 1 2 2 3 3 3 24 23 22 2 1

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-11
SLIDE 11

Knapsack(w[1..n], v[1..n], W): array [1..n] //assumed w & v such that v[i]/w[i] > v[j]/w[j] for i < j for i = 1 to n do x[i] = 0; wt = 0 while wt < W do i = best remaining item if wt + w[i] ≤ W then //w[i] is the greedy choice x[i] = 1; wt = wt + w[i] else x[i] = (W - wt)/w[i] wt = W return x

11

Frac*onal ¡Knapsack ¡Problem ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-12
SLIDE 12

12

  • Suppose ¡we ¡have ¡coins ¡of ¡the ¡following ¡denomina*ons ¡: ¡100, ¡50, ¡

25, ¡10, ¡5, ¡2, ¡1. ¡

  • The ¡problem ¡is ¡to ¡devise ¡an ¡algorithm ¡for ¡paying ¡a ¡given ¡amount ¡

to ¡a ¡customer ¡using ¡the ¡smallest ¡possible ¡number ¡of ¡coins. ¡ ¡

  • For ¡example ¡the ¡minimum ¡possible ¡number ¡of ¡coins ¡to ¡pay ¡289/-­‑ ¡

would ¡be ¡7. ¡(2x100+1x50+1x25+1x10+2x2) ¡ ¡ ¡ $

Coin ¡ ¡Problem m

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-13
SLIDE 13

13

  • The ¡ algorithm ¡ is ¡ greedy ¡ because ¡ at ¡ every ¡ step ¡ it ¡ chooses ¡ the ¡

largest ¡coin ¡it ¡can, ¡without ¡worrying ¡whether ¡this ¡will ¡prove ¡to ¡be ¡ a ¡sound ¡decision ¡in ¡the ¡long ¡run. ¡

  • Thus ¡ choosing ¡ the ¡ highest ¡ coin ¡ denomina*on ¡ is ¡ the ¡ greedy ¡
  • choice. ¡
  • Would ¡greedy ¡approach ¡always ¡ provide ¡ an ¡ op*mal ¡solu*on ¡ for ¡

any ¡ ¡coin ¡denomina*on? ¡ ¡ ¡ ¡ ¡

Coin ¡Problem ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-14
SLIDE 14

14

Coin_Change(n) const C = {100,50,25,10,5,2,1} /* set of coins S = {}; s = 0 while s ≠ n do x = select_largest_value(C) such that s + x ≤ n /* x is the greedy choice if not feasible then “no solutions” else S = S ∪ {x}; s = s + x return S E.g. n = 289, s={100,100,50,25,10,2,2}

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-15
SLIDE 15

¡ ¡ ¡ ¡ ¡ ¡Minimu mum ¡ m ¡Spanning ¡ ¡Trees

¡University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 15 ¡

slide-16
SLIDE 16

Spanning ¡ Spanning ¡Tree

¡

¡A ¡ ¡spanning ¡ ¡tree ¡ ¡ ¡for ¡ ¡a ¡ ¡connected ¡ ¡ ¡undirected ¡ ¡graph ¡ ¡G=(V,E) ¡ ¡is ¡ ¡a ¡ ¡subgraph ¡of ¡G ¡ ¡that ¡is ¡a ¡tree ¡and ¡contains ¡all ¡the ¡ver*ces ¡of ¡G ¡ ¡ ¡ Thus ¡a ¡spanning ¡tree ¡for ¡G ¡is ¡a ¡graph, ¡T ¡= ¡(V’, ¡E’) ¡with ¡the ¡following ¡ proper*es: ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡V’ ¡= ¡V ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡T ¡is ¡connected ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡T ¡is ¡acyclic. ¡ ¡ A ¡spanning ¡tree ¡is ¡called ¡a ¡tree ¡because ¡every ¡acyclic ¡undirected ¡graph ¡ ¡can ¡ ¡be ¡ ¡viewed ¡ ¡as ¡ ¡ ¡a ¡ ¡general, ¡unordered ¡ ¡tree. ¡ ¡Since ¡ ¡the ¡ ¡edges ¡ ¡are ¡ ¡undirected, ¡any ¡vertex ¡may ¡be ¡chosen ¡to ¡serve ¡as ¡the ¡root ¡of ¡the ¡tree. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 16 ¡

slide-17
SLIDE 17

A ¡graph ¡may ¡have ¡many ¡spanning ¡trees. ¡

  • r
  • r
  • r

Some ¡Spanning ¡Trees ¡from ¡Graph ¡A ¡ Graph ¡A ¡

Spanning ¡ Spanning ¡Tree

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 17 ¡

slide-18
SLIDE 18

All 16 of its Spanning Trees Complete Graph

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 18 ¡

slide-19
SLIDE 19

Minima mal ¡ ¡Spanning ¡ ¡Tree ¡ ¡(MST) ¡

¡ ¡A ¡minimal ¡spanning ¡tree ¡of ¡a ¡(connected ¡undirected) ¡weighted ¡graph ¡ ¡ ¡G ¡= ¡(V, ¡E, ¡w) ¡is ¡a ¡sub-­‑graph ¡T ¡= ¡(V’, ¡E’) ¡of ¡G ¡such ¡that ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡T ¡is ¡a ¡spanning ¡tree ¡ ¡and ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡ ¡weight ¡w(T) ¡= ¡ ¡Σ ¡ ¡ ¡w(e) ¡ ¡is ¡minimal. ¡

e ¡ϵ E’ ¡ 4 4 3 2 9 15 8 10 14 3 u v b a c d e f

* ¡Can ¡a ¡graph ¡have ¡more ¡then ¡

  • ne ¡minimum ¡spanning ¡tree? ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 19 ¡

slide-20
SLIDE 20

¡ ¡ ¡ ¡Applica;ons ¡ ¡of ¡ ¡Minimu mum ¡ m ¡Spanning ¡ ¡Trees

Minimum-­‑cost ¡spanning ¡trees ¡have ¡many ¡applica*ons. ¡Example: ¡

  • ¡ ¡Building ¡cable ¡networks ¡that ¡join ¡n ¡loca*ons ¡with ¡minimum ¡cost. ¡

Transla0ng ¡a ¡ ¡Problem ¡into ¡a ¡MST ¡

  • Each ¡loca*on ¡of ¡the ¡network ¡must ¡be ¡connected ¡using ¡the ¡least ¡

amount ¡of ¡cables. ¡ Modeling ¡the ¡Problem ¡ ¡

  • The ¡graph ¡is ¡a ¡complete, ¡undirected ¡graph ¡G ¡= ¡( ¡V, ¡E ¡,W ¡), ¡where ¡V ¡is ¡

the ¡ ¡set ¡ ¡of ¡ ¡loca*ons, ¡E ¡ ¡is ¡ ¡the ¡ ¡set ¡ ¡of ¡ ¡all ¡ ¡possible ¡ ¡interconnec*ons ¡ between ¡ ¡the ¡ ¡pairs ¡ ¡of ¡ ¡loca*ons ¡ ¡and ¡ ¡w(e) ¡is ¡the ¡length ¡of ¡the ¡cable ¡ needed ¡to ¡connect ¡the ¡pair ¡of ¡ver*ces. ¡

  • Find ¡a ¡minimum ¡spanning ¡tree. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 20 ¡

slide-21
SLIDE 21

Underlying ¡Princ Underlying ¡Principles ¡ iples ¡

Recall ¡two ¡of ¡the ¡defining ¡proper*es ¡of ¡a ¡tree: ¡ ¡

  • Removing ¡ an ¡ edge ¡ from ¡ a ¡ tree ¡ breaks ¡ it ¡ into ¡ two ¡ separate ¡

sub-­‑trees. ¡ ¡

  • Adding ¡an ¡edge ¡that ¡connects ¡two ¡ver*ces ¡in ¡a ¡tree ¡creates ¡a ¡

unique ¡cycle. ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 21 ¡

slide-22
SLIDE 22

MST ¡ ¡Algorithm ¡ m ¡– ¡ – ¡Cut ¡ ¡Property

Given ¡any ¡ ¡cut ¡ ¡in ¡ ¡an ¡ ¡edge-­‑weighted ¡graph ¡ ¡(for ¡simplicity ¡assume ¡ that ¡all ¡edge ¡weights ¡are ¡dis*nct), ¡the ¡crossing ¡edge ¡of ¡minimum ¡ weight ¡is ¡in ¡the ¡MST ¡of ¡the ¡graph. ¡ ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 22 ¡

slide-23
SLIDE 23

MS MST ¡C ¡Cons nstruc truc;o ;on ¡ n ¡

The ¡cut ¡property ¡yields ¡a ¡simple ¡greedy ¡algorithm ¡for ¡finding ¡an ¡

  • MST. ¡ ¡
  • Start ¡with ¡an ¡empty ¡set ¡T ¡of ¡edges. ¡ ¡
  • Let ¡ Eʹ″ ¡ be ¡ the ¡ set ¡ of ¡ edges ¡ relevant ¡ to ¡ a ¡ cut ¡ which ¡ do ¡ not ¡

contain ¡any ¡edge ¡from ¡T ¡

  • ¡As ¡long ¡as ¡T ¡is ¡not ¡a ¡spanning ¡tree, ¡add ¡a ¡minimal-­‑cost ¡edge ¡

from ¡Eʹ″ ¡(known ¡as ¡light ¡edge) ¡to ¡T. ¡ ¡

  • Different ¡choices ¡of ¡Eʹ″ ¡lead ¡to ¡different ¡specific ¡algorithms. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 23 ¡

slide-24
SLIDE 24

Ligh Light ¡E t ¡Edg dge e

A ¡ cut ¡ (S, ¡ V-­‑S) ¡ of ¡ an ¡ undirected ¡ graph ¡ G ¡ = ¡ (V,E) ¡ is ¡ a ¡ par**on ¡ ¡of ¡V. ¡ ¡ Ver*ces ¡in ¡the ¡set ¡S ¡-­‑ ¡gray ¡ ¡ ¡ ¡ ¡Ver*ces ¡in ¡(V-­‑S) ¡– ¡white ¡ Edges ¡crossing ¡the ¡cut ¡ ¡are ¡connec*ng ¡white ¡ver*ces ¡and ¡ black ¡ver*ces. ¡ ¡

2 4 1 6 5 3 7 9 b c d e a

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 24 ¡

slide-25
SLIDE 25

MS MST ¡C ¡Cons nstruc truc;o ;on ¡ n ¡

Given ¡any ¡cut ¡the ¡crossing ¡edge ¡of ¡minimum ¡weight ¡is ¡in ¡the ¡MST. ¡ Proof: ¡Suppose ¡min-­‑weight ¡crossing ¡edge ¡e ¡is ¡not ¡in ¡the ¡MST. ¡

  • Adding ¡e ¡to ¡the ¡MST ¡creates ¡a ¡cycle. ¡
  • Some ¡other ¡edge ¡e’ ¡in ¡cycle ¡must ¡be ¡a ¡crossing ¡edge. ¡
  • Removing ¡e’ ¡and ¡adding ¡e ¡is ¡also ¡a ¡spanning ¡tree ¡
  • Since ¡w(e) ¡is ¡less ¡than ¡w(e’) ¡that ¡spanning ¡tree ¡is ¡lower ¡weight. ¡
  • Contradic*on* ¡

Please ¡also ¡ ¡refer ¡to ¡Frank ¡Drew’s ¡slides ¡no. ¡8-­‑9 ¡on ¡MST ¡for ¡MST ¡Construc*on ¡ Theorem ¡and ¡Proof. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 25 ¡

slide-26
SLIDE 26

Generic ¡ ¡Algorithm ¡ m ¡for ¡ ¡MST

Input ¡: ¡connected ¡weighted ¡graph, ¡G ¡ Output ¡: ¡MST, ¡T, ¡for ¡graph ¡G ¡ ¡ Greedy ¡strategy ¡in ¡the ¡generic ¡algorithm ¡

  • ­‑ Grow ¡the ¡MST ¡one ¡edge ¡at ¡a ¡*me. ¡
  • ­‑ Manage ¡a ¡set ¡of ¡edges ¡A, ¡that ¡is ¡prior ¡to ¡each ¡itera*on, ¡A ¡

¡ ¡ ¡is ¡a ¡subset ¡of ¡some ¡MST ¡

  • At ¡each ¡step ¡determine ¡an ¡edge ¡(u,v) ¡that ¡can ¡be ¡ ¡

¡ ¡ ¡added ¡to ¡A ¡without ¡viola*ng ¡this ¡invariant. ¡ ¡

  • We ¡call ¡such ¡an ¡edge ¡a ¡safe ¡edge ¡for ¡A, ¡since ¡it ¡can ¡be ¡

¡ ¡ ¡safely ¡added ¡to ¡A ¡while ¡maintaining ¡the ¡invariant. ¡ ¡ ¡ ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 26 ¡

slide-27
SLIDE 27

Generic ¡ ¡Algorithm m

Generic-­‑MST(G,w) ¡ 1. A ¡ß ¡0 ¡ 2. while ¡A ¡does ¡not ¡form ¡a ¡spanning ¡tree ¡ 3. ¡ ¡do ¡find ¡an ¡edge ¡(u,v) ¡that ¡is ¡safe ¡for ¡A ¡ 4. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ß ¡A ¡U ¡{ ¡(u,v)} ¡ 5. return ¡A ¡ ¡ Safe ¡edge ¡-­‑ ¡a ¡light ¡edge ¡sa*sfying ¡a ¡given ¡property. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 27 ¡

slide-28
SLIDE 28

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Gr

Greed eedy ¡Ch ¡Choi

  • ice

ce

Two ¡algorithms ¡to ¡build ¡a ¡minimum ¡spanning ¡tree. ¡

  • MST ¡ can ¡ be ¡ grown ¡ from ¡ a ¡ forest ¡ of ¡ spanning ¡ trees ¡ : ¡ find ¡ a ¡ safe ¡

edge ¡to ¡be ¡added ¡to ¡the ¡growing ¡forest ¡by ¡finding, ¡of ¡all ¡the ¡edges ¡ that ¡connects ¡two ¡dis*nct ¡trees ¡in ¡the ¡forest. ¡ ¡The ¡choice ¡is ¡greedy ¡ because ¡at ¡each ¡step ¡it ¡adds ¡to ¡the ¡forest ¡an ¡edge ¡of ¡least ¡possible ¡

  • weight. ¡(Kruskal's ¡algorithm) ¡
  • MST ¡can ¡be ¡grown ¡from ¡the ¡current ¡spanning ¡tree: ¡Each ¡step ¡adds ¡

to ¡the ¡tree ¡A ¡ ¡a ¡light ¡edge ¡that ¡connects ¡A ¡to ¡an ¡isolated ¡vertex—

  • ne ¡on ¡which ¡no ¡edge ¡of ¡A ¡is ¡incident. ¡(Prim's ¡algorithm) ¡

¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 28 ¡

slide-29
SLIDE 29

MST-­‑Kruskal(G,w) ¡ 1. A ¡ß ¡0 ¡ 2. For ¡each ¡vertex ¡v ¡∈ ¡V[G] ¡ 3. ¡ ¡ ¡ ¡ ¡do ¡Make-­‑Set(v) ¡ 4. sort ¡the ¡edges ¡of ¡E ¡into ¡nondecreasing ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡order ¡by ¡weight ¡w ¡

  • 5. ¡ ¡ ¡ ¡for ¡each ¡edge ¡(u,v) ¡∈ ¡E, ¡taken ¡in ¡nondecreasing ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡order ¡by ¡weight ¡w ¡ 6. ¡ ¡ ¡ ¡do ¡if ¡Find-­‑Set(u) ¡ ¡≠ ¡Find-­‑Set(v) ¡ ¡ 7. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡then ¡A ¡ß ¡A ¡U ¡{ ¡(u,v)} ¡ 8. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Union ¡(u,v) ¡ 9. return ¡A ¡ ¡

A – Tree w - weight

Kruskal's ¡Algorithm ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 29 ¡

slide-30
SLIDE 30

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Kr Kruskal’s ¡ ¡Algorithm m

  • Ini*alize ¡the ¡set ¡A ¡to ¡the ¡empty ¡set ¡
  • Create ¡|V| ¡trees ¡(sets), ¡one ¡containing ¡each ¡vertex. ¡
  • Sort ¡the ¡edges ¡in ¡increasing ¡order ¡of ¡weight. ¡
  • Take ¡the ¡edges ¡in ¡the ¡sorted ¡order. ¡
  • For ¡each ¡edge ¡(u,v), ¡check ¡whether ¡the ¡endpoints ¡

(ver*ces) ¡u ¡and ¡v ¡belong ¡to ¡the ¡same ¡tree. ¡

  • It ¡is ¡safe ¡to ¡connect ¡two ¡ver*ces ¡if ¡they ¡belong ¡to ¡different ¡
  • trees. ¡If ¡so ¡the ¡edge ¡(u,v) ¡is ¡added ¡to ¡A. ¡
  • Ver*ces ¡in ¡the ¡two ¡trees ¡are ¡merged. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 30 ¡

slide-31
SLIDE 31

Kr Kruskal’s ¡ ¡Algorithm ¡ m ¡Examp mple

Initially A = { } Sets – {a} {b} {c} {d} {e} {f} E – Sorted in Ascending Order Step 1 Take (f,d) ; Find-Set(f) ≠ Find-Set(d) => add (f,d) to A A = {(f,d)} Combine Set(f) & Set(d) Sets - {a} {b} {c} {e} {f,d}

¡( ¡f, ¡d ¡) ¡ ( ¡b, ¡e ¡) ¡ ( ¡c, ¡d ¡) ¡ ( ¡a, ¡b ¡) ¡ ( ¡b, ¡c ¡) ¡ ( ¡e, ¡d ¡) ¡ ( ¡a, ¡f ¡) ¡

a ¡ b ¡ c ¡ e ¡ d ¡ f ¡

4 ¡ 8 ¡ 2 ¡ 6 ¡ 1 ¡ 4 ¡ 2 ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) 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Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 47 ¡

slide-48
SLIDE 48

Kr Kruskal’s ¡ ¡Algorithm ¡ m ¡– ¡ – ¡Run;me me ¡ ¡Analysis

Run ¡*me ¡depends ¡on ¡the ¡opera*ons ¡on ¡the ¡disjoint ¡sets ¡data ¡structure: ¡

  • Ini*alize ¡the ¡set ¡A: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(1) ¡
  • First ¡for ¡loop: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡|V| ¡MAKE-­‑SETs ¡
  • Sort ¡E: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(E ¡lg ¡E) ¡
  • Second ¡for ¡loop: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(E) ¡FIND-­‑SETs ¡and ¡UNIONs ¡ ¡=> ¡O(E ¡lg ¡E) ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • Therefore, total run time is O(E lg E).

|E| ≤ |V|2 ⇒lg |E| = O(2 lg V) = O(lg V).

  • Hence, O(E lg V) time.

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 48 ¡

slide-49
SLIDE 49

Prim’ m’s ¡ ¡Algorithm m

  • Edges ¡in ¡the ¡set ¡A ¡always ¡form ¡a ¡single ¡tree. ¡
  • Tree ¡starts ¡from ¡an ¡arbitrary ¡root ¡vertex ¡ ¡r ¡and ¡grows ¡un*l ¡

the ¡tree ¡spans ¡all ¡the ¡ver*ces ¡in ¡V. ¡

  • At ¡each ¡step ¡a ¡light ¡edge ¡is ¡added ¡to ¡the ¡tree ¡A. ¡The ¡

algorithm ¡implicitly ¡maintains ¡the ¡set ¡A ¡

  • This ¡strategy ¡is ¡greedy. ¡ ¡

* ¡Please ¡refer ¡to ¡Frank ¡Drew’s ¡slide ¡no. ¡17 ¡on ¡MST. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 49 ¡

slide-50
SLIDE 50

Prim’ m’s ¡ ¡Algorithm m

MST-PRIM (G, w, r) 1 for each u ∈ V [G] 2 do key [u] ← ∞ 3 π [u] ← NIL 4 key [r ] ← 0 5 Q ← V [G] 6 while Q ≠ ∅ 7 do u ← EXTRACT-MIN (Q) 8 for each v ∈ Adj [u] 9 do if v ∈ Q and w (u, v) < key [v] 10 then π [v] ← u 11 key [v] ← w (u, v)

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 50 ¡

slide-51
SLIDE 51

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Problem m

h f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 51 ¡

slide-52
SLIDE 52

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Solu;on

h f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9 d) h f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9 e)

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 52 ¡

slide-53
SLIDE 53

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Solu;on

h f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9 f) f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9 g) h

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 53 ¡

slide-54
SLIDE 54

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Solu;on

h f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9 i) h f b c d e g i a 4 8 11 1 2 6 2 7 4 8 7 10 14 9 h)

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 54 ¡

slide-55
SLIDE 55

55

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Analysis

  • Maintains ¡ a ¡ min-­‑priority ¡ queue ¡ by ¡ calling ¡ three ¡ priority ¡ queue ¡
  • pera*ons: ¡

¡-­‑ ¡INSERT ¡ ¡-­‑ ¡EXTRACT-­‑MIN ¡ ¡-­‑ ¡DECREASE-­‑KEY ¡

  • Running ¡*me ¡of ¡Prim’s ¡Algorithm ¡depends ¡on ¡how ¡the ¡min-­‑priority ¡

queue ¡is ¡implemented. ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-56
SLIDE 56

56

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Analysis

  • Binary ¡min-­‑heap ¡

¡ ¡ ¡ ¡Building ¡min ¡binary ¡heap ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(V) ¡ ¡ ¡ ¡ ¡DECREASE-­‑KEY ¡ ¡ ¡ ¡ ¡ ¡O(lg ¡V) ¡ ¡-­‑ ¡E ¡ ¡ ¡ ¡ ¡EXTRACT-­‑MIN ¡ ¡ ¡ ¡ ¡ ¡O(lg ¡V) ¡ ¡ ¡-­‑ ¡V ¡ ¡ ¡ ¡ ¡ ¡Total ¡*me ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O ¡((V ¡+ ¡E) ¡lg ¡V) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡O ¡(E ¡lg ¡V) ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-57
SLIDE 57

57

Prim’ m’s ¡ ¡Algorithm ¡ m ¡-­‑

  • ­‑ ¡

¡Analysis

  • Fibonacci ¡heap ¡

¡ ¡ ¡ ¡Building ¡Fibonacci ¡heap ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(V) ¡ ¡ ¡ ¡ ¡DECREASE-­‑KEY ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(1) ¡ ¡ ¡ ¡-­‑ ¡E ¡ ¡ ¡ ¡ ¡EXTRACT-­‑MIN ¡ ¡ ¡ ¡ ¡ ¡O(lg ¡V) ¡ ¡ ¡-­‑ ¡V ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Total ¡*me ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡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Cardiff Sheffield Nottingham Oxford Southampton Bristol Shrewsbury Liverpool Aberystwyth B/ham Manchester

50 40 40 30 80 70 80 50 90 50 110 70 120 110 70 100 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 62 ¡

slide-63
SLIDE 63

Cardiff Sheffield Nottingham Oxford Southampton Bristol Shrewsbury Liverpool Aberystwyth B/ham Manchester

50 40 40 30 80 70 80 50 90 50 110 70 120 110 70 100 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 63 ¡

slide-64
SLIDE 64

Cardiff Sheffield Nottingham Oxford Southampton Bristol Shrewsbury Liverpool Aberystwyth B/ham Manchester

50 40 40 30 80 70 80 50 90 50 110 70 120 110 70 100 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 64 ¡

slide-65
SLIDE 65

Cardiff Sheffield Nottingham Oxford Southampton Bristol Shrewsbury Liverpool Aberystwyth B/ham Manchester

50 40 40 30 80 70 80 50 90 50 110 70 120 110 70 100 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 65 ¡

slide-66
SLIDE 66

Cardiff Sheffield Nottingham Oxford Southampton Bristol Shrewsbury Liverpool Aberystwyth B/ham Manchester

50 40 40 30 80 70 80 50 90 50 110 70 120 110 70 100 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 66 ¡

slide-67
SLIDE 67

Cardiff Sheffield Nottingham Oxford Southampton Bristol Shrewsbury Liverpool Aberystwyth B/ham Manchester

50 40 40 30 80 70 80 50 90 50 110 70 120 110 70 100 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡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