Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet80/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   76   77   78   79   80   81   82   83   ...   266
Bog'liq
2 5296731884800181221

Listing 5-6.  A General Graph Traversal Function
def traverse(G, s, qtype=set):
    S, Q = set(), qtype()
    Q.add(s)
    while Q:
        u = Q.pop()
        if u in S: continue
        S.add(u)
        for v in G[u]:
            Q.add(v)
        yield u
 
The default queue type here is set, making it similar to the original (arbitrary) walk. You could easily define a 
stack type (with the proper add and pop methods of our general queue protocol), perhaps like this:
 
class stack(list):
    add = list.append
 
The previous depth-first test could then be repeated as follows:
 
>>> list(traverse(G, 0, stack))
[0, 5, 7, 6, 2, 3, 4, 1]
 
Of course, it’s also quite OK to implement special-purpose versions of the various traversal algorithms, even 
though they can be expressed in much the same form.


Chapter 5 

 traversal: the skeleton key of algorithmiCs
104
Depth-First Timestamps and Topological Sorting (Again)
As mentioned earlier, remembering and avoiding previously visited nodes is what keeps us from going in circles  
(or, rather, cycles), and a traversal without cycles naturally forms a tree. Such traversal trees have different names 
based on how they were constructed; for DFS, they are aptly named depth-first trees (or DFS trees). As with any 
traversal tree, the structure of a DFS tree is determined by the order in which the nodes are visited. The thing that is 
Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   76   77   78   79   80   81   82   83   ...   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