Skip to content

Instantly share code, notes, and snippets.

@ssabii
Last active September 21, 2020 10:46
Show Gist options
  • Save ssabii/702456b11b40b5ebcdb321e891fb6d09 to your computer and use it in GitHub Desktop.
Save ssabii/702456b11b40b5ebcdb321e891fb6d09 to your computer and use it in GitHub Desktop.
그래프 DFS
def dfs(vertex, graph):
visited.add(vertex)
print(vertex + 1, end = '') # 출력용
for i, v in enumerate(graph[vertex]):
if v == 1 and i not in visited:
print(end=' -> ') # 출력용
dfs(i, graph)
global visited # 방문 표시
if __name__ == "__main__":
visited = set()
graph = [
[0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0]
]
dfs(0, graph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment