Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet162/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   158   159   160   161   162   163   164   165   ...   266
Bog'liq
2 5296731884800181221

Figure 9-2.  Gradually uncovering the hidden DAG. Nodes are labeled with their final distances. Because weights are 
positive, the backward edges (dashed) cannot influence the result and are therefore irrelevant
Predictably enough, we now hit the major gap in the solution: It’s totally circular. In uncovering the basic problem 
structure (decomposing into subproblems or finding the hidden DAG), we’ve assumed that we’ve already solved the 
problem. The reasoning has still been useful, though, because we now have something specific to look for. We want to 
find the ordering—and we can find it with our trusty workhorse, induction!
Consider, again, Figure 
9-2
. Assume that the highlighted node is the one we’re trying to identify in our inductive 
step (meaning that the earlier ones have been identified and already have correct distance estimates). Just like in the 
ordinary DAG shortest path problem, we’ll be relaxing all out-edges for each node, as soon as we’ve identified it and 
determined its correct distance. That means that we’ve relaxed the edges out of all earlier nodes. We haven’t relaxed 
the out-edges of later nodes, but as discussed, they can’t matter: the distance estimates of these later nodes are upper 
bounds, and the back-edges have positive weights, so there’s no way they can contribute to a shortcut.
This means (by the earlier relaxation properties or the discussion of the DAG shortest path algorithm in  
Chapter 8) that the next node must have a correct distance estimate. That is, the highlighted node in Figure 
9-2
 must 
by now have received its correct distance estimate, because we’ve relaxed all edges out of the first three nodes. This 
is very good news, and all that remains is to figure out which node it is. We still don’t really know what the ordering is, 
remember? We’re figuring out the topological sorting as we go along, step by step.
There is only one node that could possibly be the next one, of course:
3
 the one with the lowest distance estimate
We know it’s next in the sorted order, and we know it has a correct estimate; because these estimates are upper 
bounds, none of the later nodes could possibly have lower estimates. Cool, no? And now, by induction, we’ve solved 
the problem. We just relax all out-edges of each node in distance order—which means always taking the one with the 
lowest estimate next.
This structure is quite similar to that of Prim’s algorithm: traversal with a priority queue. Just as in Prim’s, we 
know that nodes we haven’t discovered in our traversal will not have been relaxed, so we’re not (yet) interested in 
them. And of the ones we have discovered (and relaxed), we always want the one with the lowest priority. In Prim’s 
algorithm, the priority was the weight of the edge linking back to the traversal tree; in Dijkstra’s, the priority is the 
distance estimate. Of course, the priority can change as we find shortcuts (just like new possible spanning tree edges 
could reduce the priority in Prim’s), but just like in Listing 7-5, we can simply add the same node to our heap multiple 
times (rather than trying to modify the priorities of the heap entries), without compromising correctness or running 
time. The result can be found in Listing 9-3. Its running time is loglinear, or, more specifically, Q((m+n) lg n),  
where m is the number of edges and n the number of nodes. The reasoning here is that you need a (logarithmic) 
heap operation for (1) each node to be extracted from the queue and (2) each edge to be relaxed.
4
 As long as you have 
W(n) edges, which you will for graphs where you can reach Q(n) nodes from the start node, the running time can be 
simplified to Q(m lg n).
3
Well, I’m assuming distinct distances here. If more than one node has the same distance, you could have more than one candidate. 
Exercise 9-2 asks you to show what happens then.
4
You may notice that edges that go back into S are also relaxed here in order to keep the code simple. That has no effect on 
correctness or asymptotic running time, but you’re free to rewrite the code to skip these nodes if you want.


Chapter 9 

 From a to B with edsger and Friends
196

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   158   159   160   161   162   163   164   165   ...   266




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