Skip to content

Commit

Permalink
39
Browse files Browse the repository at this point in the history
  • Loading branch information
greenlemonT committed Aug 2, 2024
1 parent 95e8a25 commit 9ff27c8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions MSKIM/31to40/39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections import deque

def solution(graph, start):
adj_list = {} #
for u, v in graph:
if u in adj_list:
adj_list[u].append(v)
else:
adj_list[u] = [v]


def bfs(start):
visited = set()
queue = deque([start])
visited.add(start)
result = [start]

while queue:
node = queue.popleft()
if node in adj_list:
for neighbor in adj_list[node]:
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
result.append(neighbor)

return result

return bfs(start)


graph = [['A', 'B'], ['A', 'C'], ['B', 'D'], ['B', 'E'], ['C', 'F'], ['E', 'F']]
start_node = 'A'

print(solution(graph, start_node))

0 comments on commit 9ff27c8

Please sign in to comment.