dynami mic programmi mming
play

Dynami mic Programmi mming Jeevani Goone*llake University - PowerPoint PPT Presentation

Dynami mic Programmi mming Jeevani Goone*llake University of Colombo School of Compu*ng Sri Lanka Jeevani Goone*llake (University of Colombo - Sri Lanka)


  1. Dynami mic ¡ ¡Programmi mming Jeevani ¡Goone*llake ¡ University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  2. Dynami mic ¡ ¡Programmi mming • Dynamic ¡ Programming, ¡ like ¡ the ¡ divide-­‑and-­‑conquer ¡ approach ¡ method, ¡ solves ¡ problems ¡ by ¡ combining ¡ the ¡ solu*ons ¡to ¡sub-­‑problems. ¡ • In ¡Divide ¡and ¡Conquer, ¡the ¡sub-­‑problems ¡are ¡independent ¡ of ¡each ¡other ¡ • In ¡contrast, ¡dynamic ¡programming ¡is ¡applicable ¡ when ¡the ¡ sub ¡ problems ¡ are ¡ overlapping . ¡ That ¡ is ¡ when ¡ solu*on ¡ of ¡ one ¡sub-­‑problem ¡may ¡be ¡required ¡to ¡solve ¡another ¡sub-­‑ problem. ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  3. Dynami mic ¡ ¡Programmi mming Dynamic ¡Programming ¡(shortened ¡as ¡DP) ¡ ¡ • ¡A ¡programming ¡technique ¡ ¡ • ¡Can ¡drama*cally ¡reduce ¡the ¡run*me ¡of ¡some ¡algorithms ¡ (but ¡ not ¡ all ¡ problem ¡ has ¡ DP ¡ characteris*cs) ¡ from ¡ ¡ exponen*al ¡to ¡polynomial. ¡ ¡ ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  4. Dynami mic ¡ ¡Programmi mming ¡ ¡(DP) • Dynamic ¡ Programming ¡ algorithm ¡ solves ¡ every ¡ sub ¡ sub-­‑ problem ¡just ¡once ¡and ¡saves ¡its ¡answer ¡in ¡a ¡table. ¡ ¡ ¡ • This ¡ avoids ¡ the ¡ work ¡ of ¡ re-­‑compu*ng ¡ the ¡ answer ¡ every ¡ *me ¡ ¡the ¡sub ¡sub-­‑problem ¡is ¡encountered. ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  5. Eleme ments ¡ ¡of ¡ ¡DP • Op#mal ¡ substructure : ¡ Dynamic ¡ Programming ¡ typically ¡ applied ¡to ¡ op#miza#on ¡problems . ¡ ¡ ¡ • In ¡such ¡problems ¡there ¡can ¡be ¡many ¡possible ¡solu*ons. ¡ ¡ • Each ¡solu*on ¡has ¡a ¡value ¡and ¡we ¡wish ¡to ¡find ¡a ¡solu*on ¡ with ¡the ¡op*mal ¡value. ¡ ¡ ¡ ¡ ¡ ¡In ¡DP ¡we ¡build ¡an ¡op*mal ¡solu*on ¡to ¡the ¡problem ¡from ¡ op*mal ¡ ¡ ¡solu*ons ¡to ¡sub-­‑problems. ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  6. Eleme ments ¡ ¡of ¡ ¡DP • Overlapping ¡ sub-­‑problems : ¡ A ¡ recursive ¡ algorithm ¡ for ¡ the ¡ problem ¡ solves ¡ the ¡ same ¡ sub-­‑problems ¡ over ¡ and ¡ over, ¡ rather ¡ than ¡always ¡genera*ng ¡new ¡sub-­‑problems. ¡ ¡ • DP ¡algorithms ¡take ¡advantage ¡of ¡overlapping ¡sub-­‑problems ¡by ¡ ¡-­‑ ¡ ¡solving ¡each ¡sub-­‑problem ¡once ¡and ¡ ¡ -­‑ storing ¡the ¡solu*on ¡in ¡a ¡table ¡where ¡it ¡can ¡be ¡looked ¡ ¡ ¡ ¡ ¡up ¡when ¡needed, ¡using ¡constant ¡*me ¡per ¡look ¡up. ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  7. Dynami mic ¡ ¡Programmi mming ¡ ¡ ¡ ¡ ¡Applica*ons: ¡ ¡ ¡ • Compu*ng ¡combina*ons ¡ ¡ • Assembly-­‑line ¡scheduling ¡ • Matrix-­‑chain ¡mul*plica*on ¡ • Longest ¡Common ¡Subsequence ¡ • …………………………………. ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  8. Fi Fibonacci ¡ ¡Numb mbers • Consider ¡the ¡algorithm ¡for ¡calcula*ng ¡Fibonacci ¡numbers: ¡ ¡ ¡ ¡ int ¡fib( ¡int ¡n ¡) ¡{ ¡ ¡ ¡ ¡ ¡if ¡( ¡n ¡< ¡2 ¡) ¡return ¡n; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡fib(n-­‑1) ¡+ ¡fib(n-­‑2); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ What ¡about ¡the ¡running ¡4me ¡of ¡this ¡algorithm? ¡ ¡ ¡ ¡ ¡ ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  9. Fi Fibonacci ¡ ¡Numb mbers • Consider ¡the ¡algorithm ¡for ¡calcula*ng ¡Fibonacci ¡numbers: ¡ ¡ ¡ ¡ int ¡fib( ¡int ¡n ¡) ¡{ ¡ ¡ ¡ ¡ ¡if ¡( ¡n ¡< ¡2 ¡) ¡return ¡n; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡fib(n-­‑1) ¡+ ¡fib(n-­‑2); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(fn) ¡= ¡ O (1.6 n ) ¡ ¡ ¡ • So ¡this ¡simple ¡func*on ¡will ¡take ¡exponen*al ¡*me! ¡WHY? ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  10. Fi Fibonacci ¡ ¡Numb mbers For ¡ example, ¡ the ¡ problem ¡ of ¡ compu*ng ¡ the ¡ Fibonacci ¡ sequence ¡ exhibits ¡overlapping ¡sub-­‑problems. ¡ ¡ fib(5) ¡ fib(4) ¡ fib(3) ¡ fib(2) ¡ fib(3) ¡ fib(1) ¡ fib(2) ¡ fib(2) ¡ fib(1) ¡ fib(1) ¡ fib(0) ¡ fib(1) ¡ fib(0) ¡ fib(1) ¡ fib(0) ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  11. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Dynami ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ mic ¡ ¡Programmi mming ¡ ¡Techniques ¡? ¡ ¡? ¡ Memoization Iterative Dynamic Programming Filling a table recursively “by need”. (Filling a table “from one corner”) Memoization of results of earlier calls avoids exponential blow-up. ¡ Obtained ¡from ¡Frank ¡Drew’s ¡slides ¡on ¡Dynamic ¡Programming ¡ Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡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