Lec01 Slides
Lec01 Slides
Lec01 Slides
100B:
Introduction to
Computational Thinking
and Data Science
6.100A LECTURE 1 6
6.100B Prerequisites
For
Mathematics Computer Science more
info: Global warming
CSE
Neuron
bursting
Science &
Engineering
Just optimizing
objective function is
often straightforward
750
Calorie
Capacity
I= [ , , , , , , , , ]
V= [ 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1]
𝑉 𝑖 × 𝐼 𝑖 . value
𝑖=0
𝑉 𝑖 × 𝐼 𝑖 . weight ≤ 𝑤
𝑖=0
Food wine beer pizza burger fries coke apple donut cake
Value 89 90 30 50 90 79 90 10 85
Cals 123 154 258 354 365 150 95 195 107
calorie_limit = 750
April 3, 2023 6.100B LECTURE 1 57
Brute Force Step 1:
Generate power set
def create_combinations(n):
"""Create a list of strings
each string is a sequence of 0's and 1's
the 1's are items to include in combination"""
if n == 0:
return ['']
else:
smaller_L = create_combinations(n-1)
full_L = []
for s in smaller_L:
full_L.append(s + '0')
full_L.append(s + '1')
return full_L
calorie_limit = 750
foods = build_menu(names, values, calories)
solution, value = brute_force(foods, calorie_limit)
def generate_foods(num_foods):
names = [f'food{n}' for n in range(num_foods)]
values = [random.randint(1, 100) for _ in range(num_foods)]
calories = [random.randint(1, 300) for _ in range(num_foods)]
return names, values, calories
▪ Alas, no
▪ 0/1 knapsack problem is inherently exponential
▪ Why? Because the power set grows that fast
◦ Power set of 𝑛 elements has 𝟐𝒏 combinations
◦ Recall that we represent each combination as a string of
𝒏 zeros and ones
◦ So the cost of generating the power set is 𝚯(𝒏𝟐𝒏 )
◦ Remaining brute-force algorithm performs Θ(𝑛) work for
each combination in the power set
▪ But don’t despair
5 minutes
Use greedy by value to allocate 1500 calories Use brute force to allocate 1500 calories
Total value of items taken = 574 Total value of items taken = 778
burger: <100, 354> wine: <89, 123>
chocolate: <100, 406> beer: <90, 154>
pizza: <95, 258> pizza: <95, 258>
beer: <90, 154> burger: <100, 354>
orings: <90, 190> cola: <79, 150> By cost and by density
wine: <89, 123> apple: <50, 95> both had an optimal
celery: <10, 15> cake: <85, 107> value of 699
juice: <80, 39>
carrot: <20, 25>
orings: <90, 190>
30 × 230 ≈ 30,000,000,000
COURSE INFORMATION
https://introcomp.mit.edu/spring23/information
Piazza forum
https://piazza.com/class/lckt7mgnhk0oa
Staff email
6.100-staff@mit.edu
April 3, 2023 6.100B LECTURE 1 86
Microquizzes
▪ Must take them in-class
▪ Best 3 out of 4, no make-ups
▪ Bring your charged laptop and charger
▪ Click submit on every question
▪ Submit a checkout password at the end
▪ Office hours are for any and all questions, not just
checkoffs
▪ If LA can’t answer your question, ask the TA in charge
▪ Grading
◦ Thresholds on total score guarantee certain letter grades
◦ Those below each threshold are considered individually
◦ We don’t assign extra work
◦ TAs will let us know if they regularly interact with you
◦ Email us ASAP if there are exceptional circumstances
Complementary Knapsack
Decision Trees
DYNAMIC PROGRAMMING