Grokking Algorithms


Optimizing your travel itinerary



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə88/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   84   85   86   87   88   89   90   91   ...   122
grokking-algorithms-illustrated-programmers-curious

Optimizing your travel itinerary
Suppose you’re going to London for a nice vacation. You have two days 
there and a lot of things you want to do. You can’t do everything, so you 
make a list.


176
Chapter 9
 
 
I
 
 
Dynamic programming
For each thing you want to see, you write down how long it will take 
and rate how much you want to see it. Can you figure out what you 
should see, based on this list? 
It’s the knapsack problem again! Instead of a knapsack, you have a 
limited amount of time. And instead of stereos and laptops, you have a 
list of places you want to go. Draw the dynamic-programming grid for 
this list before moving on.
Here’s what the grid looks like.
Did you get it right? Fill in the grid. What places should you end up 
seeing? Here’s the answer.


177
Knapsack problem FAQ
Handling items that depend on each other
Suppose you want to go to Paris, so you add a couple of items on
the list.
These places take a lot of time, because first you have to travel from 
London to Paris. That takes half a day. If you want to do all three items, 
it will take four and a half days.
Wait, that’s not right. You don’t have to travel to Paris for each item. 
Once you’re in Paris, each item should only take a day. So it should be 
one day per item + half a day of travel = 3.5 days, not 4.5 days.
If you put the Eiffel Tower in your knapsack, then the Louvre becomes 
“cheaper”—it will only cost you a day instead of 1.5 days. How do you 
model this in dynamic programming?
You can’t. Dynamic programming is powerful because it can solve 
subproblems and use those answers to solve the big problem. 
Dynamic 
programming only works when each subproblem is discrete—when it 
doesn’t depend on other subproblems. 
That means there’s no way to 
account for Paris using the dynamic-programming algorithm. 

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   84   85   86   87   88   89   90   91   ...   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