Depth-First Search
Depth-first search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking.
python
# Depth-first search function
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
for neighbor in graph[start]:
if neighbor not in visited:
dfs(graph, neighbor, visited)
return visited
No comments:
Post a Comment