Grokking Algorithms



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə82/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   78   79   80   81   82   83   84   85   ...   122
grokking-algorithms-illustrated-programmers-curious

 
 
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 
The simplest algorithm is this: you try every possible set of goods and 
find the set that gives you the most value.
This 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! This 
algorithm takes O(2^
n
) time, which is very, very slow.


163
The knapsack problem
That’s impractical for any reasonable number of goods. In chapter 8, 
you saw how to calculate an
 approximate 
solution. That 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 first. After you’ve seen 
it in action once, you’ll have a lot of questions! I’ll do my best to address 
every question. 


164

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   78   79   80   81   82   83   84   85   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin