BFS and DFS
BFS and DFS
• # Example usage:
• graph = {
• 'A': {'B', 'C'},
• 'B': {'A', 'D', 'E'},
• 'C': {'A', 'F', 'G'},
• 'D': {'B'},
• 'E': {'B', 'G'},
• 'F': {'C'},
• 'G': {'C', 'E'}
• }
• bfs(graph, 'A')
If the goal is provided
• from collections import deque
• # Example usage:
• graph = {
• 'A': {'B', 'C'},
• 'B': {'A', 'D', 'E'},
• 'C': {'A', 'F', 'G'},
• 'D': {'B'},
• 'E': {'B', 'G'},
• 'F': {'C'},
• 'G': {'C', 'E'}
• }