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