Skip to content

Commit

Permalink
Add vertexExists() and edgeExists() with tests and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Oct 22, 2015
1 parent 11b6b31 commit 91db0c2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var graph = new Graph({
#### `graph.digraph`
Indicates if the graph's edges are directed. Should be considered read-only.

#### `graph.vertexExists(v)`
Returns `true` if a vertex with id `v` exists in the graph, and `false` otherwise.

#### `graph.getVertex(v)`
Returns vertex `v` in an object of the following format,
- `id` - the vertex's id.
Expand Down Expand Up @@ -74,6 +77,9 @@ Removes vertex with id `v` from the graph, including any incident edges.
#### `graph.removeVertices(vertexIdsOrLabel)`
Removes a collection of vertices and their incident edges from the graph. If `vertexIdsOrLabel` is an array of vertex ids, those vertices will be removed. If `vertexIdsOrLabel` is a label, all vertices with that label will be removed.

#### `graph.edgeExists(u, v)`
Returns `true` if an edge from `u` to `v` exists in the graph, and `false` otherwise. This method also accepts a single array argument containing the edge-pair (e.g. `[u, v]`).

#### `graph.getEdge(u, v)`
Returns the edge from vertex `u` to vertex `v` in an object of the following format,
- `pair` - an array of two vertex ids representing the edge-pair.
Expand Down
18 changes: 18 additions & 0 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ exports = module.exports = internals.Graph = function (definition) {

};

internals.Graph.prototype.vertexExists = function (v) {

return !!this._vertices[v];
};

internals.Graph.prototype.getVertex = function (v) {

var vertex = this._vertices[v] || null;
Expand Down Expand Up @@ -185,6 +190,19 @@ internals.Graph.prototype.removeVertices = function (vertices /* or a label */)
return this;
};

internals.Graph.prototype.edgeExists = function (u, v) {

if (Array.isArray(u)) {

Hoek.assert(u.length === 2, 'Not a proper edge pair.');

v = u[1];
u = u[0];
}

return !!this._lookupEdge(u, v);
};

internals.Graph.prototype.getEdge = function (u, v) {

if (Array.isArray(u)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports = module.exports = internals.Traversal = function (graph) {

internals.Traversal.prototype.hop = function (v) {

Hoek.assert(this.graph.getVertex(v), 'Vertex [', v, '] does not exist.');
Hoek.assert(this.graph.vertexExists(v), 'Vertex [', v, '] does not exist.');

this._records.push({ act: 'hop', args: [v] });
this._visit(v);
Expand Down Expand Up @@ -51,7 +51,7 @@ internals.Traversal.prototype.currentVertex = function () {

internals.Traversal.prototype.visits = function (v) {

Hoek.assert(this.graph.getVertex(v), 'Vertex [', v, '] does not exist.');
Hoek.assert(this.graph.vertexExists(v), 'Vertex [', v, '] does not exist.');

return (this._visited[v] || 0);
};
Expand Down
49 changes: 49 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,19 @@ describe('Gert', function () {
done();
});

it('vertexExists(v) indicates whether a vertex exists in a graph.', function (done) {

var graph = new Graph({ vertices: [1, 'a', 2.2] });

expect(graph.vertexExists(1)).to.equal(true);
expect(graph.vertexExists('a')).to.equal(true);
expect(graph.vertexExists(2.2)).to.equal(true);

expect(graph.vertexExists('non')).to.equal(false);

done();
});

it('getVertex(v) returns empty and non-empty vertex information.', function (done) {

var data = {
Expand Down Expand Up @@ -975,6 +988,42 @@ describe('Gert', function () {
done();
});

it('edgeExists(u, v) and edgeExists([u, v]) indicate whether an edge exists in a graph.', function (done) {

var digraph = new Graph({
digraph: true,
edges: [
['a', 'b'],
['b', 'a'],
['b', 'c'],
['b', 'b']
]
});

var nondigraph = new Graph({
digraph: false,
edges: [
['a', 'b'],
['b', 'b']
]
});

expect(digraph.edgeExists('a', 'b')).to.equal(true);
expect(digraph.edgeExists('b', 'a')).to.equal(true);
expect(digraph.edgeExists('b', 'c')).to.equal(true);
expect(digraph.edgeExists('b', 'b')).to.equal(true);
expect(digraph.edgeExists('c', 'b')).to.equal(false);

expect(digraph.edgeExists(['a', 'b'])).to.equal(true);
expect(digraph.edgeExists(['c', 'b'])).to.equal(false);

expect(nondigraph.edgeExists('a', 'b')).to.equal(true);
expect(nondigraph.edgeExists('b', 'a')).to.equal(true);
expect(nondigraph.edgeExists('b', 'b')).to.equal(true);

done();
});

it('getEdge(u, v) and getEdge([u, v]) returns the specified edge or null if no match.', function (done) {

var graph = new Graph({
Expand Down

0 comments on commit 91db0c2

Please sign in to comment.