Skip to content

Commit 1e0ad5f

Browse files
committed
weighted graph
1 parent 7b144e2 commit 1e0ad5f

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

Diff for: src/jsgraphs.js

+46
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,52 @@ var jsgraphs = jsgraphs || {};
182182

183183
jss.DiGraph = DiGraph;
184184

185+
var Edge = function(v, w, weight) {
186+
this.v = v;
187+
this.w = w;
188+
this.weight = weight;
189+
};
190+
191+
Edge.prototype.either = function() {
192+
return this.v;
193+
};
194+
195+
Edge.prototype.other = function(x) {
196+
return x == this.v ? this.w : this.v;
197+
};
198+
199+
Edge.prototype.from = function() {
200+
return this.v;
201+
};
202+
203+
Edge.prototype.to = function() {
204+
return this.w;
205+
};
206+
207+
jss.Edge = Edge;
208+
209+
var WeightedGraph = function(V) {
210+
this.V = V;
211+
this.adjList = [];
212+
213+
for ( var v = 0; v < V; ++v) {
214+
this.adjList.push([]);
215+
}
216+
};
217+
218+
WeightedGraph.prototype.adj = function(v) {
219+
return this.adjList[v];
220+
};
221+
222+
WeightedGraph.prototype.addEdge = function(e) {
223+
var v = e.either();
224+
var w = e.other(v);
225+
this.adjList[v].push(e);
226+
this.adjList[w].push(e);
227+
};
228+
229+
jss.WeightedGraph = WeightedGraph;
230+
185231
var DepthFirstSearch = function(G, s) {
186232
this.s = s;
187233
var V = G.V;

Diff for: test/weighted-graph-spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var expect = require("chai").expect;
2+
var jsgraphs = require("../src/jsgraphs");
3+
4+
describe("Create various types of Weighted Graphs", function() {
5+
describe("Create weighted undirected graph", function() {
6+
var g = new jsgraphs.WeightedGraph(8);
7+
g.addEdge(new jsgraphs.Edge(0, 7, 0.16));
8+
g.addEdge(new jsgraphs.Edge(2, 3, 0.17));
9+
g.addEdge(new jsgraphs.Edge(1, 7, 0.19));
10+
g.addEdge(new jsgraphs.Edge(0, 2, 0.26));
11+
g.addEdge(new jsgraphs.Edge(5, 7, 0.28));
12+
g.addEdge(new jsgraphs.Edge(1, 3, 0.29));
13+
g.addEdge(new jsgraphs.Edge(1, 5, 0.32));
14+
g.addEdge(new jsgraphs.Edge(2, 7, 0.34));
15+
g.addEdge(new jsgraphs.Edge(4, 5, 0.35));
16+
g.addEdge(new jsgraphs.Edge(1, 2, 0.36));
17+
g.addEdge(new jsgraphs.Edge(4, 7, 0.37));
18+
g.addEdge(new jsgraphs.Edge(0, 4, 0.38));
19+
g.addEdge(new jsgraphs.Edge(6, 2, 0.4));
20+
g.addEdge(new jsgraphs.Edge(3, 6, 0.52));
21+
g.addEdge(new jsgraphs.Edge(6, 0, 0.58));
22+
g.addEdge(new jsgraphs.Edge(6, 4, 0.93));
23+
it("should has 8 vertices", function() {
24+
expect(g.V).to.equal(8);
25+
});
26+
it("should be undirected", function() {
27+
var adj_v = g.adj(0);
28+
for (var i = 0; i < adj_v.length; ++i) {
29+
var e = adj_v[i];
30+
var w = e.other(0);
31+
var adj_w = g.adj(w);
32+
33+
var found = false;
34+
for (var j = 0; j < adj_w.length; ++j) {
35+
var e2 = adj_w[j];
36+
if(e2.other(w) == 0){
37+
found = true;
38+
}
39+
}
40+
41+
expect(found).to.equal(true);
42+
}
43+
});
44+
});
45+
46+
47+
48+
});

0 commit comments

Comments
 (0)