본문 바로가기
공부/알고리즘

BFS

by JERO__ 2021. 4. 9.

from collections import deque

 

# BFS

def bfs(graphstartvisited):

    queue = deque([start])

    visited[start] = True

    while queue:

        v = queue.popleft()

        print(v, end=' ')

        for i in graph[v]:

            if visited[i] == False:

                visited[i] = True

                queue.append(i)

 

graph = [

    [],

    [238],

    [17],

    [145],

    [35],

    [34],

    [7],

    [268],

    [17],

]

visited = [False] * 9

bfs(graph, 1, visited)

댓글