WalkTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. use Fhaculty\Graph\Graph;
  3. use Fhaculty\Graph\Walk;
  4. use Fhaculty\Graph\Algorithm\Property\WalkProperty;
  5. use Fhaculty\Graph\Vertex;
  6. use Fhaculty\Graph\Set\Edges;
  7. class WalkTest extends TestCase
  8. {
  9. /**
  10. * @expectedException UnderflowException
  11. */
  12. public function testWalkCanNotBeEmpty()
  13. {
  14. Walk::factoryCycleFromVertices(array());
  15. }
  16. public function testWalkPath()
  17. {
  18. // 1 -- 2 -- 3
  19. $graph = new Graph();
  20. $v1 = $graph->createVertex(1);
  21. $v2 = $graph->createVertex(2);
  22. $v3 = $graph->createVertex(3);
  23. $e1 = $v1->createEdgeTo($v2);
  24. $e2 = $v2->createEdgeTo($v3);
  25. $walk = Walk::factoryFromEdges(array($e1, $e2), $v1);
  26. $this->assertEquals(3, count($walk->getVertices()));
  27. $this->assertEquals(2, count($walk->getEdges()));
  28. $this->assertSame($v1, $walk->getVertices()->getVertexFirst());
  29. $this->assertSame($v3, $walk->getVertices()->getVertexLast());
  30. $this->assertSame(array($v1, $e1, $v2, $e2, $v3), $walk->getAlternatingSequence());
  31. $this->assertTrue($walk->isValid());
  32. $graphClone = $walk->createGraph();
  33. $this->assertGraphEquals($graph, $graphClone);
  34. return $walk;
  35. }
  36. /**
  37. * @param Walk $walk
  38. * @depends testWalkPath
  39. */
  40. public function testWalkPathInvalidateByDestroyingVertex(Walk $walk)
  41. {
  42. // delete v3
  43. $walk->getVertices()->getVertexLast()->destroy();
  44. $this->assertFalse($walk->isValid());
  45. }
  46. public function testWalkWithinGraph()
  47. {
  48. // 1 -- 2 -- 3
  49. $graph = new Graph();
  50. $v1 = $graph->createVertex(1);
  51. $v2 = $graph->createVertex(2);
  52. $v3 = $graph->createVertex(3);
  53. $e1 = $v1->createEdgeTo($v2);
  54. $e2 = $v2->createEdgeTo($v3);
  55. // construct partial walk "1 -- 2"
  56. $walk = Walk::factoryFromEdges(array($e1), $v1);
  57. $this->assertEquals(2, count($walk->getVertices()));
  58. $this->assertEquals(1, count($walk->getEdges()));
  59. $this->assertSame($v1, $walk->getVertices()->getVertexFirst());
  60. $this->assertSame($v2, $walk->getVertices()->getVertexLast());
  61. $this->assertSame(array($v1, $e1, $v2), $walk->getAlternatingSequence());
  62. $this->assertTrue($walk->isValid());
  63. $graphExpected = new Graph();
  64. $graphExpected->createVertex(1)->createEdgeTo($graphExpected->createVertex(2));
  65. $this->assertGraphEquals($graphExpected, $walk->createGraph());
  66. // construct same partial walk "1 -- 2"
  67. $walkVertices = Walk::factoryFromVertices(array($v1, $v2));
  68. $this->assertEquals(2, count($walkVertices->getVertices()));
  69. $this->assertEquals(1, count($walkVertices->getEdges()));
  70. $this->assertGraphEquals($graphExpected, $walkVertices->createGraph());
  71. return $walk;
  72. }
  73. public function testWalkLoop()
  74. {
  75. // 1 -- 1
  76. $graph = new Graph();
  77. $v1 = $graph->createVertex(1);
  78. $e1 = $v1->createEdge($v1);
  79. $walk = Walk::factoryFromEdges(array($e1), $v1);
  80. $this->assertEquals(2, count($walk->getVertices()));
  81. $this->assertEquals(1, count($walk->getEdges()));
  82. $this->assertSame($v1, $walk->getVertices()->getVertexFirst());
  83. $this->assertSame($v1, $walk->getVertices()->getVertexLast());
  84. $this->assertTrue($walk->isValid());
  85. return $walk;
  86. }
  87. /**
  88. * @param Walk $walk
  89. * @depends testWalkLoop
  90. */
  91. public function testWalkInvalidByDestroyingEdge(Walk $walk)
  92. {
  93. // destroy first edge found
  94. foreach ($walk->getEdges() as $edge) {
  95. $edge->destroy();
  96. break;
  97. }
  98. $this->assertFalse($walk->isValid());
  99. }
  100. public function testWalkLoopCycle()
  101. {
  102. // 1 -- 1
  103. $graph = new Graph();
  104. $v1 = $graph->createVertex(1);
  105. $e1 = $v1->createEdge($v1);
  106. $walk = Walk::factoryCycleFromEdges(array($e1), $v1);
  107. $this->assertEquals(2, count($walk->getVertices()));
  108. $this->assertEquals(1, count($walk->getEdges()));
  109. $this->assertSame($v1, $walk->getVertices()->getVertexFirst());
  110. $this->assertSame($v1, $walk->getVertices()->getVertexLast());
  111. $this->assertTrue($walk->isValid());
  112. }
  113. /**
  114. * @expectedException InvalidArgumentException
  115. */
  116. public function testWalkCycleFromVerticesIncomplete()
  117. {
  118. // 1 -- 2 -- 1
  119. $graph = new Graph();
  120. $v1 = $graph->createVertex(1);
  121. $v2 = $graph->createVertex(2);
  122. $e1 = $v1->createEdge($v2);
  123. $e2 = $v2->createEdge($v1);
  124. // should actually be [v1, v2, v1]
  125. Walk::factoryCycleFromVertices(array($v1, $v2));
  126. }
  127. /**
  128. * @expectedException InvalidArgumentException
  129. */
  130. public function testWalkCycleInvalid()
  131. {
  132. // 1 -- 2
  133. $graph = new Graph();
  134. $v1 = $graph->createVertex(1);
  135. $v2 = $graph->createVertex(2);
  136. $e1 = $v1->createEdge($v2);
  137. Walk::factoryCycleFromEdges(array($e1), $v1);
  138. }
  139. public function testLoopCycle()
  140. {
  141. // 1 --\
  142. // ^ |
  143. // \---/
  144. $graph = new Graph();
  145. $v1 = $graph->createVertex(1);
  146. $e1 = $v1->createEdgeTo($v1);
  147. $cycle = Walk::factoryCycleFromEdges(array($e1), $v1);
  148. $this->assertGraphEquals($graph, $cycle->createGraph());
  149. $cycle = Walk::factoryCycleFromPredecessorMap(array(1 => $v1), $v1);
  150. $this->assertGraphEquals($graph, $cycle->createGraph());
  151. $cycle = Walk::factoryCycleFromVertices(array($v1, $v1));
  152. $this->assertGraphEquals($graph, $cycle->createGraph());
  153. $this->assertCount(2, $cycle->getVertices());
  154. $this->assertCount(1, $cycle->getEdges());
  155. $this->assertSame($v1, $cycle->getVertices()->getVertexFirst());
  156. $this->assertSame($v1, $cycle->getVertices()->getVertexLast());
  157. $this->assertTrue($cycle->isValid());
  158. return $v1;
  159. }
  160. /**
  161. *
  162. * @param Vertex $v1
  163. * @depends testLoopCycle
  164. * @expectedException InvalidArgumentException
  165. */
  166. public function testFactoryCycleFromVerticesIncomplete(Vertex $v1)
  167. {
  168. // should actually be [v1, v1]
  169. Walk::factoryCycleFromVertices(array($v1));
  170. }
  171. /**
  172. * @expectedException InvalidArgumentException
  173. */
  174. public function testInvalidPredecessors()
  175. {
  176. $graph = new Graph();
  177. $v1 = $graph->createVertex(1);
  178. Walk::factoryCycleFromPredecessorMap(array(), $v1);
  179. }
  180. public function testFactoryFromVertices()
  181. {
  182. // 1 -- 2
  183. // \----/
  184. $graph = new Graph();
  185. $v1 = $graph->createVertex(1);
  186. $v2 = $graph->createVertex(2);
  187. $e1 = $v1->createEdge($v2)->setWeight(10);
  188. $e2 = $v1->createEdge($v2)->setWeight(20);
  189. // any edge in walk
  190. $walk = Walk::factoryFromVertices(array($v1, $v2));
  191. // edge with weight 10
  192. $walk = Walk::factoryFromVertices(array($v1, $v2), Edges::ORDER_WEIGHT);
  193. $this->assertSame($e1, $walk->getEdges()->getEdgeFirst());
  194. // edge with weight 20
  195. $walk = Walk::factoryFromVertices(array($v1, $v2), Edges::ORDER_WEIGHT, true);
  196. $this->assertSame($e2, $walk->getEdges()->getEdgeFirst());
  197. }
  198. }