Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet125/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   121   122   123   124   125   126   127   128   ...   266
Bog'liq
2 5296731884800181221

Listing 7-3.  A Naïve Implementation of Kruskal’s Algorithm
def naive_find(C, u):                           # Find component rep.
    while C[u] != u:                            # Rep. would point to itself
        u = C[u]
    return u
 
def naive_union(C, u, v):
    u = naive_find(C, u)                        # Find both reps
    v = naive_find(C, v)
    C[u] = v                                    # Make one refer to the other
 
def naive_kruskal(G):
    E = [(G[u][v],u,v) for u in G for v in G[u]]
    T = set()                                   # Empty partial solution
    C = {u:u for u in G}                        # Component reps
    for _, u, v in sorted(E):                   # Edges, sorted by weight
        if naive_find(C, u) != naive_find(C, v):
            T.add((u, v))                       # Different reps? Use it!
            naive_union(C, u, v)                # Combine components
    return T
 
The naïve Kruskal works, but it’s not all that great. (What, the name gave it away?) In the worst case, the chain 
of references we need to follow in naive_find could be linear. A rather obvious idea might be to always have the 
smaller of the two components in naive_union point to the larger, giving us some balance. Or we could think 
even more in terms of a balanced tree and give each node a rank, or height. If we always made the lowest-ranking 
representative point to the highest-ranking one, we’d get a total running time of O(m lg n) for the calls to naive_find 
and naive_union (see Exercise 7-16).
This would actually be fine because the sorting operation to begin with is 
Θ(m lg n) anyway.
12
 There is one other 
trick that is commonly used in this algorithm, though, called path compression. It entails “pulling the pointers along” 
when doing a find, making sure all the nodes we examine on our way now point directly to the representative. The 
more nodes point directly at the representative, the faster things should go in later finds, right? Sadly, the reasoning 
behind exactly how and why this helps is far too knotty for me to go into here (although I’d recommend Sect. 21.4 in 
Introduction to Algorithms by Cormen et al., if you’re interested). The end result, though, is that the worst-case total 
running time of the unions and finds is O(m
a(n)), where a(n) is almost a constant. In fact, you can assume that a(n) ≤ 4, 
for any even remotely plausible value for n. For an improved implementation of find and union, see Listing 7-4.

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   121   122   123   124   125   126   127   128   ...   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