Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet83/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   79   80   81   82   83   84   85   86   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

I
 
 
Dynamic programming
You just updated your estimate! If you have a 4 lb knapsack, you can 
it at least $3,000 worth of goods in it. You can see from the grid that 
you’re incrementally updating your estimate.
The laptop row
Let’s do the same thing with the laptop! he laptop weighs 3 lb, so it 
won’t it into a 1 lb or a 2 lb knapsack. he estimate for the irst two cells 
stays at $1,500.
At 3 lb, the old estimate was $1,500. But you can choose the laptop 
instead, and that’s worth $2,000. So the new max estimate is $2,000!
At 4 lb, things get really interesting. his is an important part. he 
current estimate is $3,000. You can put the laptop in the knapsack, but 
it’s only worth $2,000.


169
The knapsack problem
Hmm, that’s not as good as the old estimate. But wai
t! he laptop 
weighs only 3 lb, so you have 1 lb free! You could put something in
this 1 lb.
What’s the maximum value you can it into 1 lb of space? Well, you’ve 
been calculating it all along.
According to the last best estimate, you can it the guitar into that 1 lb 
space, and that’s worth $1,500. So the real comparison is as follows.
You might have been wondering why you were calculating max values 
for smaller knapsacks. I hope now it makes sense! When you have
space let over, you can use the answers to those subproblems to igure 
out what will it in that space. It’s better to take the laptop + the guitar 
for $3,500. 


170
Chapter 9
 
 
I
 
 
Dynamic programming
he inal grid looks like this.
here’s the answer: the maximum value that will it in the knapsack is 
$3,500, made up of a guitar and a laptop!
Maybe you think that I used a diferent formula to calculate the value 
of that last cell. hat’s because I skipped some unnecessary complexity 
when illing in the values of the earlier cells. Each cell’s value gets 
calculated with the same formula. Here it is.
You can use this formula with every cell in this grid, and you should 
end up with the same grid I did. Remember how I talked about solving 
subproblems? You combined the solutions to two subproblems to solve 
the bigger problem.


171
Knapsack problem FAQ

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   79   80   81   82   83   84   85   86   ...   120




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish