Skip to content

Commit

Permalink
Update dfs_stack.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dremdeveloper authored Oct 2, 2023
1 parent 6055832 commit 4a16441
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Algorithm_with_DataStructure/dfs_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def dfs(graph, start_node):

while stack:
node = stack.pop()
if not visited[node] :
visited[node] = True
print(node)
stack.extend(reversed(graph[node])) # reversed를 사용하여 깊이 우선 탐색을 유지


visited[node] = True;
print(node)

for adj_node in graph[node]: # 인접한 노드 방문
if not visited[adj_node]:
stack.append(adj_node)


# 그래프를 인접 리스트로 표현
graph = {
1: [2, 3],
Expand All @@ -49,4 +51,4 @@ def dfs(graph, start_node):
}

# DFS 알고리즘 실행
dfs(graph, 1) # 1 2 4 5 3
dfs(graph, 1) # 1 3 2 5 4

0 comments on commit 4a16441

Please sign in to comment.