Grokking Algorithms



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə66/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   62   63   64   65   66   67   68   69   ...   122
grokking-algorithms-illustrated-programmers-curious

Step 1 again:
The LP is the next cheapest node at $5.
Step 2 again:
 
Update the values of all of its neighbors.
Hey, you updated the price of both the drums and the guitar! That 
means it’s cheaper to get to the drums and guitar by following the edge 
from the LP. So you set the LP as the new parent for both instruments.


126
Chapter 7
 
 
I
 
 
Dijkstra’s algorithm
The bass guitar is the next cheapest item. Update its neighbors.
Ok, you finally have a price for the piano, by trading the guitar for the 
piano. So you set the guitar as the parent. Finally, the last node, the 
drum set.
Rama can get the piano even cheaper by trading the drum set for the 
piano instead. 
So the cheapest set of trades will cost Rama $35
.
Now, as I promised, you need to figure out the path. So far, you know 
that the shortest path costs $35, but how do you figure out the path? To 
start with, look at the parent for
 piano
.
The piano has drums as its parent. That means Rama trades the drums 
for the piano. So you follow this edge.


127
Trading for a piano
Let’s see how you’d follow the edges. 
Piano
has 
drums
as its parent.
And 
drums
has the LP as its parent.
So Rama will trade the LP for the drums. And of course, he’ll trade the 
book for the LP. By following the parents backward, you now have the 
complete path.
Here’s the series of trades Rama needs to make.


128

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   62   63   64   65   66   67   68   69   ...   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