Skip to content

Commit 1aaa9ad

Browse files
authored
Merge pull request #47 from datavis-tech/has-edge
Add hasEdge method
2 parents 226cb0e + 518451f commit 1aaa9ad

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ The last argument *weight* (optional) specifies the weight of this edge.
126126

127127
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.
128128

129+
<a name="has-edge" href="#has-edge">#</a> <i>graph</i>.<b>hasEdge</b>(<i>u</i>, <i>v</i>)
130+
131+
Returns `true` if there exists an edge from node *u* to node *v*. Returns `false` otherwise.
132+
129133
### Working with Edge Weights
130134

131135
<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>)

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare function Graph(serialized?: Serialized): {
1717
adjacent: (node: NodeId) => NodeId[];
1818
addEdge: (u: NodeId, v: NodeId, weight?: number | undefined) => any;
1919
removeEdge: (u: NodeId, v: NodeId) => any;
20+
hasEdge: (u: NodeId, v: NodeId) => boolean;
2021
setEdgeWeight: (u: NodeId, v: NodeId, weight: EdgeWeight) => any;
2122
getEdgeWeight: (u: NodeId, v: NodeId) => EdgeWeight;
2223
indegree: (node: NodeId) => number;

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function Graph(serialized) {
3333
adjacent: adjacent,
3434
addEdge: addEdge,
3535
removeEdge: removeEdge,
36+
hasEdge: hasEdge,
3637
setEdgeWeight: setEdgeWeight,
3738
getEdgeWeight: getEdgeWeight,
3839
indegree: indegree,
@@ -134,6 +135,18 @@ function Graph(serialized) {
134135
}
135136
return graph;
136137
}
138+
// Returns true if there is an edge from node u to node v.
139+
function hasEdge(u, v) {
140+
var has = false;
141+
if (edges[u]) {
142+
adjacent(u).forEach(function (_v) {
143+
if (_v === v) {
144+
has = true;
145+
}
146+
});
147+
}
148+
return has;
149+
}
137150
// Computes the indegree for the given node.
138151
// Not very efficient, costs O(E) where E = number of edges.
139152
function indegree(node) {

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Graph(serialized?: Serialized) {
2424
adjacent,
2525
addEdge,
2626
removeEdge,
27+
hasEdge,
2728
setEdgeWeight,
2829
getEdgeWeight,
2930
indegree,
@@ -143,6 +144,19 @@ function Graph(serialized?: Serialized) {
143144
return graph;
144145
}
145146

147+
// Returns true if there is an edge from node u to node v.
148+
function hasEdge(u: NodeId, v: NodeId) {
149+
let has = false;
150+
if (edges[u]) {
151+
adjacent(u).forEach(function(_v) {
152+
if(_v === v){
153+
has = true;
154+
}
155+
});
156+
}
157+
return has;
158+
}
159+
146160
// Computes the indegree for the given node.
147161
// Not very efficient, costs O(E) where E = number of edges.
148162
function indegree(node: NodeId) {

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ describe("Graph", function() {
446446
assert.deepEqual(graph.shortestPath("a", "c"), withWeight(["a", "b", "c"], 2));
447447
});
448448
});
449+
450+
describe("hadEdge", function (){
451+
it("Should compute hasEdge.", function (){
452+
var graph = Graph().addEdge("a", "b");
453+
assert.equal(graph.hasEdge("a", "b"), true);
454+
assert.equal(graph.hasEdge("b", "a"), false);
455+
assert.equal(graph.hasEdge("c", "a"), false);
456+
});
457+
})
449458
});
450459

451460
function contains(arr, item){

0 commit comments

Comments
 (0)