Skip to content

Commit c00e170

Browse files
committed
Add support for directed and mixed graphs
1 parent 303582b commit c00e170

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/MinimumSpanningTree/Base.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fhaculty\Graph\Algorithm\Base as AlgorithmBase;
66
use Fhaculty\Graph\Set\Edges;
7-
use Fhaculty\Graph\Exception\UnexpectedValueException;
87
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
98
use Fhaculty\Graph\Edge\Base as Edge;
109
use SplPriorityQueue;
@@ -21,6 +20,11 @@
2120
* Because a null graph (a Graph with no vertices) is not considered connected,
2221
* it also can not contain a spanning tree.
2322
*
23+
* Most authors demand that the input graph has to be undirected, whereas this
24+
* library supports also directed and mixed graphs. The actual direction of the
25+
* edge will be ignored, only its incident vertices will be checked. This is
26+
* done in order to be consistent to how ConnectedComponents are checked.
27+
*
2428
* @link http://en.wikipedia.org/wiki/Minimum_Spanning_Tree
2529
* @link http://en.wikipedia.org/wiki/Spanning_Tree
2630
* @link http://mathoverflow.net/questions/120536/is-the-empty-graph-a-tree
@@ -71,7 +75,6 @@ public function getWeight()
7175
*
7276
* @param Edges $edges
7377
* @param SplPriorityQueue $sortedEdges
74-
* @throws UnexpectedValueException when encountering directed edges
7578
*/
7679
protected function addEdgesSorted(Edges $edges, SplPriorityQueue $sortedEdges)
7780
{
@@ -80,10 +83,6 @@ protected function addEdgesSorted(Edges $edges, SplPriorityQueue $sortedEdges)
8083
/* @var $edge Edge */
8184
// ignore loops (a->a)
8285
if (!$edge->isLoop()) {
83-
if ($edge instanceof EdgeDirected) {
84-
throw new UnexpectedValueException('Minimum spanning tree for directed edges not supported');
85-
}
86-
8786
// Add edges with negative weight because of order in stl
8887
$sortedEdges->insert($edge, -$edge->getWeight());
8988
}

src/MinimumSpanningTree/Prim.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ public function getEdges()
5353

5454
// Check if edge is between unmarked and marked edge
5555

56-
$vertexA = $cheapestEdge->getVerticesStart()->getVertexFirst();
57-
$vertexB = $cheapestEdge->getVertexToFrom($vertexA);
56+
$vertices = $cheapestEdge->getVertices();
57+
$vertexA = $vertices->getVertexFirst();
58+
$vertexB = $vertices->getVertexLast();
5859

5960
// Edge is between marked and unmared vertex
6061
} while (!(isset($markInserted[$vertexA->getId()]) XOR isset($markInserted[$vertexB->getId()])));

0 commit comments

Comments
 (0)