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