File tree Expand file tree Collapse file tree 3 files changed +28
-21
lines changed Expand file tree Collapse file tree 3 files changed +28
-21
lines changed Original file line number Diff line number Diff line change 6
6
use Fhaculty \Graph \Set \Edges ;
7
7
use Fhaculty \Graph \Exception \UnexpectedValueException ;
8
8
use Fhaculty \Graph \Edge \Directed as EdgeDirected ;
9
+ use Fhaculty \Graph \Edge \Base as Edge ;
10
+ use SplPriorityQueue ;
9
11
10
12
/**
11
13
* Abstract base class for minimum spanning tree (MST) algorithms
@@ -51,4 +53,28 @@ abstract public function getEdges();
51
53
* @return Graph
52
54
*/
53
55
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
+ }
54
80
}
Original file line number Diff line number Diff line change @@ -40,18 +40,7 @@ public function getEdges()
40
40
$ sortedEdges = new SplPriorityQueue ();
41
41
42
42
// 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 );
55
44
56
45
$ returnEdges = array ();
57
46
Original file line number Diff line number Diff line change @@ -40,15 +40,7 @@ public function getEdges()
40
40
41
41
// get unvisited vertex of the edge and add edges from new vertex
42
42
// 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 );
52
44
53
45
do {
54
46
try {
You can’t perform that action at this time.
0 commit comments