Skip to content

Commit b5915b6

Browse files
committed
Fix the indentation of Prim's algorithm
1 parent 3575bb4 commit b5915b6

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

src/graphs/spanning-trees/prim.js

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var Heap = require('../../data-structures/heap').Heap;
88
* @param {number} id The id of the vertex
99
*/
1010
function Vertex(id) {
11-
this.id = id;
11+
'use strict';
12+
this.id = id;
1213
}
1314

1415
/**
@@ -21,9 +22,10 @@ function Vertex(id) {
2122
* @param {number} distance Weight of the node
2223
*/
2324
function Edge(e, v, distance) {
24-
this.e = e;
25-
this.v = v;
26-
this.distance = distance;
25+
'use strict';
26+
this.e = e;
27+
this.v = v;
28+
this.distance = distance;
2729
}
2830

2931
/**
@@ -33,8 +35,8 @@ function Edge(e, v, distance) {
3335
* @public
3436
*/
3537
function Graph(edges) {
36-
console.log(edges);
37-
this.edges = edges || [];
38+
'use strict';
39+
this.edges = edges || [];
3840
}
3941

4042
/**
@@ -44,58 +46,59 @@ function Graph(edges) {
4446
* @return {Graph} Graph which is the minimum spanning tree
4547
*/
4648
Graph.prototype.prim = (function () {
47-
48-
var queue;
49-
50-
/**
51-
* Initialize the algorithm.
52-
*
53-
* @private
54-
*/
55-
function init() {
56-
queue = new Heap(compareEdges);
57-
this.edges.forEach(function (e) {
58-
queue.add(e);
59-
});
60-
}
61-
62-
/**
63-
* Used for comparitions in the heap
64-
*
65-
* @private
66-
* @param {Vertex} a First operand of the comparition
67-
* @param {Vertex} b Second operand of the comparition
68-
* @return {number} Number which which is equal, greater or less then zero and
69-
* indicates whether the first vertex is "greater" than the second.
70-
*/
71-
function compareEdges(a, b) {
72-
return b.distance - a.distance;
73-
}
74-
75-
/**
76-
* Prim's algorithm implementation
77-
*
78-
* @public
79-
* @return {Graph} Minimum spanning tree.
80-
*/
81-
return function () {
82-
init.call(this);
83-
var inTheTree = {},
84-
current = queue.extract(),
85-
spannigTree = [];
49+
'use strict';
50+
51+
var queue;
52+
53+
/**
54+
* Initialize the algorithm.
55+
*
56+
* @private
57+
*/
58+
function init() {
59+
queue = new Heap(compareEdges);
60+
this.edges.forEach(function (e) {
61+
queue.add(e);
62+
});
63+
}
64+
65+
/**
66+
* Used for comparitions in the heap
67+
*
68+
* @private
69+
* @param {Vertex} a First operand of the comparition
70+
* @param {Vertex} b Second operand of the comparition
71+
* @return {number} Number which which is equal, greater or less then zero and
72+
* indicates whether the first vertex is "greater" than the second.
73+
*/
74+
function compareEdges(a, b) {
75+
return b.distance - a.distance;
76+
}
77+
78+
/**
79+
* Prim's algorithm implementation
80+
*
81+
* @public
82+
* @return {Graph} Minimum spanning tree.
83+
*/
84+
return function () {
85+
init.call(this);
86+
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]) {
8695
spannigTree.push(current);
8796
inTheTree[current.e.id] = true;
88-
while (queue.isEmpty()) {
89-
current = queue.extract();
90-
if (!inTheTree[current.v.id] ||
91-
!inTheTree[current.e.id]) {
92-
spannigTree.push(current);
93-
inTheTree[current.e.id] = true;
94-
inTheTree[current.v.id] = true;
95-
}
96-
}
97-
return new Graph(spannigTree);
98-
};
97+
inTheTree[current.v.id] = true;
98+
}
99+
}
100+
return new Graph(spannigTree);
101+
};
99102

100103
}());
101104

0 commit comments

Comments
 (0)