test_graph.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from sympy.combinatorics import Permutation
  2. from sympy.core.symbol import symbols
  3. from sympy.matrices import Matrix
  4. from sympy.matrices.expressions import (
  5. PermutationMatrix, BlockDiagMatrix, BlockMatrix)
  6. def test_connected_components():
  7. a, b, c, d, e, f, g, h, i, j, k, l, m = symbols('a:m')
  8. M = Matrix([
  9. [a, 0, 0, 0, b, 0, 0, 0, 0, 0, c, 0, 0],
  10. [0, d, 0, 0, 0, e, 0, 0, 0, 0, 0, f, 0],
  11. [0, 0, g, 0, 0, 0, h, 0, 0, 0, 0, 0, i],
  12. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  13. [m, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  14. [0, m, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  15. [0, 0, m, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  16. [j, 0, 0, 0, k, 0, 0, 1, 0, 0, l, 0, 0],
  17. [0, j, 0, 0, 0, k, 0, 0, 1, 0, 0, l, 0],
  18. [0, 0, j, 0, 0, 0, k, 0, 0, 1, 0, 0, l],
  19. [0, 0, 0, 0, d, 0, 0, 0, 0, 0, 1, 0, 0],
  20. [0, 0, 0, 0, 0, d, 0, 0, 0, 0, 0, 1, 0],
  21. [0, 0, 0, 0, 0, 0, d, 0, 0, 0, 0, 0, 1]])
  22. cc = M.connected_components()
  23. assert cc == [[0, 4, 7, 10], [1, 5, 8, 11], [2, 6, 9, 12], [3]]
  24. P, B = M.connected_components_decomposition()
  25. p = Permutation([0, 4, 7, 10, 1, 5, 8, 11, 2, 6, 9, 12, 3])
  26. assert P == PermutationMatrix(p)
  27. B0 = Matrix([
  28. [a, b, 0, c],
  29. [m, 1, 0, 0],
  30. [j, k, 1, l],
  31. [0, d, 0, 1]])
  32. B1 = Matrix([
  33. [d, e, 0, f],
  34. [m, 1, 0, 0],
  35. [j, k, 1, l],
  36. [0, d, 0, 1]])
  37. B2 = Matrix([
  38. [g, h, 0, i],
  39. [m, 1, 0, 0],
  40. [j, k, 1, l],
  41. [0, d, 0, 1]])
  42. B3 = Matrix([[1]])
  43. assert B == BlockDiagMatrix(B0, B1, B2, B3)
  44. def test_strongly_connected_components():
  45. M = Matrix([
  46. [11, 14, 10, 0, 15, 0],
  47. [0, 44, 0, 0, 45, 0],
  48. [1, 4, 0, 0, 5, 0],
  49. [0, 0, 0, 22, 0, 23],
  50. [0, 54, 0, 0, 55, 0],
  51. [0, 0, 0, 32, 0, 33]])
  52. scc = M.strongly_connected_components()
  53. assert scc == [[1, 4], [0, 2], [3, 5]]
  54. P, B = M.strongly_connected_components_decomposition()
  55. p = Permutation([1, 4, 0, 2, 3, 5])
  56. assert P == PermutationMatrix(p)
  57. assert B == BlockMatrix([
  58. [
  59. Matrix([[44, 45], [54, 55]]),
  60. Matrix.zeros(2, 2),
  61. Matrix.zeros(2, 2)
  62. ],
  63. [
  64. Matrix([[14, 15], [4, 5]]),
  65. Matrix([[11, 10], [1, 0]]),
  66. Matrix.zeros(2, 2)
  67. ],
  68. [
  69. Matrix.zeros(2, 2),
  70. Matrix.zeros(2, 2),
  71. Matrix([[22, 23], [32, 33]])
  72. ]
  73. ])
  74. P = P.as_explicit()
  75. B = B.as_explicit()
  76. assert P.T * B * P == M
  77. P, B = M.strongly_connected_components_decomposition(lower=False)
  78. p = Permutation([3, 5, 0, 2, 1, 4])
  79. assert P == PermutationMatrix(p)
  80. assert B == BlockMatrix([
  81. [
  82. Matrix([[22, 23], [32, 33]]),
  83. Matrix.zeros(2, 2),
  84. Matrix.zeros(2, 2)
  85. ],
  86. [
  87. Matrix.zeros(2, 2),
  88. Matrix([[11, 10], [1, 0]]),
  89. Matrix([[14, 15], [4, 5]])
  90. ],
  91. [
  92. Matrix.zeros(2, 2),
  93. Matrix.zeros(2, 2),
  94. Matrix([[44, 45], [54, 55]])
  95. ]
  96. ])
  97. P = P.as_explicit()
  98. B = B.as_explicit()
  99. assert P.T * B * P == M