going from a to g
play

Going from A to G Star=ng with a full tank, we can - PDF document

7/20/10 Going from A to G Star=ng with a full tank, we can drive 350 miles before we need to gas up, minimize the number of CS420


  1. 7/20/10 ¡ Going ¡from ¡A ¡to ¡G ¡ • Star=ng ¡with ¡a ¡full ¡tank, ¡we ¡can ¡drive ¡350 ¡miles ¡ before ¡we ¡need ¡to ¡gas ¡up, ¡minimize ¡the ¡number ¡of ¡ CS420 ¡lecture ¡eight ¡ =mes ¡we ¡need ¡ ¡to ¡stop ¡at ¡a ¡gas ¡sta=on ¡ Greedy ¡Algorithms ¡ ¡ ¡ ¡ ¡ ¡A ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E ¡ ¡ ¡ ¡ ¡F ¡ ¡ ¡ ¡ ¡ ¡ ¡G ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡250 ¡300 ¡ ¡ ¡ ¡ ¡600 ¡ ¡ ¡ ¡850 ¡ ¡900 ¡ ¡1100 ¡ Going ¡from ¡A ¡to ¡G ¡ Going ¡from ¡A ¡to ¡G ¡ • We ¡can ¡drive ¡350 ¡miles ¡before ¡we ¡need ¡to ¡gas ¡up, ¡ • We ¡can ¡drive ¡350 ¡miles ¡before ¡we ¡need ¡to ¡gas ¡up, ¡ minimize ¡the ¡number ¡of ¡=mes ¡we ¡need ¡ ¡to ¡stop ¡at ¡a ¡ minimize ¡the ¡number ¡of ¡=mes ¡we ¡need ¡ ¡to ¡stop ¡at ¡a ¡gas ¡ gas ¡sta=on ¡ sta=on ¡ • a ¡possible ¡(non ¡greedy) ¡op=mal ¡solu=on ¡ • we ¡can ¡make ¡it ¡greedy: ¡ ¡ ¡ ¡ ¡ ¡A ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E ¡ ¡ ¡ ¡ ¡F ¡ ¡ ¡ ¡ ¡ ¡ ¡G ¡ ¡ ¡ ¡ ¡ ¡A ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E ¡ ¡ ¡ ¡ ¡F ¡ ¡ ¡ ¡ ¡ ¡ ¡G ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡250 ¡300 ¡ ¡ ¡ ¡ ¡600 ¡ ¡ ¡ ¡850 ¡ ¡900 ¡ ¡1100 ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡250 ¡300 ¡ ¡ ¡ ¡ ¡600 ¡ ¡ ¡ ¡850 ¡ ¡900 ¡ ¡1100 ¡ A ¡greedy ¡solu=on ¡goes ¡as ¡far ¡as ¡possible ¡before ¡gassing ¡up, ¡ ¡ and ¡we ¡can ¡turn ¡an ¡op=mal ¡solu=on ¡into ¡a ¡greedy ¡one ¡by ¡ making ¡the ¡first ¡step ¡greedy, ¡and ¡then ¡applying ¡induc=on ¡ 1 ¡

  2. 7/20/10 ¡ Greedy ¡algorithms ¡ Ac=vity ¡Selec=on ¡ • Given ¡a ¡set ¡of ¡ac=vi=es ¡S ¡= ¡{ ¡1,2,3,...,N ¡} ¡that ¡use ¡a ¡ ¡ resource ¡and ¡have ¡a ¡start ¡=me ¡S i ¡ ¡and ¡finish ¡=me ¡F i ¡ ¡ S i <=F i . ¡ ¡ Greedy ¡ ¡algorithms ¡determine ¡a ¡global ¡op3mum ¡ ¡ • Ac=vi=es ¡are ¡compa=ble ¡if ¡the ¡intervals ¡[S i ,F i ) ¡and ¡ ¡ via ¡(a ¡number ¡of) ¡locally ¡op3mal ¡choices ¡ [S j ,F j ) ¡do ¡not ¡overlap: ¡S i >=F j ¡or ¡S j >=F i ¡ ¡ • ¡[ ¡ ¡[) ¡means: ¡includes ¡le\ ¡but ¡only ¡up ¡to ¡right ¡ ¡) ¡ ¡ • The ¡ ¡ Ac3vity-­‑selec3on ¡ problem ¡is ¡to ¡select ¡a ¡ ¡ maximum-­‑size ¡set ¡ of ¡ mutually ¡compa3ble ¡ ac=vi=es. ¡ Greedy ¡algorithm ¡for ¡Ac=vity ¡Selec=on ¡ Greedy ¡algorithm ¡for ¡Ac=vity ¡Selec=on ¡ • How ¡would ¡you ¡do ¡it? ¡ ¡ ¡ ¡ ¡ ¡Sort ¡ac=vi=es ¡by ¡finish ¡=me ¡F 1 ¡<= ¡F 2 ¡...<= ¡F n ¡ ¡ ¡ ¡ ¡ ¡A=1 ¡ ¡ ¡ ¡ ¡ ¡j=1 ¡ ¡ ¡ ¡ ¡ ¡for ¡i ¡= ¡2 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡S i >=F j ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡include ¡i ¡in ¡A ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j=i ¡ 2 ¡

  3. 7/20/10 ¡ Eg ¡from ¡Cormen ¡et. ¡al. ¡ Eg ¡from ¡Cormen ¡et. ¡al. ¡ ¡i ¡ ¡ ¡ ¡1 ¡ ¡ ¡2 ¡ ¡ ¡3 ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡ ¡7 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡9 ¡ ¡ ¡10 ¡ ¡ ¡11 ¡ ¡ ¡ ¡ ¡i ¡ ¡ ¡ ¡1 ¡ ¡ ¡2 ¡ ¡ ¡3 ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡ ¡7 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡9 ¡ ¡ ¡10 ¡ ¡ ¡11 ¡ ¡ ¡ ¡ S i ¡ ¡ ¡1 ¡ ¡ ¡3 ¡ ¡ ¡0 ¡ ¡ ¡5 ¡ ¡ ¡3 ¡ ¡ ¡5 ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡12 ¡ ¡ S i ¡ ¡ ¡1 ¡ ¡ ¡3 ¡ ¡ ¡0 ¡ ¡ ¡5 ¡ ¡ ¡3 ¡ ¡ ¡5 ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡12 ¡ ¡ F i ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡7 ¡ ¡ ¡8 ¡ ¡ ¡9 ¡ ¡10 ¡ ¡11 ¡ ¡12 ¡ ¡13 ¡ ¡ ¡14 ¡ F i ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡7 ¡ ¡ ¡8 ¡ ¡ ¡9 ¡ ¡10 ¡ ¡11 ¡ ¡12 ¡ ¡13 ¡ ¡ ¡14 ¡ A ¡= ¡1,4,8,11 ¡ Ac=vity ¡selec=on ¡ ¡ Greedy ¡works ¡for ¡Ac=vity ¡Selec=on ¡ • Are ¡there ¡other ¡ways ¡to ¡do ¡it? ¡ • BASE: ¡ Op=mal ¡solu=on ¡contains ¡ac=vity ¡1 ¡as ¡first ¡ ac=vity ¡ • Let ¡A ¡be ¡an ¡op=mal ¡solu=on ¡with ¡ac=vity ¡k ¡!= ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡sure... ¡ as ¡first ¡ac=vity ¡ • Then ¡we ¡can ¡replace ¡ac=vity ¡k ¡(which ¡has ¡F k >=F 1 ) ¡ by ¡ac=vity ¡1 ¡ • So, ¡picking ¡the ¡first ¡element ¡in ¡a ¡greedy ¡fashion ¡ works ¡ 3 ¡

  4. 7/20/10 ¡ Greedy ¡works ¡for ¡Ac=vity ¡Selec=on ¡ What ¡did ¡we ¡do? ¡ • STEP : ¡ ¡A\er ¡the ¡first ¡choice ¡is ¡made, ¡remove ¡ ¡ • We ¡assumed ¡there ¡was ¡a ¡non ¡greedy ¡op=mal ¡ solu=on, ¡ ¡ all ¡ac=vi=es ¡that ¡are ¡incompa=ble ¡with ¡the ¡first ¡ ¡ • then ¡we ¡stepwise ¡morphed ¡this ¡solu=on ¡in ¡a ¡ chosen ¡ac=vity ¡and ¡recursively ¡define ¡a ¡new ¡ ¡ greedy ¡op=mal ¡solu=on, ¡ problem ¡consis=ng ¡of ¡the ¡remaining ¡ac=vi=es. ¡ ¡ • thereby ¡showing ¡that ¡the ¡greedy ¡solu=on ¡ • The ¡first ¡ac=vity ¡for ¡this ¡reduced ¡problem ¡can ¡ ¡ works ¡in ¡the ¡first ¡place. ¡ be ¡made ¡in ¡a ¡greedy ¡fashion ¡by ¡principle ¡1. ¡ ¡ • By ¡induc=on, ¡Greedy ¡is ¡op=mal. ¡ MST: ¡Minimal ¡Spanning ¡Tree ¡ 11 ¡ 10 ¡ Given ¡a ¡connected ¡and ¡undirected ¡graph ¡with ¡ ¡ 12 ¡ 15 ¡ labeled ¡edges ¡( label ¡= ¡distance ), ¡find ¡a ¡tree ¡that ¡ ¡ 3 ¡ – is ¡a ¡ sub-­‑graph ¡ of ¡the ¡given ¡graph ¡(has ¡nodes ¡and ¡ 8 ¡ 7 ¡ edges ¡from ¡the ¡given ¡graph) ¡ ¡ – and ¡is ¡a ¡ minimal ¡spanning ¡tree : ¡it ¡reaches ¡each ¡ 6 ¡ node, ¡such ¡that ¡the ¡sum ¡of ¡the ¡edge ¡labels ¡is ¡ 4 ¡ minimal ¡(MST). ¡ 4 ¡

  5. 7/20/10 ¡ 11 ¡ 11 ¡ 10 ¡ 10 ¡ 12 ¡ 12 ¡ 15 ¡ 15 ¡ 3 ¡ 3 ¡ 8 ¡ 8 ¡ 7 ¡ 7 ¡ 6 ¡ 6 ¡ 4 ¡ 4 ¡ Pick ¡a ¡node ¡and ¡a ¡minimal ¡edge ¡emana=ng ¡from ¡it, ¡now ¡ ¡ Greedy ¡solu=on ¡for ¡MST? ¡ we ¡have ¡a ¡MST ¡in ¡the ¡making. ¡ Keep ¡adding ¡minimal ¡edges ¡to ¡the ¡MST ¡un=l ¡connected. ¡ 11 ¡ 11 ¡ 10 ¡ 10 ¡ 12 ¡ 12 ¡ 15 ¡ 15 ¡ 3 ¡ 3 ¡ 8 ¡ 8 ¡ 7 ¡ 7 ¡ 6 ¡ 6 ¡ 4 ¡ 4 ¡ 5 ¡

  6. 7/20/10 ¡ 11 ¡ 11 ¡ 10 ¡ 10 ¡ 12 ¡ 12 ¡ 15 ¡ 15 ¡ 3 ¡ 3 ¡ 8 ¡ 8 ¡ 7 ¡ 7 ¡ 6 ¡ 6 ¡ 4 ¡ 4 ¡ 11 ¡ 11 ¡ 10 ¡ 10 ¡ 12 ¡ 12 ¡ 15 ¡ 15 ¡ 3 ¡ 3 ¡ 8 ¡ 8 ¡ 7 ¡ 7 ¡ 6 ¡ 6 ¡ 4 ¡ 4 ¡ 6 ¡

Download Presentation
Download Policy: The content available 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