Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet87/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   83   84   85   86   87   88   89   90   ...   266
Bog'liq
2 5296731884800181221

Listing 5-10.  Breadth-First Search
def bfs(G, s):
    P, Q = {s: None}, deque([s])                # Parents and FIFO queue
    while Q:
        u = Q.popleft()                         # Constant-time for deque
        for v in G[u]:
            if v in P: continue                 # Already has parent
            P[v] = u                            # Reached from u: u is parent
            Q.append(v)
    return P
 
As you can see, the bfs function is similar to iter_dfs, from Listing 5-5. I’ve replaced the list with a deque
and I keep track of which nodes have already received a parent in the traversal tree (that is, they’re in P), rather than 
remembering which nodes we have visited (S). To extract a path to a node u, you can simply “walk backward” in P:
 
>>> path = [u]
>>> while P[u] is not None:
...     path.append(P[u])
...     u = P[u]
...
>>> path.reverse()
 
You are, of course, free to use this kind of parent dictionary in DFS as well, or to use yield to iterate over the 
nodes in BFS, for that matter. Exercise 5-13 asks you to modify the code to find the distances (rather than the paths).
Tip
 

  one way of visualizing Bfs and Dfs is as browsing the Web. Dfs is what you get if you keep following links  
and then use the Back button once you’re done with a page. the backtracking is a bit like an “undo.” Bfs is more like 
opening every link in a new window (or tab) behind those you already have and then closing the windows as you finish 
with each page.
There is really only one situation where IDDFS would be preferable over BFS: when searching a huge tree  
(or some state space “shaped” like a tree). Because there are no cycles, we don’t need to remember which nodes 
we’ve visited, which means that IDDFS needs only store the path back to the starting node.
17
 BFS, on the other hand
must keep the entire fringe in memory (as its queue), and as long as there is some branching, this fringe will grow 
exponentially with the distance to the root. In other words, in these cases IDDFS can save a significant amount of 
memory, with little or no asymptotic slowdown. 
16
On the other hand, we’ll be jumping from node to node in a manner that could not possibly be implemented in a real-life maze.
17
To have any memory savings, you’d have to remove the S set. Because you’d be traversing a tree, that wouldn’t cause any trouble 
(that is, traversal cycles).


Chapter 5 

 traversal: the skeleton key of algorithmiCs
109

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   83   84   85   86   87   88   89   90   ...   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