forked from SiZapPaaiGwat/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.js
More file actions
156 lines (147 loc) · 4.6 KB
/
prim.js
File metadata and controls
156 lines (147 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Prim's algorithm is a greedy algorithm that finds a minimum
* spanning tree for a connected weighted undirected graph.
*
* @example
*
* var Prim = require('path-to-algorithms/src/graphs/spanning-trees/prim');
* var Graph = Prim.Graph;
* var Edge = Prim.Edge;
* var Vertex = Prim.Vertex;
*
* var graph, edges = [];
* edges.push(new Edge(new Vertex(0), new Vertex(1), 4));
* edges.push(new Edge(new Vertex(0), new Vertex(7), 8));
* edges.push(new Edge(new Vertex(1), new Vertex(7), 11));
* edges.push(new Edge(new Vertex(1), new Vertex(2), 8));
* edges.push(new Edge(new Vertex(2), new Vertex(8), 2));
* edges.push(new Edge(new Vertex(2), new Vertex(3), 7));
* edges.push(new Edge(new Vertex(2), new Vertex(5), 4));
* edges.push(new Edge(new Vertex(2), new Vertex(3), 7));
* edges.push(new Edge(new Vertex(3), new Vertex(4), 9));
* edges.push(new Edge(new Vertex(3), new Vertex(5), 14));
* edges.push(new Edge(new Vertex(4), new Vertex(5), 10));
* edges.push(new Edge(new Vertex(5), new Vertex(6), 2));
* edges.push(new Edge(new Vertex(6), new Vertex(8), 6));
* edges.push(new Edge(new Vertex(8), new Vertex(7), 7));
* graph = new Graph(edges, edges.length);
*
* // { edges:
* // [ { e: '1', v: 0, distance: 4 },
* // { e: '2', v: 8, distance: 2 },
* // { e: '3', v: 2, distance: 7 },
* // { e: '4', v: 3, distance: 9 },
* // { e: '5', v: 2, distance: 4 },
* // { e: '6', v: 5, distance: 2 },
* // { e: '7', v: 0, distance: 8 },
* // { e: '8', v: 7, distance: 7 } ],
* // nodesCount: 0 }
* var spanningTree = graph.prim();
*
* @module graphs/spanning-trees/prim
*/
(function (exports) {
'use strict';
var Heap = require('../../data-structures/heap').Heap;
exports.Vertex = require('../../data-structures/vertex').Vertex;
exports.Edge = require('../../data-structures/edge').Edge;
/**
* Graph.
*
* @constructor
* @public
* @param {Array} edges Array with graph edges.
* @param {Number} nodesCount Number of nodes in graph.
*/
exports.Graph = function (edges, nodesCount) {
this.edges = edges || [];
this.nodesCount = nodesCount || 0;
};
/**
* Executes Prim's algorithm and returns minimum spanning tree.
*
* @public
* @method
* @return {Graph} Graph which is the minimum spanning tree.
*/
exports.Graph.prototype.prim = (function () {
var queue;
/**
* Used for comparitions in the heap
*
* @private
* @param {Vertex} a First operand of the comparition.
* @param {Vertex} b Second operand of the comparition.
* @return {number} Number which which is equal, greater or
* less then zero and indicates whether the first vertex is
* "greater" than the second.
*/
function compareEdges(a, b) {
return b.distance - a.distance;
}
/**
* Initialize the algorithm.
*
* @private
*/
function init() {
queue = new Heap(compareEdges);
}
return function () {
init.call(this);
var inTheTree = {};
var startVertex = this.edges[0].e.id;
var spannigTree = [];
var parents = {};
var distances = {};
var current;
inTheTree[startVertex] = true;
queue.add({
node: startVertex,
distance: 0
});
for (var i = 0; i < this.nodesCount - 1; i += 1) {
current = queue.extract().node;
inTheTree[current] = true;
this.edges.forEach(function (e) {
if (inTheTree[e.v.id] && inTheTree[e.e.id]) {
return;
}
var collection = queue.getCollection();
var node;
if (e.e.id === current) {
node = e.v.id;
} else if (e.v.id === current) {
node = e.e.id;
} else {
return;
}
for (var i = 0; i < collection.length; i += 1) {
if (collection[i].node === node) {
if (collection[i].distance > e.distance) {
queue.changeKey(i, {
node: node,
distance: e.distance
});
parents[node] = current;
distances[node] = e.distance;
}
return;
}
}
queue.add({
node: node,
distance: e.distance
});
parents[node] = current;
distances[node] = e.distance;
});
}
for (var node in parents) {
spannigTree.push(
new exports.Edge(node, parents[node], distances[node]));
}
return new exports.Graph(spannigTree);
};
}());
})(typeof window === 'undefined' ? module.exports : window);