Skip to content

Commit 664e85a

Browse files
author
Christian Lück
committed
Add (failing) tests to detect negative cycles
1 parent e6458fa commit 664e85a

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

tests/DetectNegativeCycleTest.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
use Fhaculty\Graph\Algorithm\DetectNegativeCycle;
5+
6+
class DetectNegativeCycleTest extends TestCase
7+
{
8+
public function testNullGraph()
9+
{
10+
$graph = new Graph();
11+
12+
$alg = new DetectNegativeCycle($graph);
13+
14+
$this->assertFalse($alg->hasCycleNegative());
15+
16+
return $alg;
17+
}
18+
19+
/**
20+
*
21+
* @param DetectNegativeCycle $alg
22+
* @depends testNullGraph
23+
* @expectedException UnderflowException
24+
*/
25+
public function testNullGraphHasNoCycle(DetectNegativeCycle $alg)
26+
{
27+
$alg->getCycleNegative();
28+
}
29+
30+
/**
31+
*
32+
* @param DetectNegativeCycle $alg
33+
* @depends testNullGraph
34+
* @expectedException UnderflowException
35+
*/
36+
public function testNullGraphHasNoCycleGraph(DetectNegativeCycle $alg)
37+
{
38+
$alg->createGraph();
39+
}
40+
41+
public function testNegativeLoop()
42+
{
43+
// 1 --[-1]--> 1
44+
$graph = new Graph();
45+
$v1 = $graph->createVertex(1);
46+
$e1 = $v1->createEdgeTo($v1)->setWeight(-1);
47+
48+
$alg = new DetectNegativeCycle($graph);
49+
50+
$this->assertTrue($alg->hasCycleNegative());
51+
52+
$cycle = $alg->getCycleNegative();
53+
54+
$this->assertCount(1, $cycle->getEdges());
55+
$this->assertCount(2, $cycle->getVertices());
56+
$this->assertEquals($e1, $cycle->getEdges()->getEdgeFirst());
57+
$this->assertEquals($v1, $cycle->getVertices()->getVertexFirst());
58+
}
59+
60+
public function testNegativeCycle()
61+
{
62+
// 1 --[-1]--> 2
63+
// ^ |
64+
// \---[-2]----/
65+
$graph = new Graph();
66+
$v1 = $graph->createVertex(1);
67+
$v2 = $graph->createVertex(2);
68+
$e1 = $v1->createEdgeTo($v2)->setWeight(-1);
69+
$e2 = $v2->createEdgeTo($v1)->setWeight(-2);
70+
71+
$alg = new DetectNegativeCycle($graph);
72+
73+
$this->assertTrue($alg->hasCycleNegative());
74+
75+
$cycle = $alg->getCycleNegative();
76+
77+
$this->assertCount(2, $cycle->getEdges());
78+
$this->assertCount(3, $cycle->getVertices());
79+
}
80+
81+
public function testNegativeUndirectedIsNegativeCycle()
82+
{
83+
// 1 --[-1]-- 2
84+
$graph = new Graph();
85+
$v1 = $graph->createVertex(1);
86+
$v2 = $graph->createVertex(2);
87+
$e1 = $v1->createEdge($v2)->setWeight(-1);
88+
89+
$alg = new DetectNegativeCycle($graph);
90+
91+
$this->assertTrue($alg->hasCycleNegative());
92+
93+
$cycle = $alg->getCycleNegative();
94+
95+
$this->assertCount(2, $cycle->getEdges());
96+
$this->assertCount(3, $cycle->getVertices());
97+
}
98+
99+
public function testNegativeCycleSubgraph()
100+
{
101+
// 1 --[1]--> 2 --[1]--> 3 --[1]--> 4
102+
// ^ |
103+
// \---[-2]---/
104+
$graph = new Graph();
105+
$v1 = $graph->createVertex(1);
106+
$v2 = $graph->createVertex(2);
107+
$v3 = $graph->createVertex(3);
108+
$v4 = $graph->createVertex(4);
109+
$e1 = $v1->createEdgeTo($v2)->setWeight(1);
110+
$e2 = $v2->createEdgeTo($v3)->setWeight(1);
111+
$e3 = $v3->createEdgeTo($v4)->setWeight(1);
112+
$e4 = $v4->createEdgeTo($v3)->setWeight(-2);
113+
114+
$alg = new DetectNegativeCycle($graph);
115+
116+
$this->assertTrue($alg->hasCycleNegative());
117+
118+
$cycle = $alg->getCycleNegative();
119+
120+
$this->assertCount(2, $cycle->getEdges());
121+
$this->assertCount(3, $cycle->getVertices());
122+
$this->assertTrue($cycle->getVertices()->hasVertexId(3));
123+
$this->assertTrue($cycle->getVertices()->hasVertexId(4));
124+
}
125+
126+
public function testNegativeComponents()
127+
{
128+
// 1 -- 2 3 --[-1]--> 4
129+
// ^ |
130+
// \---[-2]----/
131+
$graph = new Graph();
132+
$v1 = $graph->createVertex(1);
133+
$v2 = $graph->createVertex(2);
134+
$v3 = $graph->createVertex(3);
135+
$v4 = $graph->createVertex(4);
136+
$e1 = $v1->createEdge($v2);
137+
$e2 = $v3->createEdgeTo($v4)->setWeight(-1);
138+
$e3 = $v4->createEdgeTo($v3)->setWeight(-2);
139+
140+
$alg = new DetectNegativeCycle($graph);
141+
142+
$this->assertTrue($alg->hasCycleNegative());
143+
144+
$cycle = $alg->getCycleNegative();
145+
146+
$this->assertCount(2, $cycle->getEdges());
147+
$this->assertCount(3, $cycle->getVertices());
148+
$this->assertTrue($cycle->getVertices()->hasVertexId(3));
149+
$this->assertTrue($cycle->getVertices()->hasVertexId(4));
150+
}
151+
}

0 commit comments

Comments
 (0)