Graph Search
Graph Search
๊ทธ๋ํ ํ์ ์๊ณ ๋ฆฌ์ฆ
DFS(Depth First Search, ๊น์ด์ฐ์ ํ์)
print("Graph - DFS")
graph = {
'A': set(['B', 'C']),
'B': set(['A', 'D', 'E']),
'C': set(['A']),
'D': set(['B', 'F']),
'E': set(['B']),
'F': set(['D'])
}
root = 'A'
def DFS(graph, root):
visited = []
stack = [root]
while len(stack) > 0:
node = stack.pop()
if node not in visited:
visited.append(node)
stack += graph.get(node) - set(visited)
return visited
print(DFS(graph, root))
BFS(Breadth First Search, ๋๋น์ฐ์ ํ์)
Last updated
