Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet168/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   164   165   166   167   168   169   170   171   ...   266
Bog'liq
2 5296731884800181221

Listing 9-7.  The Floyd-Warshall Algorithm
def floyd_warshall(G):
    D, P = deepcopy(G), {}
    for u in G:
        for v in G:
            if u == v or G[u][v] == inf:
                P[u,v] = None
            else:
                P[u,v] = u
    for k in G:
        for u in G:
            for v in G:
                shortcut = D[u][k] + D[k][v]
                if shortcut < D[u][v]:
                    D[u][v] = shortcut
                    P[u,v] = P[k,v]
    return D, P
 
Note that it’s important to use shortcut < D[u][v] here, and not shortcut <= D[u][v]. Although the latter would 
still give the correct distances, you could get cases where the last step was D[v][v], which would lead to P[u,v] = None.
The Floyd-Warshall algorithm can quite easily be modified to calculate the transitive closure of a graph  
(Warshall’s algorithm). See Exercise 9-9.


Chapter 9 

 From a to B with edsger and Friends
201
Meeting in the Middle
The subproblems solutions of Dijkstra’s algorithm—and of BFS, its unweighted special case—spread outward on a graph 
like ripples on a pond. If all you want is getting from A to B, or, using the customary node names, from s to t, this means 
that the “ripple” has to pass many nodes that you’re not really interested, as in the left image in Figure 
9-4
. If, on the other 
hand, you start traversing from both your starting point and your end point (assuming you can traverse edges in reverse), 
the two ripples can, in some cases, meet up in the middle, saving you a lot of work, as illustrated in the right image.

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   164   165   166   167   168   169   170   171   ...   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