test_connected_components.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import assert_equal, assert_array_almost_equal
  4. from scipy.sparse import csgraph
  5. def test_weak_connections():
  6. Xde = np.array([[0, 1, 0],
  7. [0, 0, 0],
  8. [0, 0, 0]])
  9. Xsp = csgraph.csgraph_from_dense(Xde, null_value=0)
  10. for X in Xsp, Xde:
  11. n_components, labels =\
  12. csgraph.connected_components(X, directed=True,
  13. connection='weak')
  14. assert_equal(n_components, 2)
  15. assert_array_almost_equal(labels, [0, 0, 1])
  16. def test_strong_connections():
  17. X1de = np.array([[0, 1, 0],
  18. [0, 0, 0],
  19. [0, 0, 0]])
  20. X2de = X1de + X1de.T
  21. X1sp = csgraph.csgraph_from_dense(X1de, null_value=0)
  22. X2sp = csgraph.csgraph_from_dense(X2de, null_value=0)
  23. for X in X1sp, X1de:
  24. n_components, labels =\
  25. csgraph.connected_components(X, directed=True,
  26. connection='strong')
  27. assert_equal(n_components, 3)
  28. labels.sort()
  29. assert_array_almost_equal(labels, [0, 1, 2])
  30. for X in X2sp, X2de:
  31. n_components, labels =\
  32. csgraph.connected_components(X, directed=True,
  33. connection='strong')
  34. assert_equal(n_components, 2)
  35. labels.sort()
  36. assert_array_almost_equal(labels, [0, 0, 1])
  37. def test_strong_connections2():
  38. X = np.array([[0, 0, 0, 0, 0, 0],
  39. [1, 0, 1, 0, 0, 0],
  40. [0, 0, 0, 1, 0, 0],
  41. [0, 0, 1, 0, 1, 0],
  42. [0, 0, 0, 0, 0, 0],
  43. [0, 0, 0, 0, 1, 0]])
  44. n_components, labels =\
  45. csgraph.connected_components(X, directed=True,
  46. connection='strong')
  47. assert_equal(n_components, 5)
  48. labels.sort()
  49. assert_array_almost_equal(labels, [0, 1, 2, 2, 3, 4])
  50. def test_weak_connections2():
  51. X = np.array([[0, 0, 0, 0, 0, 0],
  52. [1, 0, 0, 0, 0, 0],
  53. [0, 0, 0, 1, 0, 0],
  54. [0, 0, 1, 0, 1, 0],
  55. [0, 0, 0, 0, 0, 0],
  56. [0, 0, 0, 0, 1, 0]])
  57. n_components, labels =\
  58. csgraph.connected_components(X, directed=True,
  59. connection='weak')
  60. assert_equal(n_components, 2)
  61. labels.sort()
  62. assert_array_almost_equal(labels, [0, 0, 1, 1, 1, 1])
  63. def test_ticket1876():
  64. # Regression test: this failed in the original implementation
  65. # There should be two strongly-connected components; previously gave one
  66. g = np.array([[0, 1, 1, 0],
  67. [1, 0, 0, 1],
  68. [0, 0, 0, 1],
  69. [0, 0, 1, 0]])
  70. n_components, labels = csgraph.connected_components(g, connection='strong')
  71. assert_equal(n_components, 2)
  72. assert_equal(labels[0], labels[1])
  73. assert_equal(labels[2], labels[3])
  74. def test_fully_connected_graph():
  75. # Fully connected dense matrices raised an exception.
  76. # https://github.com/scipy/scipy/issues/3818
  77. g = np.ones((4, 4))
  78. n_components, labels = csgraph.connected_components(g)
  79. assert_equal(n_components, 1)