|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -use Fhaculty\Graph\Exception\UnexpectedValueException; |
4 |
| -use Fhaculty\Graph\Edge\Base as Edge; |
5 |
| -use Graphp\Algorithms\ResidualGraph; |
6 | 3 | use Fhaculty\Graph\Graph;
|
| 4 | +use Graphp\Algorithms\ResidualGraph; |
7 | 5 |
|
8 | 6 | class ResidualGraphTest extends TestCase
|
9 | 7 | {
|
@@ -87,6 +85,81 @@ public function testEdgePartial()
|
87 | 85 | $this->assertGraphEquals($expected, $residual);
|
88 | 86 | }
|
89 | 87 |
|
| 88 | + public function testResidualGraphCanOptionallyKeepNullCapacityForEdgeWithZeroFlow() |
| 89 | + { |
| 90 | + // 1 -[0/2]-> 2 |
| 91 | + $graph = new Graph(); |
| 92 | + $v1 = $graph->createVertex(1); |
| 93 | + $v2 = $graph->createVertex(2); |
| 94 | + $v1->createEdgeTo($v2)->setFlow(0)->setCapacity(2); |
| 95 | + |
| 96 | + // 1 -[0/2]-> 2 |
| 97 | + // ^ | |
| 98 | + // \--[0/0]---/ |
| 99 | + $expected = new Graph(); |
| 100 | + $v1 = $expected->createVertex(1); |
| 101 | + $v2 = $expected->createVertex(2); |
| 102 | + $v1->createEdgeTo($v2)->setFlow(0)->setCapacity(2); |
| 103 | + $v2->createEdgeTo($v1)->setFlow(0)->setCapacity(0); |
| 104 | + |
| 105 | + $alg = new ResidualGraph($graph); |
| 106 | + $alg->setKeepNullCapacity(true); |
| 107 | + $residual = $alg->createGraph(); |
| 108 | + |
| 109 | + $this->assertGraphEquals($expected, $residual); |
| 110 | + } |
| 111 | + |
| 112 | + public function testResidualGraphCanOptionallyKeepNullCapacityForEdgeWithZeroCapacityRemaining() |
| 113 | + { |
| 114 | + // 1 -[2/2]-> 2 |
| 115 | + $graph = new Graph(); |
| 116 | + $v1 = $graph->createVertex(1); |
| 117 | + $v2 = $graph->createVertex(2); |
| 118 | + $v1->createEdgeTo($v2)->setFlow(2)->setCapacity(2); |
| 119 | + |
| 120 | + // 1 -[0/0]-> 2 |
| 121 | + // ^ | |
| 122 | + // \--[0/2]---/ |
| 123 | + $expected = new Graph(); |
| 124 | + $v1 = $expected->createVertex(1); |
| 125 | + $v2 = $expected->createVertex(2); |
| 126 | + $v1->createEdgeTo($v2)->setFlow(0)->setCapacity(0); |
| 127 | + $v2->createEdgeTo($v1)->setFlow(0)->setCapacity(2); |
| 128 | + |
| 129 | + $alg = new ResidualGraph($graph); |
| 130 | + $alg->setKeepNullCapacity(true); |
| 131 | + $residual = $alg->createGraph(); |
| 132 | + |
| 133 | + $this->assertGraphEquals($expected, $residual); |
| 134 | + } |
| 135 | + |
| 136 | + public function testParallelEdgesCanBeMerged() |
| 137 | + { |
| 138 | + // 1 -[1/2]-> 2 |
| 139 | + // | ^ |
| 140 | + // \--[2/3]---/ |
| 141 | + $graph = new Graph(); |
| 142 | + $v1 = $graph->createVertex(1); |
| 143 | + $v2 = $graph->createVertex(2); |
| 144 | + $v1->createEdgeTo($v2)->setFlow(1)->setCapacity(2); |
| 145 | + $v1->createEdgeTo($v2)->setFlow(2)->setCapacity(3); |
| 146 | + |
| 147 | + // 1 -[0/2]-> 2 |
| 148 | + // ^ | |
| 149 | + // \--[0/3]---/ |
| 150 | + $expected = new Graph(); |
| 151 | + $v1 = $expected->createVertex(1); |
| 152 | + $v2 = $expected->createVertex(2); |
| 153 | + $v1->createEdgeTo($v2)->setFlow(0)->setCapacity(2); |
| 154 | + $v2->createEdgeTo($v1)->setFlow(0)->setCapacity(3); |
| 155 | + |
| 156 | + $alg = new ResidualGraph($graph); |
| 157 | + $alg->setMergeParallelEdges(true); |
| 158 | + $residual = $alg->createGraph(); |
| 159 | + |
| 160 | + $this->assertGraphEquals($expected, $residual); |
| 161 | + } |
| 162 | + |
90 | 163 | /**
|
91 | 164 | * expect exception for undirected edges
|
92 | 165 | * @expectedException UnexpectedValueException
|
|
0 commit comments