Algorithms For Dummies



Download 7,18 Mb.
Pdf ko'rish
bet310/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   306   307   308   309   310   311   312   313   ...   651
Bog'liq
Algorithms

  Reconnecting the Dots 

     177


steps = bfs(graph, 'A')

print ('\nBFS:', steps)

Queue is: ['A']

Processing A

Adding B to the queue

Adding C to the queue

Queue is: ['B', 'C']

Processing B

Adding A to the queue

Adding D to the queue

Queue is: ['C', 'A', 'D']

Processing C

Adding E to the queue

Queue is: ['A', 'D', 'E']

Processing A

Queue is: ['D', 'E']

Processing D

Adding F to the queue

Queue is: ['E', 'F']

Processing E

Queue is: ['F']

Processing F

BFS: ['A>B', 'A>C', 'B>A', 'B>D', 'C>E', 'D>F']

The output shows how the algorithm searches. It’s in the order that you expect — 

one level at a time. The biggest advantage of using BFS is that it’s guaranteed 

to return the shortest path between two points as the first output when used to 

find paths.

The example code uses a simple list as a queue. As described in Chapter 4, a queue 

is a first in/first out (FIFO) data structure that works like a line at a bank, where 

the first item put into the queue is also the first item that comes out. For this pur-

pose, Python provides an even better data structure called a deque (pronounced 

deck). You create it using the 

deque

 function from the 



collections

 package. It 

performs insertions and extractions in linear time, and you can use it as both a 

queue and a stack. You can discover more about the 

deque

 function at 



https://

pymotw.com/2/collections/deque.html

.


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   306   307   308   309   310   311   312   313   ...   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