@@ -8,7 +8,8 @@ var Heap = require('../../data-structures/heap').Heap;
8
8
* @param {number } id The id of the vertex
9
9
*/
10
10
function Vertex ( id ) {
11
- this . id = id ;
11
+ 'use strict' ;
12
+ this . id = id ;
12
13
}
13
14
14
15
/**
@@ -21,9 +22,10 @@ function Vertex(id) {
21
22
* @param {number } distance Weight of the node
22
23
*/
23
24
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 ;
27
29
}
28
30
29
31
/**
@@ -33,8 +35,8 @@ function Edge(e, v, distance) {
33
35
* @public
34
36
*/
35
37
function Graph ( edges ) {
36
- console . log ( edges ) ;
37
- this . edges = edges || [ ] ;
38
+ 'use strict' ;
39
+ this . edges = edges || [ ] ;
38
40
}
39
41
40
42
/**
@@ -44,58 +46,59 @@ function Graph(edges) {
44
46
* @return {Graph } Graph which is the minimum spanning tree
45
47
*/
46
48
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 ] ) {
86
95
spannigTree . push ( current ) ;
87
96
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
+ } ;
99
102
100
103
} ( ) ) ;
101
104
0 commit comments