fork download
  1. import numpy as np
  2. from collections import deque
  3.  
  4. def bfs_finder(d, start):
  5. sorter = np.argsort(d[:, 0])
  6. done = set()
  7. todo = deque([start])
  8. output = np.empty_like(d)
  9. pos = 0
  10. while todo:
  11. key = todo.popleft()
  12. if key in done:
  13. continue
  14. done.add(key)
  15. left = np.searchsorted(d[:, 0], key, 'left', sorter)
  16. if left >= d.shape[0] or d[sorter[left], 0] != key:
  17. continue
  18. right = np.searchsorted(d[:, 0], key, 'right', sorter)
  19. next = pos + right - left
  20. output[pos:next, :] = d[sorter[left:right], :]
  21. todo.extend(output[pos:next, 1])
  22. pos = next
  23. return output
  24.  
  25. arr = np.array([[1, 7], [2, 0], [2, 1], [2, 3], [3, 4], [3, 5], [5, 6]])
  26. print(bfs_finder(arr, 2))
Success #stdin #stdout 0.14s 24720KB
stdin
Standard input is empty
stdout
[[2 0]
 [2 1]
 [2 3]
 [1 7]
 [3 4]
 [3 5]
 [5 6]]