Skip to content

Commit 3fddf25

Browse files
authored
Merge pull request #15 from datavis-tech/weighted-edges
adding edge weights
2 parents c3d6975 + 6a13f82 commit 3fddf25

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
package-lock.json
3+
*.swp

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,26 @@ Removes the specified node. Returns *graph* to support method chaining. The argu
116116

117117
### Adding and Removing Edges
118118

119-
<a name="add-edge" href="#add-edge">#</a> <i>graph</i>.<b>addEdge</b>(<i>u</i>, <i>v</i>)
119+
<a name="add-edge" href="#add-edge">#</a> <i>graph</i>.<b>addEdge</b>(<i>u</i>, <i>v</i>[,<i>weight</i>])
120120

121121
Adds an edge from node *u* to node *v*. Returns *graph* to support method chaining. The arguments *u* and *v* are string identifiers for nodes. This function also adds *u* and *v* as nodes if they were not already added.
122122

123+
The last argument *weight* (optional) specifies the weight of this edge.
124+
123125
<a name="remove-edge" href="#remove-edge">#</a> <i>graph</i>.<b>removeEdge</b>(<i>u</i>, <i>v</i>)
124126

125127
Removes the edge from node *u* to node *v*. Returns *graph* to support method chaining. The arguments *u* and *v* are string identifiers for nodes. This function does not remove the nodes *u* and *v*. Does nothing if the edge does not exist.
126128

129+
### Working with Edge Weights
130+
131+
<a name="set-edge-weight" href="#set-edge-weight">#</a> <i>graph</i>.<b>setEdgeWeight</b>(<i>u</i>, <i>v</i>, <i>weight</i>)
132+
133+
Sets the *weight* (a number) of the edge from node *u* to node *v*.
134+
135+
<a name="get-edge-weight" href="#get-edge-weight">#</a> <i>graph</i>.<b>getEdgeWeight</b>(<i>u</i>, <i>v</i>, <i>weight</i>)
136+
137+
Gets the *weight* of the edge from node *u* to node *v*. If no weight was previously set on this edge, then the value 1 is returned.
138+
127139
### Querying the Graph
128140

129141
<a name="nodes" href="#nodes">#</a> <i>graph</i>.<b>nodes</b>()

index.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = function Graph(serialized){
99
adjacent: adjacent,
1010
addEdge: addEdge,
1111
removeEdge: removeEdge,
12+
setEdgeWeight: setEdgeWeight,
13+
getEdgeWeight: getEdgeWeight,
1214
indegree: indegree,
1315
outdegree: outdegree,
1416
depthFirstSearch: depthFirstSearch,
@@ -22,6 +24,11 @@ module.exports = function Graph(serialized){
2224
// Values are adjacent node id arrays.
2325
var edges = {};
2426

27+
// The weights of edges.
28+
// Keys are string encodings of edges.
29+
// Values are weights (numbers).
30+
var edgeWeights = {};
31+
2532
// If a serialized graph was passed into the constructor, deserialize it.
2633
if(serialized){
2734
deserialize(serialized);
@@ -72,12 +79,36 @@ module.exports = function Graph(serialized){
7279
return edges[node] || [];
7380
}
7481

82+
// Computes a string encoding of an edge,
83+
// for use as a key in an object.
84+
function encodeEdge(u, v){
85+
return u + "|" + v;
86+
}
87+
88+
// Sets the weight of the given edge.
89+
function setEdgeWeight(u, v, weight){
90+
edgeWeights[encodeEdge(u, v)] = weight;
91+
return graph;
92+
}
93+
94+
// Gets the weight of the given edge.
95+
// Returns 1 if no weight was previously set.
96+
function getEdgeWeight(u, v){
97+
var weight = edgeWeights[encodeEdge(u, v)];
98+
return weight === undefined ? 1 : weight;
99+
}
100+
75101
// Adds an edge from node u to node v.
76102
// Implicitly adds the nodes if they were not already added.
77-
function addEdge(u, v){
103+
function addEdge(u, v, weight){
78104
addNode(u);
79105
addNode(v);
80106
adjacent(u).push(v);
107+
108+
if (weight !== undefined) {
109+
setEdgeWeight(u, v, weight);
110+
}
111+
81112
return graph;
82113
}
83114

test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,27 @@ describe("Graph", function() {
311311
checkSerialized(graph.serialize());
312312
});
313313
});
314+
315+
describe("Edge Weights", function() {
316+
317+
it("Should set and get an edge weight.", function (){
318+
var graph = Graph().addEdge("a", "b", 5);
319+
assert.equal(graph.getEdgeWeight("a", "b"), 5);
320+
});
321+
322+
it("Should set edge weight via setEdgeWeight.", function (){
323+
var graph = Graph()
324+
.addEdge("a", "b")
325+
.setEdgeWeight("a", "b", 5);
326+
assert.equal(graph.getEdgeWeight("a", "b"), 5);
327+
});
328+
329+
it("Should return weight of 1 if no weight set.", function (){
330+
var graph = Graph().addEdge("a", "b");
331+
assert.equal(graph.getEdgeWeight("a", "b"), 1);
332+
});
333+
334+
});
314335
});
315336

316337
function contains(arr, item){

0 commit comments

Comments
 (0)