Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet59/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   55   56   57   58   59   60   61   62   ...   266
Bog'liq
2 5296731884800181221

Listing 4-8.  A Solution to the Celebrity Problem
def celeb(G):
    n = len(G)
    u, v = 0, 1                                 # The first two
    for c in range(2,n+1):                      # Others to check
        if G[u][v]: u = c                       # u knows v? Replace u
        else:       v = c                       # Otherwise, replace v
    if u == n:      c = v                       # u was replaced last; use v
    else:           c = u                       # Otherwise, u is a candidate
    for v in range(n):                          # For everyone else...
        if c == v: continue                     # Same person? Skip.
        if G[c][v]: break                       # Candidate knows other
        if not G[v][c]: break                   # Other doesn't know candidate
    else:
        return c                                # No breaks? Celebrity!
    return None                                 # Couldn't find anyone
 
To try these celebrity-finding functions, you can just whip up a random graph.
11
 Let’s switch each edge on or off 
with equal probability:
 
>>> from random import randrange
>>> n = 100
>>> G = [[randrange(2) for i in range(n)] for i in range(n)]
 
Now make sure there is a celebrity in there and run the two functions:
 
>>> c = randrange(n)
>>> for i in range(n):
...     G[i][c] = True
...     G[c][i] = False
...
>>> naive_celeb(G)
57
>>> celeb(G)
57
 
Note that though one is quadratic and one is linear, the time to build the graph (whether random or from some 
other source) is quadratic here. That could be avoided (for a sparse graph, where the average number of edges is less 
than Q(n)), with some other graph representation; see Chapter 2 for suggestions.
11
There is, in fact, a rich theory about random graphs. A web search should turn up lots of material.


Chapter 4 

 InduCtIon and reCursIon ... and reduCtIon 
82
Topological Sorting
In almost any project, the tasks to be undertaken will have dependencies that partially restrict their ordering. 
For example, unless you have a very avant-garde fashion sense, you need to put on your socks before your boots, 
but whether you put on your hat before your shorts is of less importance. Such dependencies are (as mentioned 
in Chapter 2) easily represented as a directed acyclic graph (DAG), and finding an ordering that respect the 
dependencies (so that all the edges point forward in the ordering) is called topological sorting.
Figure 
4-5
 illustrates the concept. In this case, there is a unique valid ordering, but consider what would happen if 
you removed the edge ab, for example—then a could be placed anywhere in the order, as long as it was before f.

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   266




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
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