Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ The last argument *weight* (optional) specifies the weight of this edge.

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.

<a name="has-edge" href="#has-edge">#</a> <i>graph</i>.<b>hasEdge</b>(<i>u</i>, <i>v</i>)

Returns `true` if there exists an edge from node *u* to node *v*. Returns `false` otherwise.

### Working with Edge Weights

<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>)
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare function Graph(serialized?: Serialized): {
adjacent: (node: NodeId) => NodeId[];
addEdge: (u: NodeId, v: NodeId, weight?: number | undefined) => any;
removeEdge: (u: NodeId, v: NodeId) => any;
hasEdge: (u: NodeId, v: NodeId) => boolean;
setEdgeWeight: (u: NodeId, v: NodeId, weight: EdgeWeight) => any;
getEdgeWeight: (u: NodeId, v: NodeId) => EdgeWeight;
indegree: (node: NodeId) => number;
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Graph(serialized) {
adjacent: adjacent,
addEdge: addEdge,
removeEdge: removeEdge,
hasEdge: hasEdge,
setEdgeWeight: setEdgeWeight,
getEdgeWeight: getEdgeWeight,
indegree: indegree,
Expand Down Expand Up @@ -134,6 +135,18 @@ function Graph(serialized) {
}
return graph;
}
// Returns true if there is an edge from node u to node v.
function hasEdge(u, v) {
var has = false;
if (edges[u]) {
adjacent(u).forEach(function (_v) {
if (_v === v) {
has = true;
}
});
}
return has;
}
// Computes the indegree for the given node.
// Not very efficient, costs O(E) where E = number of edges.
function indegree(node) {
Expand Down
14 changes: 14 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function Graph(serialized?: Serialized) {
adjacent,
addEdge,
removeEdge,
hasEdge,
setEdgeWeight,
getEdgeWeight,
indegree,
Expand Down Expand Up @@ -143,6 +144,19 @@ function Graph(serialized?: Serialized) {
return graph;
}

// Returns true if there is an edge from node u to node v.
function hasEdge(u: NodeId, v: NodeId) {
let has = false;
if (edges[u]) {
adjacent(u).forEach(function(_v) {
if(_v === v){
has = true;
}
});
}
return has;
}

// Computes the indegree for the given node.
// Not very efficient, costs O(E) where E = number of edges.
function indegree(node: NodeId) {
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ describe("Graph", function() {
assert.deepEqual(graph.shortestPath("a", "c"), withWeight(["a", "b", "c"], 2));
});
});

describe("hadEdge", function (){
it("Should compute hasEdge.", function (){
var graph = Graph().addEdge("a", "b");
assert.equal(graph.hasEdge("a", "b"), true);
assert.equal(graph.hasEdge("b", "a"), false);
assert.equal(graph.hasEdge("c", "a"), false);
});
})
});

function contains(arr, item){
Expand Down