test_traversal.py 2.3 KB

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