Algorithms For Dummies


Doing simulations using Monte Carlo



Download 7,18 Mb.
Pdf ko'rish
bet541/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   537   538   539   540   541   542   543   544   ...   651
Bog'liq
Algorithms

Doing simulations using Monte Carlo

As part of understanding the Quickselect algorithm, it pays to know how it works 

internally. By setting a counter inside the 

quickselect

 function, you can check 

performance under different conditions using a Monte Carlo simulation:

def quickselect(series, k, counter=0):

    pivot = choice(series)

    left, right = list(),list()

    for item in series:

        if item < pivot:



334

 

   


  PART 5 

 Challenging Difficult Problems

            left.append(item)

        if item > pivot:

            right.append(item)

    counter 

+= len(series)

    length_left = len(left)

    if length_left > k:

        return quickselect(left, k, counter)

    k -= length_left

    duplicates = series.count(pivot)

    if duplicates > k:

        return float(pivot), counter

    k -= duplicates

    return quickselect(right, k, counter)

The first experiment tries to determine how many operations the algorithm needs, 

on average, to find the median of an input list of 1001 numbers:

results = list()

for run in range(1000):

    n = 1001

    series = [randint(1,25) for i in range(n)]

    median,count = quickselect(series, n//2)

    assert(median==np.median(series))

    results.append(count)

print ("Mean operations: %i" % np.mean(results))

Mean operations: 2764

Displaying the results on a histogram (see Figure 17-4) reveals that the algorithm 

computes from two to four times the size of the input, with three times being the 

most likely number of processed computations.

import matplotlib.pyplot as plt

%matplotlib inline

plt.hist(results, bins='auto')

plt.xlabel("Computations")

plt.ylabel("Frequency")

plt.show()



CHAPTER 17


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   537   538   539   540   541   542   543   544   ...   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