Last active
September 21, 2020 10:46
-
-
Save ssabii/702456b11b40b5ebcdb321e891fb6d09 to your computer and use it in GitHub Desktop.
그래프 DFS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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