# 1단계 - 리스트 안에 리스트 보기 # graph = [ # [], # [2, 3], # [1, 4], # [1], # [2] # ] # print(graph) # 출력 # [[], [2, 3], [1, 4], [1], [2]] # ------------------------------------------------------- # 2단계 - 특정 정점과 연결된 곳 보기 # graph = [ # [], # [2, 3], # [1, 4], # [1], # [2] # ] # 출력하려면?? # [2, 3] # ----------------------------------------------------------------- # 3단계 - 연결된 정점 하나씩 출력 # graph = [ # [], # [2, 3], # [1, 4], # [1], # [2] # ] # --------------------------------------------- # 4단계 - 방문 배열 만들기 # visited = [False, False, False, False, False] # print(visited) # 출력 # [False, False, False, False, False] # ----------------------------------- # 5단계 - 방문 표시하기 # visited = [False, False, False, False, False] # visited[1] = True # print(visited) # 출력 # [False, True, False, False, False] # 의미 # 1번 방문 완료 # --------------------------------------- # 6단계 - 큐 사용하기 # from collections import deque # q = deque() # q.append(1) # q.append(2) # q.append(3) # print(q) # 출력 # deque([1, 2, 3]) # -------------------------------------------- # 7단계 - 큐에서 꺼내기 # from collections import deque # q = deque() # q.append(1) # q.append(2) # q.append(3) # while q: # now = q.popleft() # print(now) # 출력 # 1 # 2 # 3 # -------------------------- # 8단계 - BFS 흉내내기 # from collections import deque # graph = [ # [], # [2, 3], # [1, 4], # [1], # [2] # ] # visited = [False, False, False, False, False] # q = deque() # q.append(1) # visited[1] = True # while q: # now = q.popleft() # print(now) # for next in graph[now]: # if visited[next] == False: # visited[next] = True # q.append(next) # 실행 순서 # q = [1] # 1 출력 # → 2,3 발견 # q = [2,3] # 2 출력 # → 4 발견 # q = [3,4] # 3 출력 # q = [4] # 4 출력 # 최종 출력 # 1 # 2 # 3 # 4
To embed this project on your website, copy the following code and paste it into your website's HTML: