Grokking Algorithms



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

I
 
 
Dynamic programming
Every dynamic-programming algorithm starts with a grid. Here’s a grid 
for the knapsack problem.
he rows of the grid are the items, and the columns are knapsack 
weights from 1 lb to 4 lb. You need all of those columns because they 
will help you calculate the values of the sub-knapsacks.
he grid starts out empty. You’re going to ill in each cell of the grid. 
Once the grid is illed in, you’ll have your answer to this problem! 
Please follow along. Make your own grid, and we’ll ill it out together. 
The guitar row
I’ll show you the exact formula for calculating this grid later. Let’s do a 
walkthrough irst. Start with the irst row.
his is the 
guitar
row, which means you’re trying to it the guitar into 
the knapsack. At each cell, there’s a simple decision: do you steal the 
guitar or not? Remember, you’re trying to ind the set of items to steal 
that will give you the most value. 
he irst cell has a knapsack of capacity 1 lb. he guitar is also 1 lb, 
which means it its into the knapsack! So the value of this cell is $1,500, 
and it contains a guitar.


165
The knapsack problem
Let’s star
t illing in the grid. 
Like this, each cell in the grid will contain a list of all the items that it 
into the knapsack at that point. 
Let’s look at the next cell. Here you have a knapsack of capacity 2 lb. 
Well, the guitar will deinitely it in there!
he same for the rest of the cells in this row. Remember, this is the irst 
row, so you have 
only
the guitar to choose from. You’re pretending
that 
the other two items aren’t available to steal right now.
At this point, you’re probably confused. 
Why
are you doing this for 
knapsacks with a capacity of 1 lb, 2 lb, and so on, when the problem 
talks about a 4 lb knapsack? Remember how I told you that dynamic 
programming starts with a small problem and builds up to the big 
problem? You’re solving subproblems here that will help you to solve 
the big problem. Read on, and things will become clearer.


166

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   77   78   79   80   81   82   83   84   ...   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