test_traversal.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import numpy as np
  2. from numpy.testing import assert_array_almost_equal
  3. from scipy.sparse.csgraph import (breadth_first_tree, depth_first_tree,
  4. csgraph_to_dense, csgraph_from_dense)
  5. def test_graph_breadth_first():
  6. csgraph = np.array([[0, 1, 2, 0, 0],
  7. [1, 0, 0, 0, 3],
  8. [2, 0, 0, 7, 0],
  9. [0, 0, 7, 0, 1],
  10. [0, 3, 0, 1, 0]])
  11. csgraph = csgraph_from_dense(csgraph, null_value=0)
  12. bfirst = np.array([[0, 1, 2, 0, 0],
  13. [0, 0, 0, 0, 3],
  14. [0, 0, 0, 7, 0],
  15. [0, 0, 0, 0, 0],
  16. [0, 0, 0, 0, 0]])
  17. for directed in [True, False]:
  18. bfirst_test = breadth_first_tree(csgraph, 0, directed)
  19. assert_array_almost_equal(csgraph_to_dense(bfirst_test),
  20. bfirst)
  21. def test_graph_depth_first():
  22. csgraph = np.array([[0, 1, 2, 0, 0],
  23. [1, 0, 0, 0, 3],
  24. [2, 0, 0, 7, 0],
  25. [0, 0, 7, 0, 1],
  26. [0, 3, 0, 1, 0]])
  27. csgraph = csgraph_from_dense(csgraph, null_value=0)
  28. dfirst = np.array([[0, 1, 0, 0, 0],
  29. [0, 0, 0, 0, 3],
  30. [0, 0, 0, 0, 0],
  31. [0, 0, 7, 0, 0],
  32. [0, 0, 0, 1, 0]])
  33. for directed in [True, False]:
  34. dfirst_test = depth_first_tree(csgraph, 0, directed)
  35. assert_array_almost_equal(csgraph_to_dense(dfirst_test),
  36. dfirst)
  37. def test_graph_breadth_first_trivial_graph():
  38. csgraph = np.array([[0]])
  39. csgraph = csgraph_from_dense(csgraph, null_value=0)
  40. bfirst = np.array([[0]])
  41. for directed in [True, False]:
  42. bfirst_test = breadth_first_tree(csgraph, 0, directed)
  43. assert_array_almost_equal(csgraph_to_dense(bfirst_test),
  44. bfirst)
  45. def test_graph_depth_first_trivial_graph():
  46. csgraph = np.array([[0]])
  47. csgraph = csgraph_from_dense(csgraph, null_value=0)
  48. bfirst = np.array([[0]])
  49. for directed in [True, False]:
  50. bfirst_test = depth_first_tree(csgraph, 0, directed)
  51. assert_array_almost_equal(csgraph_to_dense(bfirst_test),
  52. bfirst)