Skip to content

Commit 7f3976d

Browse files
committed
fix the bug with the eager prim
1 parent 54e7a3c commit 7f3976d

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ for(var i=0; i < mst.length; ++i) {
343343

344344
### Use Eager Prim algorithm to find the minimum spanning tree of a weighted graph
345345

346-
The sample code below show how to obtain the minimum spanning tree from a weighted graph using Eager Prim algorithm:
346+
The sample code below show how to obtain the minimum spanning tree from a weighted graph using Eager Prim algorithm (Link: [HTML DEMO](https://rawgit.com/chen0040/js-graph-algorithms/master/examples/example-eager-prim.html)):
347347

348348
```javascript
349349
var jsgraphs = require('js-graph-algorithms');

examples/example-eager-prim.html

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<html>
2+
<head>
3+
<title>
4+
Minimum Spanning Tree Weighted Graph (Eager Prim)
5+
</title>
6+
<script src="../third-party-libs/vis/vis.js" type="text/javascript"></script>
7+
<script src="../src/jsgraphs.js" type="text/javascript"></script>
8+
<link href="../third-party-libs/vis/vis.css" type="text/css" />
9+
</head>
10+
11+
<body>
12+
<h2>Minimum Spanning Tree Weighted Graph (Eager Prim)</h2>
13+
<div id="mynetwork"></div>
14+
15+
<script type="text/javascript">
16+
(function(){
17+
var g = new jsgraphs.WeightedGraph(8);
18+
19+
g.addEdge(new jsgraphs.Edge(0, 7, 0.16));
20+
g.addEdge(new jsgraphs.Edge(2, 3, 0.17));
21+
g.addEdge(new jsgraphs.Edge(1, 7, 0.19));
22+
g.addEdge(new jsgraphs.Edge(0, 2, 0.26));
23+
g.addEdge(new jsgraphs.Edge(5, 7, 0.28));
24+
g.addEdge(new jsgraphs.Edge(1, 3, 0.29));
25+
g.addEdge(new jsgraphs.Edge(1, 5, 0.32));
26+
g.addEdge(new jsgraphs.Edge(2, 7, 0.34));
27+
g.addEdge(new jsgraphs.Edge(4, 5, 0.35));
28+
g.addEdge(new jsgraphs.Edge(1, 2, 0.36));
29+
g.addEdge(new jsgraphs.Edge(4, 7, 0.37));
30+
g.addEdge(new jsgraphs.Edge(0, 4, 0.38));
31+
g.addEdge(new jsgraphs.Edge(6, 2, 0.4));
32+
g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
33+
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
34+
g.addEdge(new jsgraphs.Edge(6, 4, 0.93));
35+
36+
var prim = new jsgraphs.EagerPrimMST(g);
37+
var mst = prim.mst;
38+
39+
40+
var g_nodes = [];
41+
var g_edges = [];
42+
for(var v=0; v < g.V; ++v){
43+
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
44+
g_nodes.push({
45+
id: v,
46+
label: g.node(v).label
47+
});
48+
}
49+
50+
for(var i=0; i < mst.length; ++i) {
51+
var e = mst[i];
52+
var v = e.either();
53+
var w = e.other(v);
54+
e.highlighted = true;
55+
console.log('(' + v + ', ' + w + '): ' + e.weight);
56+
g_edges.push({
57+
from: v,
58+
to: w,
59+
length: e.weight,
60+
label: '' + e.weight,
61+
color: '#ff0000',
62+
value: 2
63+
});
64+
}
65+
66+
for(var v = 0; v < g.V; ++v) {
67+
68+
var adj_v = g.adj(v);
69+
for(var i = 0; i < adj_v.length; ++i) {
70+
var e = adj_v[i];
71+
var w = e.other(v);
72+
if(w > v) continue; // make sure only one edge between w and v since the graph is undirected
73+
if(e.highlighted) continue;
74+
75+
g_edges.push({
76+
from: v,
77+
to: w,
78+
length: e.weight,
79+
label: '' + e.weight
80+
});
81+
};
82+
}
83+
84+
console.log(g.V); // display 6, which is the number of vertices in g
85+
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
86+
87+
var nodes = new vis.DataSet(g_nodes);
88+
89+
// create an array with edges
90+
var edges = new vis.DataSet(g_edges);
91+
92+
// create a network
93+
var container = document.getElementById('mynetwork');
94+
var data = {
95+
nodes: nodes,
96+
edges: edges
97+
};
98+
var options = {};
99+
var network = new vis.Network(container, data, options);
100+
})();
101+
</script>
102+
</body>
103+
</html>

src/jsgraphs.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,12 @@ var jsgraphs = jsgraphs || {};
899899
}
900900
this.mst = [];
901901
this.visit(G, 0);
902-
while(!this.pq.isEmpty() && this.mst.length < V-1) {
902+
while(!this.pq.isEmpty()) {
903903
var e = this.pq.minKey();
904-
var v = this.pq.delMin();
904+
var w = this.pq.delMin();
905905

906906
this.mst.push(e);
907907

908-
var w = e.other(v);
909-
910908
if(!this.marked[w]){
911909
this.visit(G, w);
912910
}
@@ -920,6 +918,7 @@ var jsgraphs = jsgraphs || {};
920918
for(var i = 0; i < adj_v.length; ++i) {
921919
var e = adj_v[i];
922920
var w = e.other(v);
921+
if(this.marked[w]) continue;
923922
if(this.pq.containsIndex(w)){
924923
this.pq.decreaseKey(w, e);
925924
} else {

0 commit comments

Comments
 (0)