Algorithms For Dummies



Download 7,18 Mb.
Pdf ko'rish
bet340/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   336   337   338   339   340   341   342   343   ...   651
Bog'liq
Algorithms

  Reconnecting the Dots 

     195


When the distance recorded in the priority queue isn’t infinite, but it’s more than 

the distance that the algorithm has just calculated, it means that the algorithm 

has found a shortcut, a shorter way to reach that vertex from the starting point, 

and it stores the information in the priority queue. Of course, if the distance 

recorded in the priority queue is shorter than the one just evaluated by the algo-

rithm, the algorithm discards the information because the new route is longer. 

After updating all the distances to the neighboring vertexes, the algorithm deter-

mines whether it has reached the end vertex. If not, it picks the shortest edge 

present in the priority queue, visits it, and starts evaluating the distance to the 

new neighboring vertexes.

As the narrative of the algorithm explained, Dijikstra’s algorithm keeps a precise 

accounting of the cost to reach every vertex that it encounters, and it updates its 

information only when it finds a shorter way. The running complexity of the algo-

rithm in Big-O notation is O(E*log(V)), where E is the number of edges and V the 

number of vertexes in the graph. The following code shows how to implement 

Dijikstra’s algorithm using Python:

def dijkstra(graph, start, end):

    inf = float('inf')

    known = set()

    priority = priority_queue()

    path = {start: start}

    for vertex in graph:

        if vertex == start:

            priority.push(0, vertex)

        else:

            priority.push(inf, vertex)

    last = start

    while last != end:

        (weight, actual_node) = priority.pop()

        if actual_node not in known:

            for next_node in graph[actual_node]:

                upto_actual = priority.index[actual_node]

                upto_next = priority.index[next_node]

                to_next = upto_actual 

+ \

                graph[actual_node][next_node]



                if to_next < upto_next:

                    priority.push(to_next, next_node)

                    print("Found shortcut from %s to %s"

                          % (actual_node, next_node))

                    print ("\tTotal length up so far: %i"



196


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   336   337   338   339   340   341   342   343   ...   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