Algorithms For Dummies


Exploring the World of Graphs



Download 7,18 Mb.
Pdf ko'rish
bet329/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   325   326   327   328   329   330   331   332   ...   651
Bog'liq
Algorithms

 Exploring the World of Graphs

2. 


Start from any vertex in the graph and place it into the solution.

3. 


Determine whether there are still vertexes that aren’t part of the solution:

• 

Enumerate the edges that touch the vertexes in the solution.



• 

Insert the edge with the minimum weight into the spanning tree. (This is 

the greedy principle at work in the algorithm: Always choose the minimum 

at each step to obtain an overall minimum result.)

By translating these steps into Python code, you can test the algorithm on the 

example weighted graph using the following code:

def prim(graph, start):

    treepath = {}

    total = 0

    queue = priority_queue()

    queue.push(0 , (start, start))

    while queue:

        weight, (node_start, node_end) = queue.pop()

        if node_end not in treepath:

            treepath[node_end] = node_start

            if weight:

                print("Added edge from %s" \

                      " to %s weighting %i"

                      % (node_start, node_end, weight))

                total 

+= weight

            for next_node, weight \

            in graph[node_end].items():

                queue.push(weight , (node_end, next_node))

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

    return treepath

treepath = prim(graph, 'A')

Added edge from A to B weighting 2

Added edge from B to C weighting 2

Added edge from B to D weighting 2

Added edge from D to E weighting 1

Added edge from E to F weighting 1

Total spanning tree length: 8

The algorithm prints the processing steps, showing the edge it adds at each stage 

and the weight the edge adds to the total. The example displays the total sum of 

weights and the algorithm returns a Python dictionary containing the ending 

vertex as key and the starting vertex as value for each edge of the resulting 



CHAPTER 9


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   325   326   327   328   329   330   331   332   ...   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