Grokking Algorithms



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

 
I
 
 
Dynamic programming
You have three items that you can put into the knapsack.
What items should you steal so that you steal the maximum money’s 
worth of goods? 
The simple solution 
he simplest algorithm is this: you try every possible set of goods and 
ind the set that gives you the most value.
his works, but it’s really slow. For 3 items, you have to calculate 8 
possible sets. For 4 items, you have to calculate 16 sets. With every 
item you add, the number of sets you have to calculate doubles! his 
algorithm takes O(2^
n
) time, which is very, very slow.


163
The knapsack problem
hat’s impractical for any reasonable number of goods. In chapter 8, 
you saw how to calculate an
approximate 
solution. hat solution will be 
close to the optimal solution, but it may not be the optimal solution.
So how do you calculate the optimal solution?
Dynamic programming
Answer: With dynamic programming! Let’s see how the dynamic-
programming algorithm works here. Dynamic programming starts by 
solving subproblems and builds up to solving the big problem.
For the knapsack problem, you’ll start by solving the problem for 
smaller knapsacks (or “sub-knapsacks”) and then work up to solving 
the original problem.
Dynamic programming is a hard concept, so don’t worry if you don’t get it 
right away.
We’re going to look at a lot of examples.
I’ll start by showing you the algorithm in action irst. Ater you’ve seen 
it in action once, you’ll have a lot of questions! I’ll do my best to address 
every question. 


164
Chapter 9
 
 

Download 6,4 Mb.

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