� 1 LECTURE 4: D&C, DYNAMIC PROGRAMMING ADVANCED ALGORITHMS
� 2 LECTURE 4 ANNOUNCEMENTS ▸ Homework 0 grading (close to done) ▸ Homework 1 posted; due on Monday Sep 10, 11:59 PM ▸ Start early! (Post comments/questions on canvas…) ▸ Unless immediate, argue why algorithm is correct ▸ Three steps — algorithm description, proof of correctness, complexity analysis
� 3 LECTURE 4 LAST CLASS ▸ Divide and conquer ▸ Break instance into smaller pieces, solve pieces separately , combine solutions ▸ Instances disjoint in examples we saw — not necessary (today) ▸ Merge-sort algorithm link on ▸ Recurrences homepage i Master theorem ▸ Guess and prove by induction Akira Bazzi Theorem ▸ Plug and chug ▸ Recurrence tree
� 4 LECTURE 4 DIVIDE AND CONQUER PROBLEM INSTANCE Divide step Solve sub-problems Combine/“conquer” FULL SOLUTION
� 5 LECTURE 4 TODAY’S PLAN ▸ More examples — divide and conquer ▸ Continue integer multiplication ▸ Median finding ▸ Introduction — dynamic programming
� 6 LECTURE 4 EXAMPLE 2: MULTIPLYING INTEGERS Question: given two n digit numbers, how efficiently can we multiply them? ▸ Elementary school algorithm: takes time O(n 2 ) ▸ Basic divide and conquer — recap
� 7 LECTURE 4 DIVIDE AND CONQUER n A = B = X Y Z W X Mz Mz Mz OO Mz O Some observations: A = 10 n /2 X + Y B = 10 n /2 Z + W D 7 L'm El AB = 10 n XZ + 10 n /2 ( YZ + XW ) + YW azn digits ▸ Divide and conquer alg: compute XZ, YZ, XW, YW , compute sum above ▸ Gives recurrence: T ( n ) = 4 T ( n /2) + O ( n )
� 8 LECTURE 4 SOLVING RECURRENCE Tht 4TH Oln 1 master theorem E g f Tfn 042 a chug Corl plug do better than elementary Can we school Arg
� 9 LECTURE 4 CAN WE DO BETTER? Reason to hope: we only need to find three quantities: XZ, (XW + YZ), YW
� 10 LECTURE 4 CAN WE DO BETTER? Reason to hope: we only need to find three quantities: XZ, (XW + YZ), YW r a Kartsuba’s observation: XW + YZ = ( X + Y )( W + Z ) − XZ − YW we need only THREE multiplications 3 TIZI TIM Oln
� 11 LECTURE 4 OVERALL RUNNING TIME TIM 3M g c n Cin 131C I 1347 C n 1 z 32T ng cn 1 log N I cnfltZztfzIt.r.JCatEHHcn.fzfzre aani.s.s 37171
� 12 LECTURE 4 EVEN BETTER? I 35 ▸ Can we do linear time? N m ▸ HW1 — will see something approaching linear K ite N 2 time alg Fourier Transform Ofm log n Fast a 42 problem if can do better we Open
� 13 LECTURE 4 COMPUTING THE MEDIAN Question: given an unsorted list A[0], A[1], …, A[n-1], find the (n/2)th smallest element output Ith smallest O n log n Sort the array 0cal Divide and conquer?
A n n medians of the pieces don't give much meaningful information l i
� 14 LECTURE 4 KEY INSIGHT: “APPROXIMATE MEDIAN” HELPS! Aln i AH AH tf TAIN D Ifs Af AIDAN Fddi 12 elements t 13 213
Observations now restrict search to we can 1 een sized array a 2.13 answer is need to be careful 2 not median of sub array but f 1h Smallest element i qb6n l a k find the Kth an array some Given Select A K smallest element A Z E Select select Bg a 1 all elements in Eu T Iz Oh 1 n time taken to find sub array B t C n t Ent E t Ept c'n l t Zz t E n
B t n 21 3 Select Ez Z How does recursive call work B 24313 B njrd smallest element of B x be Let a the is myth smallest element of B smallest element of B Z F B select wilt Rida A K select select B k approx median find a cursive call10h tides That a y I BIL F IAI B sat givesdetails new array
� 15 LECTURE 4 Trick MEDIAN OF MEDIANS IS APPROXIMATE MEDIAN! A In AIA Afi o 1 Fo 101Mt Tff Tfn
� 16 LECTURE 4 DIVIDE AND CONQUER — OVERVIEW ▸ Problem “cleanly” divides into not-too-many sub-problems ▸ Solutions easily (efficiently) combined ▸ Leads to simple, efficient solutions ▸ Analysis by induction, complexity by recurrences
� 17 LECTURE 4 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 next topic — dynamic programming
� 18 LECTURE 4 NEW PROBLEM: 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 . is A fol t A kid t A Es B sum to S find if any all subsets time 2 n
� 19 LECTURE 4 I 4 DIVIDE AND CONQUER f Aln D Alo T T T T ▸ Try 1: divide into two halves s o is.tl Sti TIE 1 n logan 1 s n togs
� 20 LECTURE 4 RUNNING TIME log n
� 21 LECTURE 4 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 . ▸ See if first element is picked or not I Aln B look fords in AID for a sum of look in AID s Afo a sum of Aln D Takes time 2h
� 22 LECTURE 4 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 . ▸ See if first element is picked or not ▸ Is this really divide and conquer?
� 23 LECTURE 4 RUNNING TIME
� 24 LECTURE 4 LOOKING MORE CLOSELY — EXAMPLE A[] = {1, 2, 3, 5, 7, 9, 10, 11}; S = 20
� 25 LECTURE 4 AVOIDING MULTIPLE SOLVES ON “SAME” INSTANCE ▸ Store answers! ▸ How much are we storing? ▸ Running time
Recommend
More recommend