/
CS141:WEEK 6 DISCUSSION Dynamic Programming CS141:WEEK 6 DISCUSSION Dynamic Programming

CS141:WEEK 6 DISCUSSION Dynamic Programming - PowerPoint Presentation

zoe
zoe . @zoe
Follow
65 views
Uploaded On 2023-11-07

CS141:WEEK 6 DISCUSSION Dynamic Programming - PPT Presentation

Huffman Coding HUFFMAN CODING EXAMPLE CHARACTER OCCURENCE A 5 B 13 C 7 D 15 E 4 A5 B13 C7 D15 E4 HUFFMAN CODING EXAMPLE CHARACTER OCCURENCE A 5 B 13 C 7 D 15 E 4 A5 ID: 1029931

sfo candy src cost candy sfo cost src atl mem lax cutting city length dynamic coding dest huffman flight

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS141:WEEK 6 DISCUSSION Dynamic Programm..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. CS141:WEEK 6 DISCUSSIONDynamic ProgrammingHuffman Coding

2. HUFFMAN CODING EXAMPLECHARACTEROCCURENCEA5B13C7D15E4A:5B:13C:7D:15E:4

3. HUFFMAN CODING EXAMPLECHARACTEROCCURENCEA5B13C7D15E4A:5B:13C:7D:15E:4E:4A:59

4. HUFFMAN CODING EXAMPLECHARACTEROCCURENCEA5B13C7D15E4A:5B:13C:7D:15E:4E:4A:59C:716

5. HUFFMAN CODING EXAMPLECHARACTEROCCURENCEA5B13C7D15E4A:5B:13C:7D:15E:4E:4A:59C:716B:13D:1528

6. HUFFMAN CODING EXAMPLECHARACTEROCCURENCEA5B13C7D15E4A:5B:13C:7D:15E:4E:4A:59C:716B:13D:152844

7. HUFFMAN CODING EXAMPLECHARACTEROCCURENCEENCODINGA5011B1310C700D1511E4010E:4A:59C:716B:13D:15284401001101

8. DYNAMIC PROGRAMMINGTry all possible solutions Store the solutions to subproblems in an extra data structureUse that data structure to solve the problem efficiently

9. Example: Cutting the CandyProblem: We are candy merchants. We have candy bar of total length n, we want to sell all of it. We can cut pieces from. Each piece of candy with length i is worth .Want: Maximize our revenue by cutting pieces from our big candy bar in an optimal way (So we can sell some candy of total length n for more money)Input: int n: total amount (length) of candy we want to sellint[] p: each element of p represents the value of a piece of length i 

10. Cutting the Candy: Example RunTaken from: https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture12.pdf

11. Cutting the Candy: A Naïve SolutionTry every possible cutting strategy recursively!First cut a piece off left end of the candy of size iThen, find the optimal way to cut the remainder of the candyTry with all possible values of iWhat would be the type of time complexity?

12. Cutting the Candy: Dynamic Programming SolutionWe can do better!In the naïve solution, we are calculating the same values many many times!What if we stored some values in an additional array and used them when necessary?

13. Cutting the Candy: Dynamic Programming SolutionIdea: Keep an array r of size n+1Each element i of r, , represents the the maximum revenue that can be achieved with pieces whose size is smaller than or equal to i onlyHow can we populate r array? What is equal to? 

14. Cutting the candy: Dynamic Programming SolutionWe calculate the solutions for smaller rods first For each candy length value j, we find the optimal cuttingWe do this by iterating through all possible pieces At each step, we maximize the value of candy length j. : the optimal solution of subproblem with candy length (we record this to use later) gives the optimal solution to our problem! 

15. Cutting the Candy: Dynamic Programming SolutionWhat is the time complexity of this algorithm?Are we better off?

16. Cutting the Candy: Example Runn=4r[0]=0r[1]=1r[2]=5r[3]=8r[4]=10

17. Example: Finding Cheapest Flight (with some/many stopover)Want to fly for Los Angeles (LAX) to Atlanta(ATL)Direct flightOnly 1 Stopover at Denver, Chicago, DC, San Francisco….2 Stopovers at Denver and Chicago, San Francisco and DC, ….

18. Problem Statement:Cheapest flight within k stopsWe have a source(src) and destination(dest) locationGiven the cost of the direct flights of different citiesWe want to go from LAX to ATLDirect Flight = $300Is there any cheaper way to get there?CostLAXATLDCSFO….LAX030035050ATL3500250200DC450500500SFO100200500…

19. Problem Statement:Cheapest flight within k stops (cont.)1 StopoverLAX - DC - ATL = 350 + 50 = $400LAX - SFO – ATL = 50 + 200 = $2502 StopoverLAX - SFO - DC - ATL = 50 + 50 + 50 = $150LAX - DC - SFO - ATL = 350 + … LAXATLDCSFO….LAX030035050ATL3500250200DC450500500SFO100200500…

20. Naïve Solutionans = +inffor k = 0 to K ans = min(ans, findFlights(src, dest, k))return ansfindFlights(src, dest, k) if (k==0) return Cost[src][dest] cost = +inf for city in (cities with a direct flight from src) temp = findFlights(city, dest, k-1) + Cost[src][city] cost = min(cost, temp) return cost

21. Naïve Solution: AnalysisFor large K, we would have multiple calls with same src and k for the functionHow to solve this issue?Memorization!!!

22. Naïve -> Dynamic ProgrammingDestination city is constantWe can just store the answer of the (src cities, k) pairLet’s add a 2D array mem(city, int k)

23. Dynamic ProgrammingfindFlights(src, dest, k) if (k==0) return mem[src][k] = Cost[src][dest] cost = +inf for city in (cities with a direct flight from src) temp = mem[city][k-1] + Cost[src][city] cost = min(cost, temp) mem[src][k] = cost return cost

24. LAXATLDCSFOLAX030035050ATL3500250200DC450500500SFO100200500K =012LAX300250150ATL00DC50700SFO200100k=1LAX-> (DC 0->ALT ) = LAX-> DC + mem[DC][0] = 350 + 50 = 400LAX->(SFO 0->ATL) = LAX->SFO + mem[SFO][0] = 50 + 200 = 250DC-> (LAX 0 ->ATL) = DC ->LAX + mem[LAX][0] = 450 + 300DC-> (SFO 0->ATL) = DC->SFO + mem[SFO][0] = 500 + 200SFO-> (LAX 0->ATL) = SFO -> LAX + mem[LAX][0] = 100 + 300SFO-> (DC 0->ATL) = SFO->DC + mem[dc][0] = 50 + 50 = 100LAX ->( DC 1-> ATL) = LAX->DC + mem[DC][1] = 350 + 700LAX-> (SFO 1-> ATL) = LAX ->SFO + mem[SFO][1]= 50 + 100 = 150LAX -> (DEN 4-> ATL) = LAX ->DEN + mem[DEN][4]=