From 518451fbb747bf8a080ce407dd82747250fa376d Mon Sep 17 00:00:00 2001 From: Curran Date: Thu, 20 May 2021 10:46:44 -0400 Subject: [PATCH] Add hasEdge method --- README.md | 4 ++++ index.d.ts | 1 + index.js | 13 +++++++++++++ index.ts | 14 ++++++++++++++ test.js | 9 +++++++++ 5 files changed, 41 insertions(+) diff --git a/README.md b/README.md index 3fb45f5..a0f9286 100644 --- a/README.md +++ b/README.md @@ -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. +# graph.hasEdge(u, v) + +Returns `true` if there exists an edge from node *u* to node *v*. Returns `false` otherwise. + ### Working with Edge Weights # graph.setEdgeWeight(u, v, weight) diff --git a/index.d.ts b/index.d.ts index 3d63e4e..583391b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; diff --git a/index.js b/index.js index d2fc91d..898829f 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ function Graph(serialized) { adjacent: adjacent, addEdge: addEdge, removeEdge: removeEdge, + hasEdge: hasEdge, setEdgeWeight: setEdgeWeight, getEdgeWeight: getEdgeWeight, indegree: indegree, @@ -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) { diff --git a/index.ts b/index.ts index cc05b13..6db5826 100644 --- a/index.ts +++ b/index.ts @@ -24,6 +24,7 @@ function Graph(serialized?: Serialized) { adjacent, addEdge, removeEdge, + hasEdge, setEdgeWeight, getEdgeWeight, indegree, @@ -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) { diff --git a/test.js b/test.js index 0075b7d..eace124 100644 --- a/test.js +++ b/test.js @@ -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){