Algorithms For Dummies



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

  Reconnecting the Dots 

     187


This example uses a class that allows it to perform priority-queue comparisons 

that determine whether the queue contains elements and when those elements 

contain a certain edge (avoiding double insertions). The priority queue has another 

useful characteristic (whose usefulness is explained when working on Dijkstra’s 

algorithm): If you insert an edge with a different weight than previously stored, 

the code updates the edge weight and rearranges the edge position in the heap.

from heapq import heapify, heappop, heappush

class priority_queue():

    def __init__(self):

        self.queue = list()

        heapify(self.queue)

        self.index = dict()

    def push(self, priority, label):

        if label in self.index:

            self.queue = [(w,l)

                for w,l in self.queue if l!=label]

            heapify(self.queue)

        heappush(self.queue, (priority, label))

        self.index[label] = priority

    def pop(self):

        if self.queue:

            return heappop(self.queue)

    def __contains__(self, label):

        return label in self.index

    def __len__(self):

        return len(self.queue)




Download 7,18 Mb.

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