Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. def backtrace(parent, start, end):
  2. path = [end]
  3. while path[-1] != start:
  4. path.append(parent[path[-1]])
  5. path.reverse()
  6. return path
  7.  
  8. def basicSearch(tree, start, goal):
  9. parent = {}
  10. path = []
  11. visited = [False] * (len(tree))
  12. squeue = queue.Queue()
  13.  
  14. squeue.put(start)
  15. visited[start] = True
  16. found=False
  17. while squeue:
  18. s = squeue.get(0)
  19. path.append(s)
  20. if(s == goal):
  21. found=True
  22. return backtrace(parent, start, goal)
  23.  
  24. # Get all adjacent vertices of the
  25. # dequeued vertex s. If a adjacent
  26. # has not been visited, then mark it
  27. # visited and enqueue it
  28.  
  29. for l in tree[s]:
  30. if visited[l] == False:
  31. parent[l] = s
  32. squeue.put(l)
  33. visited[l] = True
  34.  
  35.  
  36. return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement