You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defdfs(node): # node = 현재 방문할 노드visited[node] =Trueprint(v, end=' ')
# 현재 노드와 연결된 다른 노드를 재귀적으로 방문foriingraph[node]: # graph 리스트는 인접 리스트 형식으로 표현된 그래프ifnotvisited[i]: # i 노드를 방문하지 않았다면dfs(i) # 방문
깊이 우선 그래프 탐색 알고리즘
스택 자료구조 또는 재귀 함수를 이용함
BFS (Breadth-First Search)
fromcollectionsimportdequedefbfs(node): # start = 첫 시작점이 되는 노드q=deque()
q.append(node) # 큐에 시작 노드를 넣어줌whileq: # q 에 남은 노드가 없을 때까지 반복node=q.popleft() # 지금 방문한 노드print(node, end=' ')
foriingraph[x]: # 인접 노드들에 대하여ifnotvisited[i]: # 방문하지 않은 노드가 있다면q.append(i) # 방문하기 위해 큐에 집어넣고visited[i] =True# 해당 노드를 방문했다고 표시