� 1 LECTURE 5: DYNAMIC PROGRAMMING ADVANCED ALGORITHMS
� 2 LECTURE 5 ANNOUNCEMENTS ▸ Homework 1 due on Monday Sep 10, 11:59 PM ▸ Instructor office hours Thursday -> Friday (1 - 2 pm) ▸ Comment on notes — JeffE’s material ▸ Solutions to HW0 on course page birds ▸ Pecking order problem of HW1 vertices O O could be cycles O O
� 3 LECTURE 5 LAST CLASS ▸ Divide and conquer ▸ Break instance into smaller pieces, solve pieces separately , combine solutions (*) 58 n ▸ Merge-sort, multiplying n digit numbers (time << n 2 ) ▸ Median (and k ’th smallest element) in O(n) time (no sorting!) ▸ Key idea: approximate median to break array into “nearly equal” pieces 1 ▸ All analyses by writing recursion, solving (tree, plug-and-chug, …) Master theorem Notes on homepage Akira Bazzi theorem Jeff E's notes
� 4 LECTURE 5 OVERLAP IN RECURSIVE CALLS ▸ Divide & conquer: usually sub-problems don’t interact “much” ▸ What if they do? Can run into wastefulness — solving same sub-problem many times… ▸ Motivation for dynamic programming
� 5 LECTURE 5 TODAY’S PLAN ▸ Continue example — subset sum ▸ Knapsack problem ▸ Traveling salesman
Sn s � 6 LECTURE 5 L Alo All f s I Aln i RECAP: SUBSET SUM Problem: given a set of non-negative integers A[0], A[1], …, A[n-1], and an integer S , find if there is a subset of A[i] whose sum is S . ▸ Trivial algorithm: 2 n time — check all subsets ▸ Sometimes better: divide into two halves — check all possibilities for how sum is divided fl nitlogg It T ( n ) = S ⋅ T ( n /2) ⟹ T ( n ) = S log 2 n = n log 2 S 2 7 4 15 i 5 3 5 30 22 8 29 1
� 7 LECTURE 5 Problem: given a set of non-negative integers A[0], A[1], …, A[n-1], TRY 2 and an integer S , find if there is a subset of A[i] whose sum is S . time on whenwehauf.in ▸ See if first element is picked or not Afi 1 Afia tafia S can write Suppose we correctness is we immediate Alo is used Alo is NOT usedto write arepdqkijy.ua.lt fw E f can be written as see if S Alo can write See if we An i Afi using 5 using All Aln i I
� 8 LECTURE 5 when we have runtime RUNNING TIME n c m A fit's Thi I 2T l 1 n n Th F fin Fast theomania In n 2 t l t 2 t 2 n almost 2 2 EE as bad as brute force
� 9 LECTURE 5 LOOKING MORE CLOSELY — EXAMPLE TE A[] = {1, 2, 3, 5, 7, 9, 10, 11}; S = 20 t is b 19 E3 B 3,5 20 3,51 1 7 3,5 18 3 5.3 3 s 20 I L I N s 17 17 sit 5,7 1 start index _2 Sum needed 17
� 10 LECTURE 5 AVOIDING MULTIPLE SOLVES ON “SAME” INSTANCE ▸ Store answers! How many different sub problems ▸ How much are we storing? one have 7 cc 2 can ▸ Running time if I possibilities any n possible of distinct instances is E sa n
thm Mo di In each recursive step first check if we have solved this so read off instance before If the answer we store the answer after If not computation can be stored in an Answers to computation n Sti array start index needed Sam
� 11 LECTURE 5 subtle point IS THIS POLYNOMIAL? we say poly time algorithm mean we when INPUT SIZE in polynomial go 2 S S an i all 01 og att flog aft flog.am if ilg e udaiistn n.SE Runningtime andsaySE C fznz.sn Input size an n I fan n 2 Running time
� 12 LECTURE 5 “KNAPSACK” / COIN CHANGE Problem: suppose we are given coins of denominations 1c, 5c, 10c, 20c, 25c, 50c. Make change for $1.90 using the fewest # of coins. a 206 201C ▸ Heuristics? 50 d 50k 254,104,5 50 that you CAN use use the largest coin Atways Greedy E
� 13 LECTURE 5 denominations do d dn RECURSIVE ALGORITHM S target sum S use do once The do twice on't used 1 call idn B and eachis a recursive d s using 7 3 tank it do using and 3 S 2do Hans 2 i 2tans3 n
� 14 LECTURE 5 RUNNING TIME are of the type recursive calls All set of denominations S I D 1 do suffix of Sum needed 01ns needed to store answers Space Of n S Time needed Edo
� 15 LECTURE 5 GENERAL IDEA OF DYNAMIC PROGRAMMING ▸ Write down recursive algorithm ▸ Observe “common sub-problems” in recursion ▸ Capture sub-problems “succinctly”
� 16 LECTURE 5 TRAVELING SALESMAN PROBLEM Problem: given an undirected graph with edge weights, find a path that visits every vertex precisely once and returns to start.
� 17 LECTURE 5 TRAVELING SALESMAN PROBLEM Problem: given an undirected graph with edge weights, find a path that visits every vertex precisely once and returns to start. ▸ Trivial algorithm?
� 18 LECTURE 5 TRAVELING SALESMAN PROBLEM Problem: given an undirected graph with edge weights, find a path that visits every vertex precisely once and returns to start. ▸ Trivial algorithm? ▸ Many heuristics for geometric cases
� 19 LECTURE 5 RECURSIVE ALGORITHM
� 20 LECTURE 5 RECURSIVE ALGORITHM — CORRECTNESS
� 21 LECTURE 5 RECURSIVE ALGORITHM — CORRECTNESS
� 22 LECTURE 5 SUB-PROBLEMS, RUNNING TIME
� 23 LECTURE 5 SUB-PROBLEMS, RUNNING TIME
� 24 LECTURE 5 CAN WE DO BETTER? ▸ Likely not — NP hard problem ▸ We will see that finding “approximately good” solution possible!
Recommend
More recommend