test_connected_components.py 3.1 KB

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