greedy algorithms ms jeevani goone llake university of
play

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


  1. ¡ ¡ ¡ ¡ ¡ ¡ Greedy ¡ ¡Algorithms ms Jeevani ¡Goone*llake ¡ University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 1 ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  2. Th The ¡Gr e ¡Greed eedy ¡Ap ¡Approa oach 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) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 2 ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

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

  4. Key ¡ ¡Ingredients ¡ ¡for ¡ ¡a ¡ ¡Greedy ¡ ¡Algorithm m • Key ¡ ingredients ¡ for ¡ a ¡ greedy ¡ algorithm ¡ to ¡ solve ¡ a ¡ par*cular ¡ op*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) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 4 ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  5. Gr Greed eedy-­‑ch -­‑choi oice ¡p ce ¡prop oper 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 ¡ one ¡ greedy ¡ choice ¡ aVer ¡ another, ¡ reducing ¡ each ¡ given ¡ problem ¡ instance ¡to ¡a ¡smaller ¡one. ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 5 ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  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) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ 6 ¡ Lecture4 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  7. 0/ 0/1 ¡ ¡K 1 ¡ ¡Knap napsac sack ¡pr k ¡problem blem ¡ ¡ ¡Problem ¡statement ¡: ¡A ¡thief ¡robbing ¡a ¡store ¡finds ¡n ¡items. ¡ ¡ ¡ ¡The ¡i th ¡item ¡-­‑ ¡worth ¡v i ¡rupees ¡, ¡weighs ¡w i ¡kg ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡where ¡v i ¡and ¡w i ¡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 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  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). ¡ Maximize subject to is the number of copies of the item x i ϵ {0.1} Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ 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on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend