Skip to content

Commit

Permalink
Create edges via Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 19, 2020
1 parent 0833f8f commit 40eed82
Show file tree
Hide file tree
Showing 33 changed files with 228 additions and 228 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=5.3",
"graphp/graph": "dev-master#9b4ff23 as 1.0.0"
"graphp/graph": "dev-master#9ce805c as 1.0.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35"
Expand Down
6 changes: 3 additions & 3 deletions src/MaximumMatching/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ public function getEdges()

if ($group === $groupA) {
// group A: source
$superSource->createEdgeTo($vertex)->setCapacity(1)->setFlow(0);
$graphFlow->createEdgeDirected($superSource, $vertex)->setCapacity(1)->setFlow(0);

// temporarily create edges from A->B for flow graph
$originalVertex = $this->graph->getVertex($vertex->getId());
foreach ($originalVertex->getVerticesEdgeTo() as $vertexTarget) {
$vertex->createEdgeTo($graphFlow->getVertex($vertexTarget->getId()))->setCapacity(1)->setFlow(0);
$graphFlow->createEdgeDirected($vertex, $graphFlow->getVertex($vertexTarget->getId()))->setCapacity(1)->setFlow(0);
}
} else {
// group B: sink
$vertex->createEdgeTo($superSink)->setCapacity(1)->setFlow(0);
$graphFlow->createEdgeDirected($vertex, $superSink)->setCapacity(1)->setFlow(0);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/MinimumCostFlow/CycleCanceling.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function createGraph()

if ($balance > 0) {
// positive balance => source capacity
$superSource->createEdgeTo($vertex)->setCapacity($balance);
$resultGraph->createEdgeDirected($superSource, $vertex)->setCapacity($balance);

$sumBalance += $balance;
} elseif ($balance < 0) {
// negative balance => sink capacity (positive)
$vertex->createEdgeTo($superSink)->setCapacity(-$balance);
$resultGraph->createEdgeDirected($vertex, $superSink)->setCapacity(-$balance);
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/BipartitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testGraphPairIsBipartit()
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdgeTo($v2);
$graph->createEdgeDirected($v1, $v2);

$alg = new AlgorithmBipartit($graph);

Expand Down Expand Up @@ -59,9 +59,9 @@ public function testGraphTriangleCycleIsNotBipartit()
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v1->createEdgeTo($v2);
$v2->createEdgeTo($v3);
$v3->createEdgeTo($v1);
$graph->createEdgeDirected($v1, $v2);
$graph->createEdgeDirected($v2, $v3);
$graph->createEdgeDirected($v3, $v1);

$alg = new AlgorithmBipartit($graph);

Expand Down
12 changes: 6 additions & 6 deletions tests/CompleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testGraphSimplePairK2()
{
// 1 -- 2
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));
$graph->createEdgeUndirected($graph->createVertex(1), $graph->createVertex(2));

$alg = new AlgorithmComplete($graph);

Expand All @@ -39,7 +39,7 @@ public function testGraphSingleDirectedIsNotComplete()
{
// 1 -> 2
$graph = new Graph();
$graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
$graph->createEdgeDirected($graph->createVertex(1), $graph->createVertex(2));

$alg = new AlgorithmComplete($graph);

Expand All @@ -53,10 +53,10 @@ public function testAdditionalEdgesToNotAffectCompleteness()
// 2 -> 1
// 1 -> 1
$graph = new Graph();
$graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
$graph->getVertex(1)->createEdge($graph->getVertex(2));
$graph->getVertex(2)->createEdgeTo($graph->getVertex(1));
$graph->getVertex(1)->createEdgeTo($graph->getVertex(1));
$graph->createEdgeDirected($graph->createVertex(1), $graph->createVertex(2));
$graph->createEdgeUndirected($graph->getVertex(1), $graph->getVertex(2));
$graph->createEdgeDirected($graph->getVertex(2), $graph->getVertex(1));
$graph->createEdgeDirected($graph->getVertex(1), $graph->getVertex(1));

$alg = new AlgorithmComplete($graph);

Expand Down
12 changes: 6 additions & 6 deletions tests/ConnectedComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function testGraphEdgeDirections()
{
// 1 -- 2 -> 3 <- 4
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));
$graph->getVertex(2)->createEdgeTo($graph->createVertex(3));
$graph->createVertex(4)->createEdgeTo($graph->getVertex(3));
$graph->createEdgeUndirected($graph->createVertex(1), $graph->createVertex(2));
$graph->createEdgeDirected($graph->getVertex(2), $graph->createVertex(3));
$graph->createEdgeDirected($graph->createVertex(4), $graph->getVertex(3));

$alg = new AlgorithmConnected($graph);

Expand All @@ -61,8 +61,8 @@ public function testComponents()
$v3 = $graph->createVertex(3);
$v4 = $graph->createVertex(4);
$v5 = $graph->createVertex(5);
$v1->createEdge($v2);
$v3->createEdgeTo($v4);
$graph->createEdgeUndirected($v1, $v2);
$graph->createEdgeDirected($v3, $v4);

$alg = new AlgorithmConnected($graph);

Expand All @@ -73,7 +73,7 @@ public function testComponents()
$this->assertCount(3, $graphs);

$ge = new Graph();
$ge->createVertex(1)->createEdge($ge->createVertex(2));
$ge->createEdgeUndirected($ge->createVertex(1), $ge->createVertex(2));
$this->assertGraphEquals($ge, $alg->createGraphComponentVertex($v2));

$ge = new Graph();
Expand Down
4 changes: 2 additions & 2 deletions tests/DegreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function testGraphIrregular()
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v1->createEdgeTo($v2);
$v2->createEdgeTo($v3);
$graph->createEdgeDirected($v1, $v2);
$graph->createEdgeDirected($v2, $v3);

$alg = new AlgorithmDegree($graph);

Expand Down
22 changes: 11 additions & 11 deletions tests/DetectNegativeCycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testNegativeLoop()
// 1 --[-1]--> 1
$graph = new Graph();
$v1 = $graph->createVertex(1);
$e1 = $v1->createEdgeTo($v1)->setWeight(-1);
$e1 = $graph->createEdgeDirected($v1, $v1)->setWeight(-1);

$alg = new DetectNegativeCycle($graph);

Expand All @@ -65,8 +65,8 @@ public function testNegativeCycle()
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdgeTo($v2)->setWeight(-1);
$v2->createEdgeTo($v1)->setWeight(-2);
$graph->createEdgeDirected($v1, $v2)->setWeight(-1);
$graph->createEdgeDirected($v2, $v1)->setWeight(-2);

$alg = new DetectNegativeCycle($graph);

Expand All @@ -84,7 +84,7 @@ public function testNegativeUndirectedIsNegativeCycle()
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdge($v2)->setWeight(-1);
$graph->createEdgeUndirected($v1, $v2)->setWeight(-1);

$alg = new DetectNegativeCycle($graph);

Expand All @@ -106,10 +106,10 @@ public function testNegativeCycleSubgraph()
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v4 = $graph->createVertex(4);
$v1->createEdgeTo($v2)->setWeight(1);
$v2->createEdgeTo($v3)->setWeight(1);
$v3->createEdgeTo($v4)->setWeight(1);
$v4->createEdgeTo($v3)->setWeight(-2);
$graph->createEdgeDirected($v1, $v2)->setWeight(1);
$graph->createEdgeDirected($v2, $v3)->setWeight(1);
$graph->createEdgeDirected($v3, $v4)->setWeight(1);
$graph->createEdgeDirected($v4, $v3)->setWeight(-2);

$alg = new DetectNegativeCycle($graph);

Expand All @@ -133,9 +133,9 @@ public function testNegativeComponents()
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v4 = $graph->createVertex(4);
$v1->createEdge($v2);
$v3->createEdgeTo($v4)->setWeight(-1);
$v4->createEdgeTo($v3)->setWeight(-2);
$graph->createEdgeUndirected($v1, $v2);
$graph->createEdgeDirected($v3, $v4)->setWeight(-1);
$graph->createEdgeDirected($v4, $v3)->setWeight(-2);

$alg = new DetectNegativeCycle($graph);

Expand Down
8 changes: 4 additions & 4 deletions tests/DirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testGraphUndirected()
{
// 1 -- 2
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));
$graph->createEdgeUndirected($graph->createVertex(1), $graph->createVertex(2));

$alg = new AlgorithmDirected($graph);

Expand All @@ -33,7 +33,7 @@ public function testGraphDirected()
{
// 1 -> 2
$graph = new Graph();
$graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
$graph->createEdgeDirected($graph->createVertex(1), $graph->createVertex(2));

$alg = new AlgorithmDirected($graph);

Expand All @@ -46,8 +46,8 @@ public function testGraphMixed()
{
// 1 -- 2 -> 3
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));
$graph->getVertex(2)->createEdgeTo($graph->createVertex(3));
$graph->createEdgeUndirected($graph->createVertex(1), $graph->createVertex(2));
$graph->createEdgeDirected($graph->getVertex(2), $graph->createVertex(3));

$alg = new AlgorithmDirected($graph);

Expand Down
8 changes: 4 additions & 4 deletions tests/EulerianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testGraphPairHasNoCycle()
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdge($v2);
$graph->createEdgeUndirected($v1, $v2);

$alg = new AlgorithmEulerian($graph);

Expand All @@ -34,9 +34,9 @@ public function testGraphTriangleCycleIsNotBipartit()
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v3 = $graph->createVertex(3);
$v1->createEdge($v2);
$v2->createEdge($v3);
$v3->createEdge($v1);
$graph->createEdgeUndirected($v1, $v2);
$graph->createEdgeUndirected($v2, $v3);
$graph->createEdgeUndirected($v3, $v1);

$alg = new AlgorithmEulerian($graph);

Expand Down
8 changes: 4 additions & 4 deletions tests/FlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testEdgeWithZeroFlowIsConsideredFlow()
{
// 1 -> 2
$graph = new Graph();
$graph->createVertex(1)->createEdgeTo($graph->createVertex(2))->setFlow(0);
$graph->createEdgeDirected($graph->createVertex(1), $graph->createVertex(2))->setFlow(0);


$alg = new AlgorithmFlow($graph);
Expand All @@ -40,7 +40,7 @@ public function testEdgeWithZeroFlowIsConsideredFlow()
public function testGraphSimple(Graph $graph)
{
// 1 -> 2
$graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
$graph->createEdgeDirected($graph->createVertex(1), $graph->createVertex(2));

$alg = new AlgorithmFlow($graph);

Expand All @@ -59,7 +59,7 @@ public function testGraphSimple(Graph $graph)
public function testGraphWithUnweightedEdges(Graph $graph)
{
// additional flow edge: 2 -> 3
$graph->getVertex(2)->createEdgeTo($graph->createVertex(3))->setFlow(10);
$graph->createEdgeDirected($graph->getVertex(2), $graph->createVertex(3))->setFlow(10);

$alg = new AlgorithmFlow($graph);

Expand Down Expand Up @@ -88,7 +88,7 @@ public function testVertexWithUndirectedEdgeHasInvalidFlow()
{
// 1 -- 2
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2))->setFlow(10);
$graph->createEdgeUndirected($graph->createVertex(1), $graph->createVertex(2))->setFlow(10);


$alg = new AlgorithmFlow($graph);
Expand Down
8 changes: 4 additions & 4 deletions tests/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testGraphPairIsBipartit()
$graph = new Graph();
$v1 = $graph->createVertex(1)->setGroup(1);
$v2 = $graph->createVertex(2)->setGroup(2);
$v1->createEdgeTo($v2);
$graph->createEdgeDirected($v1, $v2);

$alg = new AlgorithmGroups($graph);

Expand All @@ -45,9 +45,9 @@ public function testGraphTriangleCycleIsNotBipartit()
$v1 = $graph->createVertex(1)->setGroup(1);
$v2 = $graph->createVertex(2)->setGroup(2);
$v3 = $graph->createVertex(3)->setGroup(1);
$v1->createEdgeTo($v2);
$v2->createEdgeTo($v3);
$v3->createEdgeTo($v1);
$graph->createEdgeDirected($v1, $v2);
$graph->createEdgeDirected($v2, $v3);
$graph->createEdgeDirected($v3, $v1);

$alg = new AlgorithmGroups($graph);

Expand Down
8 changes: 4 additions & 4 deletions tests/LoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function testGraphWithMixedCircuitIsNotConsideredLoop()
$graph = new Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v1->createEdgeTo($v2);
$v2->createEdge($v1);
$graph->createEdgeDirected($v1, $v2);
$graph->createEdgeUndirected($v2, $v1);

$alg = new AlgorithmLoop($graph);

Expand All @@ -35,7 +35,7 @@ public function testGraphUndirectedLoop()
{
// 1 -- 1
$graph = new Graph();
$graph->createVertex(1)->createEdge($v1 = $graph->getVertex(1));
$graph->createEdgeUndirected($graph->createVertex(1), $v1 = $graph->getVertex(1));

$alg = new AlgorithmLoop($graph);

Expand All @@ -47,7 +47,7 @@ public function testGraphDirectedLoop()
{
// 1 -> 1
$graph = new Graph();
$graph->createVertex(1)->createEdgeTo($v1 = $graph->getVertex(1));
$graph->createEdgeDirected($graph->createVertex(1), $v1 = $graph->getVertex(1));

$alg = new AlgorithmLoop($graph);

Expand Down
Loading

0 comments on commit 40eed82

Please sign in to comment.