Algorithms For Dummies


PART 3   Exploring the World of Graphs



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

 

   


  PART 3 

 Exploring the World of Graphs

Applying breadth-first search

A breadth-first search (BFS) begins at the graph root and explores every node that 

attaches to the root. It then searches the next level — exploring each level in turn 

until it reaches the end. Consequently, in the example graph, the search explores 

from A to B and C before it moves on to explore D. BFS explores the graph in a 

systematic way, exploring vertexes all around the starting vertex in a circular 

fashion. It begins by visiting all the vertexes a single step from the starting vertex

it then moves two steps out, then three steps out, and so on. The following code 

demonstrates how to perform a breadth-first search.

def bfs(graph, start):

    queue = [start]

    queued = list()

    path = list()

    while queue:

        print ('Queue is: %s' % queue)

        vertex = queue.pop(0)

        print ('Processing %s' % vertex)

        for candidate in graph[vertex]:

            if candidate not in queued:

                queued.append(candidate)

                queue.append(candidate)

                path.append(vertex

+'>'+candidate)

                print ('Adding %s to the queue'

                       % candidate)

    return path



FIGURE 9-1: 

Representing the 

example graph by 

NetworkX.




CHAPTER 9


Download 7,18 Mb.

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