Skip to content

Commit 9d89993

Browse files
committed
Fix the Prim's MST algorithm
1 parent 9a53c98 commit 9d89993

File tree

1 file changed

+74
-79
lines changed

1 file changed

+74
-79
lines changed

src/graphs/spanning-trees/prim.js

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ function Edge(e, v, distance) {
3434
* @constructor
3535
* @public
3636
*/
37-
function Graph(edges) {
37+
function Graph(edges, nodesCount) {
3838
'use strict';
3939
this.edges = edges || [];
40+
this.nodesCount = nodesCount || 0;
4041
}
4142

4243
/**
@@ -57,9 +58,6 @@ Graph.prototype.prim = (function () {
5758
*/
5859
function init() {
5960
queue = new Heap(compareEdges);
60-
this.edges.forEach(function (e) {
61-
queue.add(e);
62-
});
6361
}
6462

6563
/**
@@ -84,18 +82,57 @@ Graph.prototype.prim = (function () {
8482
return function () {
8583
init.call(this);
8684
var inTheTree = {},
87-
current = queue.extract(),
88-
spannigTree = [];
89-
spannigTree.push(current);
90-
inTheTree[current.e.id] = true;
91-
while (queue.isEmpty()) {
92-
current = queue.extract();
93-
if (!inTheTree[current.v.id] ||
94-
!inTheTree[current.e.id]) {
95-
spannigTree.push(current);
96-
inTheTree[current.e.id] = true;
97-
inTheTree[current.v.id] = true;
98-
}
85+
startVertex = this.edges[0].e.id,
86+
spannigTree = [],
87+
parents = {},
88+
distances = {},
89+
current;
90+
inTheTree[startVertex] = true;
91+
queue.add({
92+
node: startVertex,
93+
distance: 0
94+
});
95+
for (var i = 0; i < this.nodesCount - 1; i += 1) {
96+
current = queue.extract().node;
97+
inTheTree[current] = true;
98+
this.edges.forEach(function (e) {
99+
if (inTheTree[e.v.id] && inTheTree[e.e.id]) {
100+
return;
101+
}
102+
var collection = queue.getCollection(),
103+
node;
104+
if (e.e.id === current) {
105+
node = e.v.id;
106+
} else if (e.v.id === current) {
107+
node = e.e.id;
108+
} else {
109+
return;
110+
}
111+
for (var i = 0; i < collection.length; i += 1) {
112+
if (collection[i].node === node) {
113+
if (collection[i].distance > e.distance) {
114+
queue.changeKey(i, {
115+
node: node,
116+
distance: e.distance
117+
});
118+
parents[node] = current;
119+
distances[node] = e.distance;
120+
}
121+
return;
122+
}
123+
}
124+
queue.add({
125+
node: node,
126+
distance: e.distance
127+
});
128+
parents[node] = current;
129+
distances[node] = e.distance;
130+
});
131+
console.log(queue._heap);
132+
console.log();
133+
}
134+
for (var node in parents) {
135+
spannigTree.push(new Edge(node, parents[node], distances[node]));
99136
}
100137
return new Graph(spannigTree);
101138
};
@@ -104,71 +141,29 @@ Graph.prototype.prim = (function () {
104141

105142
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
106143
* * * * * * * * * * * * * * Sample graph * * * * * * * * * * *
107-
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
144+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
108145

109-
var graph;
110-
111146
(function () {
112-
var edges = [];
113-
114-
edges.push(new Edge(
115-
new Vertex(0),
116-
new Vertex(1),
117-
7
118-
));
119-
120-
edges.push(new Edge(
121-
new Vertex(0),
122-
new Vertex(2),
123-
9
124-
));
125-
126-
edges.push(new Edge(
127-
new Vertex(0),
128-
new Vertex(5),
129-
16
130-
));
131-
132-
edges.push(new Edge(
133-
new Vertex(1),
134-
new Vertex(2),
135-
10
136-
));
137-
138-
edges.push(new Edge(
139-
new Vertex(1),
140-
new Vertex(3),
141-
15
142-
));
143-
144-
edges.push(new Edge(
145-
new Vertex(2),
146-
new Vertex(3),
147-
11
148-
));
149-
150-
edges.push(new Edge(
151-
new Vertex(2),
152-
new Vertex(5),
153-
2
154-
));
155-
156-
edges.push(new Edge(
157-
new Vertex(3),
158-
new Vertex(4),
159-
6
160-
));
161-
162-
edges.push(new Edge(
163-
new Vertex(4),
164-
new Vertex(5),
165-
9
166-
));
167-
168-
graph = new Graph(edges);
169-
147+
'use strict';
148+
var graph, edges = [];
149+
edges.push(new Edge(new Vertex(0), new Vertex(1), 4));
150+
edges.push(new Edge(new Vertex(0), new Vertex(7), 8));
151+
edges.push(new Edge(new Vertex(1), new Vertex(7), 11));
152+
edges.push(new Edge(new Vertex(1), new Vertex(2), 8));
153+
edges.push(new Edge(new Vertex(2), new Vertex(8), 2));
154+
edges.push(new Edge(new Vertex(2), new Vertex(3), 7));
155+
edges.push(new Edge(new Vertex(2), new Vertex(5), 4));
156+
edges.push(new Edge(new Vertex(2), new Vertex(3), 7));
157+
edges.push(new Edge(new Vertex(3), new Vertex(4), 9));
158+
edges.push(new Edge(new Vertex(3), new Vertex(5), 14));
159+
edges.push(new Edge(new Vertex(4), new Vertex(5), 10));
160+
edges.push(new Edge(new Vertex(5), new Vertex(6), 2));
161+
edges.push(new Edge(new Vertex(6), new Vertex(8), 6));
162+
edges.push(new Edge(new Vertex(8), new Vertex(7), 7));
163+
graph = new Graph(edges, 9);
164+
165+
console.log(graph.prim());
170166
}());
171167

172-
console.log(graph.prim());
173168

174-
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
169+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

0 commit comments

Comments
 (0)