0% found this document useful (0 votes)
71 views23 pages

Algorithm Design and Analysis (ADA) : Dynamic Programming I

This document discusses an algorithm for solving the problem of picking balls of maximum possible weight without selecting adjacent balls. It begins by introducing a greedy approach that does not always find the optimal solution. It then realizes the optimal solution can be found by considering two possibilities - including or excluding the last ball. This leads to a recursive algorithm that considers both possibilities. However, the runtime of this recursive algorithm is very poor due to solving overlapping subproblems repeatedly. The document then introduces memoization to cache previously computed subsolutions, eliminating redundancy and resulting in an efficient dynamic programming solution with linear runtime.

Uploaded by

Ananya Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views23 pages

Algorithm Design and Analysis (ADA) : Dynamic Programming I

This document discusses an algorithm for solving the problem of picking balls of maximum possible weight without selecting adjacent balls. It begins by introducing a greedy approach that does not always find the optimal solution. It then realizes the optimal solution can be found by considering two possibilities - including or excluding the last ball. This leads to a recursive algorithm that considers both possibilities. However, the runtime of this recursive algorithm is very poor due to solving overlapping subproblems repeatedly. The document then introduces memoization to cache previously computed subsolutions, eliminating redundancy and resulting in an efficient dynamic programming solution with linear runtime.

Uploaded by

Ananya Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Algorithm Design and Analysis

(ADA)
CSE222
Dynamic Programming I
29 1 21
The First Example

2 5 6 4 3 5

Problem : Pick balls of maximum possible


weight ; No two adjacent balls
The First Example : An Intuitive ‘Greedy’ Approach

x v x
x r
Mon Un Un
2 5 6 4 3 5

Attempt I : Pick the ball of maximum weight


unless you have picked its neighbors and
continue… 13
Greedy gives
The First Example : Divide and Conquer

2 5 6 4 3 5

use 4 recursions
i 4 T M2 C
Tcn
OCny
Understanding the Optimal Solution

t
2 5 6 4 5 3
Observation : Last ball is not part of the optimal
solution
Understanding the Optimal Solution

x
y
2 5 6 4 5 3
Last ball is not part of the optimal solution

Solution same even if last ball removed from


instance
Understanding the Optimal Solution

x w

2 4 6 4 3 5
Last ball is part of the optimal solution
So, second last ball cannot be !
Understanding the Optimal Solution

r
2 4 6 4
I 3 5
Last ball is part of the optimal solution
Hence, second last ball cannot be !
Remaining green balls form an optimal solution to
the remaining instance (Why?) Proof by contradiction
Towards an Algorithm

Upshot : The optimal solution for balls 𝑏1 , 𝑏2 , ⋯ 𝑏𝑛


can look only two different ways –
Towards an Algorithm

Upshot : The optimal solution for balls 𝑏1 , 𝑏2 , ⋯ 𝑏𝑛


can look only two different ways –
1. 𝑏𝑛 not in the solution : Then the overall
solution is just the solution with balls
𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−1
Towards an Algorithm

Upshot : The optimal solution for balls 𝑏1 , 𝑏2 , ⋯ 𝑏𝑛


can look only two different ways –
1. 𝑏𝑛 not in the solution : Then the overall
solution is just the solution with balls
𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−1

2. 𝑏𝑛 is in the solution : Then overall solution is


solution with balls 𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−2 plus 𝑏𝑛
Towards an Algorithm

Trouble : We do not know the optimal solution


So, we do not know which option to take !
Towards an Algorithm

Trouble : We do not know the optimal solution


So, we do not know which option to take !

Possible solution : Try both the possibilities and


return the best of the two
A Recursive Algorithm
SelectBalls (𝑏1 , 𝑏2 , ⋯ 𝑏𝑛 )
If n==1, return 𝑏31 Else
max 0 weight by
𝑤1 = SelectBalls(𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−1 )
𝑤2 = SelectBalls(𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−2 ) + weight
of 𝑏𝑛

Return max{𝑤1 , 𝑤2 }
A Recursive Algorithm : Runtime
Tcn Runtime for
SelectBalls (𝑏1 , 𝑏2 , ⋯ 𝑏𝑛 ) by.bz bn
If n=1, return 𝑏1 Else
T n 1 n e t Tcn 2 Cc
𝑤1 = SelectBalls(𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−1 )
𝑤2 = SelectBalls(𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−2 ) + weight
of 𝑏𝑛
TG p
I
Return max{𝑤1 , 𝑤2 } Golden Ratio
Why is the Runtime So Horrible ?
Soleil
l
Sola y Solar 2

skcn
Gg
sfsncn.gg
The MOST important insight

Question : How many distinct subproblems is this


algorithm really solving ? h

Why2 There is a subproblem for


b bz hi bn
every prefix of in
Eliminating Redundancy
Obvious Fix : Cache already computed
subproblem values in an array and look them
up in 𝑂(1) time if available ; otherwise recurse

Memoization
Eliminating Redundancy
Tab.LI 0ptsoen
Tab : Array of size 𝑛
by.bz obi
SelectBalls (𝑏1 , 𝑏2 , ⋯ 𝑏𝑛 ) of
If Tab[n] is valid return Tab[n] else

If n==1, Tab[1]= weight of 𝑏1 Else

𝑤1 = SelectBalls(𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−1 )
𝑤2 = SelectBalls(𝑏1 , 𝑏2 , ⋯ 𝑏𝑛−2 ) + weight

L
Tab[n] = max{𝑤1 , 𝑤2 }
of 𝑏𝑛
Eliminating Redundancy : Even Better Solution
Tab : Array of size 𝑛
SelectBalls (𝑏1 , 𝑏2 , ⋯ 𝑏𝑛 )
0 Tab Il max 0 weight bis
Tab o

i 2 to n
for Tab i i
Tab it mae
Tab i 2 t weight bi
of
Recovery the Solution

Noextraspi g

building the tab i


0 O O O O O
Special Case of Independent Set problem

o o o O
o

You might also like