|
| 1 | +<?php |
| 2 | + |
| 3 | +use Fhaculty\Graph\Algorithm\MinimumCostFlow\Base; |
| 4 | +use Fhaculty\Graph\Graph; |
| 5 | + |
| 6 | +abstract class BaseMcfTest extends TestCase |
| 7 | +{ |
| 8 | + /** |
| 9 | + * |
| 10 | + * @param Graph $graph |
| 11 | + * @return Base |
| 12 | + */ |
| 13 | + abstract protected function createAlgorithm(Graph $graph); |
| 14 | + |
| 15 | + public function testNull() |
| 16 | + { |
| 17 | + $graph = new Graph(); |
| 18 | + |
| 19 | + $alg = $this->createAlgorithm($graph); |
| 20 | + $this->assertEquals(0, $alg->getWeightFlow()); |
| 21 | + } |
| 22 | + |
| 23 | + public function testSingleIntermediary() |
| 24 | + { |
| 25 | + $graph = new Graph(); |
| 26 | + $v1 = $graph->createVertex(1); |
| 27 | + |
| 28 | + $alg = $this->createAlgorithm($graph); |
| 29 | + $this->assertEquals(0, $alg->getWeightFlow()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSimpleEdge() |
| 33 | + { |
| 34 | + // 1(+2) -[0/2/2]-> 2(-2) |
| 35 | + $graph = new Graph(); |
| 36 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 37 | + $v2 = $graph->createVertex(2)->setBalance(-2); |
| 38 | + $v1->createEdgeTo($v2)->setWeight(2)->setCapacity(2); |
| 39 | + |
| 40 | + $alg = $this->createAlgorithm($graph); |
| 41 | + $this->assertEquals(4, $alg->getWeightFlow()); // 2x2 |
| 42 | + } |
| 43 | + |
| 44 | + public function testMultipleSinks() |
| 45 | + { |
| 46 | + // 1(+2) -[0/2/2]-> 2(-1) |
| 47 | + // -[0/4/-5]-> 3(-1) |
| 48 | + $graph = new Graph(); |
| 49 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 50 | + $v2 = $graph->createVertex(2)->setBalance(-1); |
| 51 | + $v3 = $graph->createVertex(3)->setBalance(-1); |
| 52 | + $v1->createEdgeTo($v2)->setWeight(2)->setCapacity(2); |
| 53 | + $v1->createEdgeTo($v3)->setWeight(-5)->setCapacity(4); |
| 54 | + |
| 55 | + $alg = $this->createAlgorithm($graph); |
| 56 | + $this->assertEquals(-3, $alg->getWeightFlow()); // 1*2 + 1*-5 |
| 57 | + } |
| 58 | + |
| 59 | + public function testIntermediaryVertices() |
| 60 | + { |
| 61 | + // 1(+2) -[0/1/4]-> 2 -[0/6/-2]-> 4(-2) |
| 62 | + // -[0/4/5]-> 3 -[0/6/8]-> |
| 63 | + $graph = new Graph(); |
| 64 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 65 | + $v2 = $graph->createVertex(2); |
| 66 | + $v3 = $graph->createVertex(3); |
| 67 | + $v4 = $graph->createVertex(4)->setBalance(-2); |
| 68 | + $v1->createEdgeTo($v2)->setWeight(4)->setCapacity(1); |
| 69 | + $v2->createEdgeTo($v4)->setWeight(-2)->setCapacity(6); |
| 70 | + $v1->createEdgeTo($v3)->setWeight(5)->setCapacity(4); |
| 71 | + $v3->createEdgeTo($v4)->setWeight(8)->setCapacity(6); |
| 72 | + |
| 73 | + $alg = $this->createAlgorithm($graph); |
| 74 | + $this->assertEquals(15, $alg->getWeightFlow()); // 1*4 + 1*-2 + 1*5 + 1*8 |
| 75 | + } |
| 76 | + |
| 77 | + public function testEdgeCapacities() |
| 78 | + { |
| 79 | + // 1(+2) -[0/3/4]-> 2 -[0/4/5]-> 3 ->[0/6/-2]-> 3(-2) |
| 80 | + $graph = new Graph(); |
| 81 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 82 | + $v2 = $graph->createVertex(2); |
| 83 | + $v3 = $graph->createVertex(3); |
| 84 | + $v4 = $graph->createVertex(4)->setBalance(-2); |
| 85 | + $v1->createEdgeTo($v2)->setWeight(4)->setCapacity(3); |
| 86 | + $v2->createEdgeTo($v3)->setWeight(5)->setCapacity(4); |
| 87 | + $v3->createEdgeTo($v4)->setWeight(-2)->setCapacity(6); |
| 88 | + |
| 89 | + $alg = $this->createAlgorithm($graph); |
| 90 | + $this->assertEquals(14, $alg->getWeightFlow()); // 2*4 + 2*5 + 2*-2 |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @expectedException UnexpectedValueException |
| 95 | + */ |
| 96 | + public function testEdgeCapacityInsufficientFails() |
| 97 | + { |
| 98 | + // 1(+2) -[0/1]-> 2(-2) |
| 99 | + $graph = new Graph(); |
| 100 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 101 | + $v2 = $graph->createVertex(2)->setBalance(-2); |
| 102 | + $v1->createEdgeTo($v2)->setCapacity(1); |
| 103 | + |
| 104 | + $alg = $this->createAlgorithm($graph); |
| 105 | + $alg->getWeightFlow(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @expectedException UnexpectedValueException |
| 110 | + */ |
| 111 | + public function testEdgeCapacityUnsetFails() |
| 112 | + { |
| 113 | + // 1(+2) -> 2(-2) |
| 114 | + $graph = new Graph(); |
| 115 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 116 | + $v2 = $graph->createVertex(2)->setBalance(-2); |
| 117 | + $v1->createEdgeTo($v2); |
| 118 | + |
| 119 | + $alg = $this->createAlgorithm($graph); |
| 120 | + $alg->getWeightFlow(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @expectedException UnexpectedValueException |
| 125 | + */ |
| 126 | + public function testIsolatedVerticesFail() |
| 127 | + { |
| 128 | + // 1(+2), 2(-2) |
| 129 | + $graph = new Graph(); |
| 130 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 131 | + $v2 = $graph->createVertex(2)->setBalance(-2); |
| 132 | + |
| 133 | + $alg = $this->createAlgorithm($graph); |
| 134 | + $alg->getWeightFlow(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @expectedException UnexpectedValueException |
| 139 | + */ |
| 140 | + public function testUnbalancedFails() |
| 141 | + { |
| 142 | + // 1(+2) -> 2(-3) |
| 143 | + $graph = new Graph(); |
| 144 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 145 | + $v2 = $graph->createVertex(2)->setBalance(-3); |
| 146 | + $v1->createEdgeTo($v2)->setCapacity(3); |
| 147 | + |
| 148 | + $alg = $this->createAlgorithm($graph); |
| 149 | + $alg->getWeightFlow(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @expectedException UnexpectedValueException |
| 154 | + */ |
| 155 | + public function testUndirectedFails() |
| 156 | + { |
| 157 | + // 1(+2) -- 2(-2) |
| 158 | + $graph = new Graph(); |
| 159 | + $v1 = $graph->createVertex(1)->setBalance(2); |
| 160 | + $v2 = $graph->createVertex(2)->setBalance(-2); |
| 161 | + $v1->createEdge($v2)->setCapacity(2); |
| 162 | + |
| 163 | + $alg = $this->createAlgorithm($graph); |
| 164 | + $alg->getWeightFlow(); |
| 165 | + } |
| 166 | +} |
0 commit comments