� 1 LECTURE 6: DP, GREEDY ALGORITHMS ADVANCED ALGORITHMS
� 2 LECTURE 6 ANNOUNCEMENTS ▸ Homework 1 due on Monday Sep 10, 11:59 PM ▸ Video issues last class ▸ Comment on notes — JeffE’s material
� 3 LECTURE 6 LAST CLASS ▸ Dynamic programming ▸ Recursive algorithms — observe that same sub-problems solved many times ▸ Key idea: avoid re-solving by storing answers! ▸ How many “different problem instances” arise? ▸ Examples: subset sum and coin change problems s I t n emojis
� 4 LECTURE 6 OVERLAP IN RECURSIVE CALLS if Afi are not integers all bet If A = {1, 2, 3, 5, 7, 11, 14, 15} , S = 20 safe Don’t use first element Use first element A’ = {2, 3, 5, 7, 11, 14, 15} , S’ = 19 A’’ = {5, 7, 11, 14, 15} , S’ = 17 A’’ = {5, 7, 11, 14, 15} , S’ = 17 Key observation: every sub-problem has a suffix of A as the array, and some target sum in the range [0,…,S]. Thus only n(S+1) sub-problems!
� 5 LECTURE 6 TODAY’S PLAN ▸ Conclude DP — a couple more examples from a high level ▸ Greedy algorithms
� 6 LECTURE 6 RECAP: DYNAMIC PROGRAMMING ▸ Design recursive algorithm ▸ Observe that number of distinct sub-problems is “small” ▸ Store answers! (memorize) in Hwa more ▸ Top-down vs. Bottom-up ▸ Challenge in using DP: formalizing sub-problems
� 7 LECTURE 6 POPULAR E.G.: LONGEST INCREASING SUB-SEQUENCE Problem: given a sequence of numbers A[0], A[1], …, A[n-1], find longest “sub-sequence” whose elements are increasing AfD A 147 Alto A 107 ▸ Natural recursive algorithm? 10 2 f 7y 3g cases based on whether the first Split into or not in the sub sequence element is call on remaining list recursive
procedure Aln B find target All M's Afo Afo that includes increasing sub seg Answer to original problem Afi Afia Aln D Mls miax all 4 3 Mls 411,2 3 write recursion for 1,2 3 Mls 3 GI Mis l 2,3 Atl Aln B Ato Mls His t Afi AH find Ali Alief Aln B MI f m ou put i Correctness increasing that includes AT Take anynsubseg A fo Afif Afif Ali Let it fo be A a that Afi so it will be Alo We know in the recursive considered procedure
can argue inductively that Now we Mislaid Ali HI rtl It I r.u.i.by sftaIhifhey student's suggestion does Not work a bsg I Ml InD o
� 8 LECTURE 6 #(SUB-PROBLEMS) AND RUN TIME sub problems n Running time n otherwise is key MIS procedure isn't defining Moira correct should be careful about defining the right sub problem
� 9 LECTURE 6 TRAVELING SALESMAN PROBLEM Problem: given undirected graph with edge wts, find a path of least total weight that visits every vertex precisely once and returns to start.
� 10 LECTURE 6 can TRAVELING SALESMAN PROBLEM ni p Problem: given undirected graph with edge wts, find a path of least total weight that visits every vertex precisely once and returns to start. check all permutations see ▸ Trivial algorithm? which one has least cost F 3,6 fin D n Nn al on Dynamicorithn 2n nu define sub problem very carefully key
� 11 LECTURE 6 TRAVELING SALESMAN PROBLEM Problem: given undirected graph with edge wts, find a path of least total weight that visits every vertex precisely once and returns to start. ▸ Trivial algorithm? ▸ Many heuristics for geometric cases
� 12 LECTURE 6 RECURSIVE ALGORITHM
� 13 LECTURE 6 SUB-PROBLEMS, RUNNING TIME
� 14 LECTURE 6 SUB-PROBLEMS, RUNNING TIME
� 15 LECTURE 6 CAN WE DO BETTER? ▸ Likely not — NP hard problem ▸ We will see that finding “approximately good” solution possible!
� 16 LECTURE 6 GREEDY ALGORITHMS ▸ Greedy == “myopic” ▸ Algorithm makes sequence of decisions — chooses best at each step, without taking future into account Greedy algorithms almost neues work give insights about how to solve often sometimes they actually
� 17 LECTURE 6 EXAMPLE Scheduling: given set of jobs with processing times p 1 , p 2 , …, p n , find an order of processing so that sum of completion times is minimized 2 4 i 3 1 i C 7 C 1 I 3 i Cz 10 Cg first small do jobs with Intuitive idea in order of increasing pi 9rdgfnh.fm Process
� 18 LECTURE 6 NATURAL HEURISTIC?
� 19 LECTURE 6 CORRECTNESS Processing jobs in this order is the best Clem possible Contradict Suppose the optimal order Proof 1 U could not have u v im iz it been opt order I suppose we swap j ti tiz t ti tti.tpizmtiztk.TK Pi Epi i similarly pi Epi i
� 20 LECTURE 6 CORRECTNESS — PROOF 2
� 21 LECTURE 6 EXAMPLE — MATCHING Matching: suppose we have n children, n gifts and “happiness” values H ij . Assign gifts to children to maximize “total happiness” value ofgift Gj to G C Hy child C Ga Cz 0 c o i i i l i y C x Gn o n
� 22 LECTURE 6 GREEDY ALGORITHM Go through children Ci Cz a For each child assign O C o gift from the nomaining gifts that has the most value Xcx Greedy does not always X G 10 solution find best
� 23 LECTURE 6 CORRECTNESS
� 24 LECTURE 6 CORRECTNESS
Recommend
More recommend