Algorithms For Dummies


PART 5   Challenging Difficult Problems



Download 7,18 Mb.
Pdf ko'rish
bet517/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   513   514   515   516   517   518   519   520   ...   651
Bog'liq
Algorithms

 

   


  PART 5 

 Challenging Difficult Problems

        for idx,dist in enumerate(D[0][1:])}

cities = D.shape[0]

for subset_size in range(2, cities):

    # Here we define the size of the subset of cities

    new_memo = dict()

    for subset in [frozenset(comb) | {0} for comb in

                   combinations(range(1, cities),

                                subset_size)]:

        # We enumerate the subsets having a certain subset

        # size

        for ending in subset - {0}:

            # We consider every ending point in the subset

            all_paths = list()

            for k in subset:

                # We check the shortest path for every

                # element in the subset

                if k != 0 and k!=ending:

                    length = memo[(subset-{ending},k)][0

                                        ] 

+ D[k][ending]

                    index  = memo[(subset-{ending},k)][1

                                            ] 

+ [ending]

                    all_paths.append((length, index))

            new_memo[(subset, ending)] = min(all_paths)

    # In order to save memory, we just record the previous

    # subsets since we won't use shorter ones anymore

    memo = new_memo

# Now we close the cycle and get back to the start of the

# tour, city zero

tours = list()

for distance, path in memo.values():

    distance 

+= D[path[-1],0]

    tours.append((distance, path

+[0]))

# We can now declare the shortest tour



distance, path = min(tours)

print ('Shortest dynamic programming tour is: %s, %i kms'

       % (path, distance))

Shortest dynamic programming tour is:

 [0, 1, 3, 2, 4, 0], 80 kms



CHAPTER 16


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   513   514   515   516   517   518   519   520   ...   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