Algorithms For Dummies


a.  Choose the shortest edge from the heap. b



Download 7,18 Mb.
Pdf ko'rish
bet332/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   328   329   330   331   332   333   334   335   ...   651
Bog'liq
Algorithms

a.  Choose the shortest edge from the heap.

b.  Determine whether the two vertexes connected by the edge appear in 

different trees from among the set of connected trees.




190

 

   


  PART 3 

 Exploring the World of Graphs

c.  When the trees differ, connect the trees using the edge (defining an 

aggregation).



d.  When the vertexes appear in the same tree, discard the edge.

e.  Repeat steps a through d for the remaining edges on the heap.

The following example demonstrates how to turn these steps into Python code:

def kruskal(graph):

    priority = priority_queue()

    print ("Pushing all edges into the priority queue")

    treepath = list()

    connected = dict()

    for node in graph:

        connected[node] = [node]

        for dest, weight in graph[node].items():

            priority.push(weight, (node,dest))

    print ("Totally %i edges" % len(priority))

    print ("Connected components: %s"

           % connected.values())

    total = 0

    while len(treepath) < (len(graph)-1):

        (weight, (start, end)) = priority.pop()

        if end not in connected[start]:

            treepath.append((start, end))

            print ("Summing %s and %s components:"

                   % (connected[start],connected[end]))

            print ("\tadded edge from %s " \

                   "to %s weighting %i"

                   % (start, end, weight))

            total 

+= weight

            connected[start] 

+= connected[end][:]

            for element in connected[end]:

                connected[element]= connected[start]

    print ("Total spanning tree length: %i" % total)

    return sorted(treepath, key=lambda x:x[0])

print ('\nMinimum spanning tree: %s' % kruskal(graph))

Pushing all edges into the priority queue

Totally 9 edges

Connected components: dict_values([['A'], ['E'], ['F'],

                                  ['B'], ['D'], ['C']])



CHAPTER 9


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   328   329   330   331   332   333   334   335   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
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