Source code online books for professionals by professionals



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

Listing 9-3.  Dijkstra’s Algorithm
from heapq import heappush, heappop
 
def dijkstra(G, s):
    D, P, Q, S = {s:0}, {}, [(0,s)], set()      # Est., tree, queue, visited
    while Q:                                    # Still unprocessed nodes?
        _, u = heappop(Q)                       # Node with lowest estimate
        if u in S: continue                     # Already visited? Skip it
        S.add(u)                                # We've visited it now
        for v in G[u]:                          # Go through all its neighbors
            relax(G, u, v, D, P)                # Relax the out-edge
            heappush(Q, (D[v], v))              # Add to queue, w/est. as pri
    return D, P                                 # Final D and P returned
 
Dijkstra’s algorithm may be similar to Prim’s (with another set of priorities for the queue), but it is also closely 
related to another old favorite: BFS. Consider the case where the edge weights are positive integers. Now, replace an 
edge that has weight w with w-1 unweighted edges, connecting a path of dummy nodes (see Figure 
9-3
). We’re ruining 
what chances we had for an efficient solution (see Exercise 9-3), but we know that BFS will find a correct solution. In 
fact, it will do so in a way very similar to Dijkstra’s algorithm: It will spend an amount of time on each (original) edge 
proportional to its weight, so it will reach each (original) node in order of distance from the start node.
a
b
3
a
b

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   159   160   161   162   163   164   165   166   ...   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