fork download
  1. def depth_limited_search(graph, start, goal, depth_limit):
  2. def dls(node, depth):
  3. if depth > depth_limit:
  4. return False
  5. if node == goal:
  6. return True
  7. for neighbor in graph.get(node, []):
  8. if dls(neighbor, depth + 1):
  9. return True
  10. return False
  11.  
  12. return dls(start, 0)
  13.  
  14. # Example Usage
  15. graph = {
  16. 'A': ['B', 'C'],
  17. 'B': ['D', 'E'],
  18. 'C': ['F'],
  19. 'D': [],
  20. 'E': [],
  21. 'F': []
  22. }
  23. print(depth_limited_search(graph, 'A', 'E', 2)) # Output: True
  24. print(depth_limited_search(graph, 'A', 'F', 1)) # Output: False
Success #stdin #stdout 0.03s 9640KB
stdin
Standard input is empty
stdout
True
False