Op#miza#on Problems,: John Gu7ag MIT Department of Electrical Engineering and Computer Science
Op#miza#on Problems,: John Gu7ag MIT Department of Electrical Engineering and Computer Science
John Gu7ag
MIT Department of Electrical Engineering and
Computer Science
6.0002 LECTURE 2 1
Relevant Reading for Today’s Lecture
§ Chapter 13
6.0002 LECTURE 2 2
The Pros and Cons of Greedy
§ Easy to implement
§ Computa<onally efficient
Ques<on 1
6.0002 LECTURE 2 3
Brute Force Algorithm
6.0002 LECTURE 2 4
Search Tree Implementa#on
6.0002 LECTURE 2 5
A Search Tree Enumerates Possibili#es
LeS-first, depth-first
enumera<on
Take Don’tTake
6.0002 LECTURE 2
Val = 170 Val = 120 Val = 140 Val = 90 Val = 80 Val = 30 Val = 50 Val = 0
Cal = 766 Cal = 766 Cal = 508 Cal = 145 Cal = 612 Cal = 258 Cal = 354 Cal = 0
6
Image © source unknown. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use.
6.0002 LECTURE 2 7
Computa#onal Complexity
§ Time based on number of nodes generated
§ Number of levels is number of items to choose from
§ Number of nodes at level i is 2i
§ So, if there are n items the number of nodes is
◦ ∑𝑖=0↑𝑖=𝑛▒2↑𝑖
↑𝑛+1 )
◦ I.e., O(2
§ An obvious op<miza<on: don’t explore parts of tree
that violate constraint (e.g., too many calories)
◦ Doesn’t change complexity
§ Does this mean that brute force is never useful?
◦ Let’s give it a try
6.0002 LECTURE 2 8
Header for Decision Tree Implementa#on
6.0002 LECTURE 2 9
Body of maxVal (without comments)
if toConsider == [] or avail == 0:
result = (0, ())
elif toConsider[0].getUnits() > avail:
result = maxVal(toConsider[1:], avail)
else:
nextItem = toConsider[0]
withVal, withToTake = maxVal(toConsider[1:],
avail - nextItem.getUnits())
withVal += nextItem.getValue()
withoutVal, withoutToTake = maxVal(toConsider[1:], avail)
if withVal > withoutVal:
result = (withVal, withToTake + (nextItem,))
else:
result = (withoutVal, withoutToTake)
return result
6.0002 LECTURE 2 10
Try on Example from Lecture 1
6.0002 LECTURE 2 11
Search Tree Worked Great
6.0002 LECTURE 2 12
Code to Try Larger Examples
import random
6.0002 LECTURE 2 13
Is It Hopeless?
6.0002 LECTURE 2 14
Dynamic Programming?
6.0002 LECTURE 2 15
Recursive Implementa#on of Fibonnaci
def fib(n):
if n == 0 or n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
fib(120) = 8,670,007,398,507,948,658,051,921
6.0002 LECTURE 2 16
Call Tree for Recursive Fibonnaci(6) = 13
fib(6)
fib(5) fib(4)
fib(1) fib(0)
6.0002 LECTURE 2 17
Clearly a Bad Idea to Repeat Work
6.0002 LECTURE 2 18
Using a Memo to Compute Fibonnaci
def fastFib(n, memo = {}):
"""Assumes n is an int >= 0, memo used only by
recursive calls
Returns Fibonacci of n"""
if n == 0 or n == 1:
return 1
try:
return memo[n]
except KeyError:
result = fastFib(n-1, memo) +\
fastFib(n-2, memo)
memo[n] = result
return result
6.0002 LECTURE 2 19
When Does It Work?
6.0002 LECTURE 2 20
What About 0/1 Knapsack Problem?
Ques<ons 2 and 3
6.0002 LECTURE 2 21
Search Tree
Op<mal substructure?
Overlapping subproblems?
Take Don’tTake
6.0002 LECTURE 2
Val = 170 Val = 120 Val = 140 Val = 90 Val = 80 Val = 30 Val = 50 Val = 0
Cal = 766 Cal = 766 Cal = 508 Cal = 145 Cal = 612 Cal = 258 Cal = 354 Cal = 0
22
A Different Menu
6.0002 LECTURE 2
23
Need Not Have Copies of Items
6.0002 LECTURE 2 24
Search Tree
§ Each node = <taken, leS, value, remaining calories>
6.0002 LECTURE 2 25
What Problem is Solved at Each Node?
6.0002 LECTURE 2 26
Overlapping Subproblems
6.0002 LECTURE 2 27
Modify maxVal to Use a Memo
6.0002 LECTURE 2 28
Performance
6.0002 LECTURE 2 29
How Can This Be?
§ Problem is exponen<al
§ Have we overturned the laws of the universe?
§ Is dynamic programming a miracle?
§ No, but computa<onal complexity can be subtle
§ Running <me of fastMaxVal is governed by number of
dis<nct pairs, <toConsider, avail>
◦ Number of possible values of toConsider bounded
by len(items)
◦ Possible values of avail a bit harder to characterize
◦ Bounded by number of dis<nct sums of weights
◦ Covered in more detail in assigned reading
6.0002 LECTURE 2 30
Summary of Lectures 1-2
6.0002 LECTURE 2 31
The “Roll-over” Op#miza#on Problem
Score = ((60 – (a+b+c+d+e))*F + a*ps1 + b*ps2 + c*ps3 + d*ps4 + e*ps5
Objec<ve:
Given values for F, ps1, ps2, ps3, ps4, ps5
Find values for a, b, c, d, e that maximize score
Constraints:
a, b, c, d, e are each 10 or 0
a + b + c + d + e ≥ 20
6.0002 LECTURE 2 32
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.