Skip to content

Commit 4bf420c

Browse files
committed
Move duplicate logic to base class
1 parent e6b6121 commit 4bf420c

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/MinimumSpanningTree/Base.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Fhaculty\Graph\Set\Edges;
77
use Fhaculty\Graph\Exception\UnexpectedValueException;
88
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
9+
use Fhaculty\Graph\Edge\Base as Edge;
10+
use SplPriorityQueue;
911

1012
/**
1113
* Abstract base class for minimum spanning tree (MST) algorithms
@@ -51,4 +53,28 @@ abstract public function getEdges();
5153
* @return Graph
5254
*/
5355
abstract protected function getGraph();
56+
57+
/**
58+
* helper method to add a set of Edges to the given set of sorted edges
59+
*
60+
* @param Edges $edges
61+
* @param SplPriorityQueue $sortedEdges
62+
* @throws UnexpectedValueException when encountering directed edges
63+
*/
64+
protected function addEdgesSorted(Edges $edges, SplPriorityQueue $sortedEdges)
65+
{
66+
// For all edges
67+
foreach ($edges as $edge) {
68+
/* @var $edge Edge */
69+
// ignore loops (a->a)
70+
if (!$edge->isLoop()) {
71+
if ($edge instanceof EdgeDirected) {
72+
throw new UnexpectedValueException('Minimum spanning tree for directed edges not supported');
73+
}
74+
75+
// Add edges with negative weight because of order in stl
76+
$sortedEdges->insert($edge, -$edge->getWeight());
77+
}
78+
}
79+
}
5480
}

src/MinimumSpanningTree/Kruskal.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,7 @@ public function getEdges()
4040
$sortedEdges = new SplPriorityQueue();
4141

4242
// For all edges
43-
foreach ($this->graph->getEdges() as $edge) {
44-
// ignore loops (a->a)
45-
if (!$edge->isLoop()) {
46-
if ($edge instanceof EdgeDirected) {
47-
throw new UnexpectedValueException('Kruskal for directed edges not supported');
48-
}
49-
// Add edges with negativ Weight because of order in stl
50-
$sortedEdges->insert($edge, -$edge->getWeight());
51-
}
52-
}
53-
54-
// $sortedEdges = $this->graph->getEdgesOrdered('weight');
43+
$this->addEdgesSorted($this->graph->getEdges(), $sortedEdges);
5544

5645
$returnEdges = array();
5746

src/MinimumSpanningTree/Prim.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,7 @@ public function getEdges()
4040

4141
// get unvisited vertex of the edge and add edges from new vertex
4242
// Add all edges from $currentVertex to priority queue
43-
foreach ($vertexCurrent->getEdges() as $currentEdge) {
44-
if (!$currentEdge->isLoop()) {
45-
if ($currentEdge instanceof EdgeDirected) {
46-
throw new UnexpectedValueException('Unable to create MST for directed graphs');
47-
}
48-
// Add edges to priority queue with inverted weights (priority queue has high values at the front)
49-
$edgeQueue->insert($currentEdge, -$currentEdge->getWeight());
50-
}
51-
}
43+
$this->addEdgesSorted($vertexCurrent->getEdges(), $edgeQueue);
5244

5345
do {
5446
try {

0 commit comments

Comments
 (0)