Skip to content

Commit ca08410

Browse files
committed
Merge branch 'minor'
2 parents 02afcfb + f34b5bf commit ca08410

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/MaximumMatching/Flow.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public function getEdges()
5858
} elseif ($group === $groupB) {
5959
$vertex->createEdgeTo($superSink)->setCapacity(1)->setFlow(0);
6060
} else {
61+
// @codeCoverageIgnoreStart
6162
throw new LogicException('Should not happen. Unknown set: ' + $belongingSet);
63+
// @codeCoverageIgnoreEnd
6264
}
6365
}
6466

tests/MaxFlow/EdmondsKarpTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@ class EdmondsKarpTest extends PHPUnit_Framework_TestCase
1010
{
1111
public function testEdgeDirected()
1212
{
13+
// 0 -[0/10]-> 1
1314
$graph = new Graph();
1415
$v0 = $graph->createVertex(0);
1516
$v1 = $graph->createVertex(1);
1617

1718
$v0->createEdgeTo($v1)->setCapacity(10);
1819

20+
// 0 -[10/10]-> 1
1921
$alg = new AlgorithmMaxFlowEdmondsKarp($v0, $v1);
2022

2123
$this->assertEquals(10, $alg->getFlowMax());
2224
}
2325

2426
public function testEdgesMultiplePaths()
2527
{
28+
// 0 -[0/5]---------> 1
29+
// | ^
30+
// | |
31+
// \-[0/7]-> 2 -[0/9]-/
2632
$graph = new Graph();
2733
$v0 = $graph->createVertex(0);
2834
$v1 = $graph->createVertex(1);
@@ -32,11 +38,65 @@ public function testEdgesMultiplePaths()
3238
$v0->createEdgeTo($v2)->setCapacity(7);
3339
$v2->createEdgeTo($v1)->setCapacity(9);
3440

41+
// 0 -[5/5]---------> 1
42+
// | ^
43+
// | |
44+
// \-[7/7]-> 2 -[7/9]-/
3545
$alg = new AlgorithmMaxFlowEdmondsKarp($v0, $v1);
3646

3747
$this->assertEquals(12, $alg->getFlowMax());
3848
}
3949

50+
public function testEdgesMultiplePathsTwo()
51+
{
52+
// 0 -[0/5]---------> 1-[0/10]-> 3
53+
// | ^ |
54+
// | | |
55+
// \-[0/7]-> 2 -[0/9]-/ |
56+
// ^ |
57+
// \---[0/2]-----------/
58+
$graph = new Graph();
59+
$v0 = $graph->createVertex(0);
60+
$v1 = $graph->createVertex(1);
61+
$v2 = $graph->createVertex(2);
62+
$v3 = $graph->createVertex(3);
63+
$v4 = $graph->createVertex(4);
64+
$v5 = $graph->createVertex(5);
65+
66+
$v0->createEdgeTo($v1)->setCapacity(5);
67+
$v0->createEdgeTo($v2)->setCapacity(7);
68+
$v2->createEdgeTo($v1)->setCapacity(9);
69+
$v1->createEdgeTo($v3)->setCapacity(10);
70+
$v3->createEdgeTo($v2)->setCapacity(2);
71+
72+
$alg = new AlgorithmMaxFlowEdmondsKarp($v0, $v3);
73+
74+
$this->assertEquals(10, $alg->getFlowMax());
75+
76+
$alg = new AlgorithmMaxFlowEdmondsKarp($v0, $v2);
77+
78+
$this->assertEquals(9, $alg->getFlowMax());
79+
}
80+
81+
public function testEdgesMultiplePathsTree()
82+
{
83+
$graph = new Graph();
84+
$v0 = $graph->createVertex(0);
85+
$v1 = $graph->createVertex(1);
86+
$v2 = $graph->createVertex(2);
87+
$v3 = $graph->createVertex(3);
88+
89+
$v0->createEdgeTo($v1)->setCapacity(4);
90+
$v0->createEdgeTo($v2)->setCapacity(2);
91+
$v1->createEdgeTo($v2)->setCapacity(3);
92+
$v1->createEdgeTo($v3)->setCapacity(1);
93+
$v2->createEdgeTo($v3)->setCapacity(6);
94+
95+
$alg = new AlgorithmMaxFlowEdmondsKarp($v0, $v3);
96+
97+
$this->assertEquals(6, $alg->getFlowMax());
98+
}
99+
40100
// public function testEdgesParallel(){
41101
// $graph = new Graph();
42102
// $v0 = $graph->createVertex(0);
@@ -55,12 +115,14 @@ public function testEdgesMultiplePaths()
55115
*/
56116
public function testEdgesUndirected()
57117
{
118+
// 0 -[0/7]- 1
58119
$graph = new Graph();
59120
$v0 = $graph->createVertex(0);
60121
$v1 = $graph->createVertex(1);
61122

62123
$v1->createEdge($v0)->setCapacity(7);
63124

125+
// 0 -[7/7]- 1
64126
$alg = new AlgorithmMaxFlowEdmondsKarp($v0, $v1);
65127

66128
$this->assertEquals(7, $alg->getFlowMax());
@@ -78,4 +140,30 @@ public function testEdgesUndirected()
78140
// $this->assertEquals(0.735802, $alg->getFlowMax());
79141
// }
80142

143+
144+
/**
145+
* @expectedException InvalidArgumentException
146+
*/
147+
public function testInvalidFlowToOtherGraph()
148+
{
149+
$graph1 = new Graph();
150+
$vg1 = $graph1->createVertex(1);
151+
152+
$graph2 = new Graph();
153+
$vg2 = $graph2->createVertex(2);
154+
155+
new AlgorithmMaxFlowEdmondsKarp($vg1, $vg2);
156+
}
157+
158+
/**
159+
* @expectedException InvalidArgumentException
160+
*/
161+
public function testInvalidFlowToSelf()
162+
{
163+
$graph = new Graph();
164+
$v1 = $graph->createVertex(1);
165+
166+
new AlgorithmMaxFlowEdmondsKarp($v1, $v1);
167+
}
168+
81169
}

tests/MaximumMatching/FlowTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function testSingleEdge()
3131
$this->assertEquals(1, $alg->getNumberOfMatches());
3232
// actual edge instance returned
3333
$this->assertEquals(array($edge), $alg->getEdges());
34+
35+
// check
36+
$flowgraph = $alg->createGraph();
37+
$this->assertInstanceOf('Fhaculty\Graph\Graph', $flowgraph);
3438
}
3539

3640
/**

0 commit comments

Comments
 (0)