1
0
Fork 0

Added Degrees to Vertices, Size and Order to Graphs

This commit is contained in:
Lucas Dohmen 2012-06-26 12:51:26 +02:00
parent ac092872ee
commit c6b2b2642c
2 changed files with 197 additions and 0 deletions

View File

@ -879,6 +879,45 @@ Vertex.prototype._getShortestDistance = function (todo_list, distances) {
return vertex;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief number of edges
///
/// @FUN{@FA{vertex}.degree()}
///
/// Returns the number of edges of the @FA{vertex}.
///
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.degree = function () {
return this._graph._edges.edges(this._id).length;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief number of in-edges
///
/// @FUN{@FA{vertex}.inDegree()}
///
/// Returns the number of inbound edges of the @FA{vertex}.
///
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.inDegree = function () {
return this._graph._edges.inEdges(this._id).length;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief number of out-edges
///
/// @FUN{@FA{vertex}.outDegree()}
///
/// Returns the number of outbound edges of the @FA{vertex}.
///
////////////////////////////////////////////////////////////////////////////////
Vertex.prototype.outDegree = function () {
return this._graph._edges.outEdges(this._id).length;
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
@ -1376,6 +1415,28 @@ Graph.prototype.removeEdge = function (edge) {
}
};
////////////////////////////////////////////////////////////////////////////////
/// @brief number of vertices
///
/// @FUN{@FA{graph}.order()}
///
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.order = function () {
return this._vertices.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @brief number of edges
///
/// @FUN{@FA{graph}.size()}
///
////////////////////////////////////////////////////////////////////////////////
Graph.prototype.size = function () {
return this._edges.count();
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,136 @@
/*jslint indent: 2,
nomen: true,
maxlen: 80,
sloppy: true */
/*global require,
db,
assertEqual, assertTrue,
print,
PRINT_OBJECT,
AvocadoCollection, AvocadoEdgesCollection,
processCsvFile */
////////////////////////////////////////////////////////////////////////////////
/// @brief test the graph class
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2012 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler, Lucas Dohmen
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var jsunity = require("jsunity"),
Helper = require("test-helper").Helper,
console = require("console");
// -----------------------------------------------------------------------------
// --SECTION-- collection methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief test suite: Get Neighbor Function
////////////////////////////////////////////////////////////////////////////////
function measurementSuite() {
var Graph = require("graph").Graph,
graph_name = "UnitTestsCollectionGraph",
vertex = "UnitTestsCollectionVertex",
edge = "UnitTestsCollectionEdge",
graph = null;
return {
////////////////////////////////////////////////////////////////////////////////
/// @brief set up
////////////////////////////////////////////////////////////////////////////////
setUp : function () {
try {
try {
// Drop the graph if it exsits
graph = new Graph(graph_name);
print("FOUND: ");
PRINT_OBJECT(graph);
graph.drop();
} catch (err1) {
}
graph = new Graph(graph_name, vertex, edge);
} catch (err2) {
console.error("[FAILED] setup failed:" + err2);
}
},
////////////////////////////////////////////////////////////////////////////////
/// @brief tear down
////////////////////////////////////////////////////////////////////////////////
tearDown : function () {
try {
if (graph !== null) {
graph.drop();
}
} catch (err) {
console.error("[FAILED] tear-down failed:" + err);
}
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test the get total, in and out degree of a vertex
////////////////////////////////////////////////////////////////////////////////
testGetDegrees : function () {
var v1 = graph.addVertex(1),
v2 = graph.addVertex(2),
v3 = graph.addVertex(3);
graph.addEdge(v1, v2);
graph.addEdge(v3, v1);
assertEqual(v1.degree(), 2);
assertEqual(v1.inDegree(), 1);
assertEqual(v1.outDegree(), 1);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test the get total, in and out degree of a vertex
////////////////////////////////////////////////////////////////////////////////
testGetOrderAndSize : function () {
var v1 = graph.addVertex(1),
v2 = graph.addVertex(2),
v3 = graph.addVertex(3);
graph.addEdge(v1, v2);
graph.addEdge(v3, v1);
assertEqual(graph.order(), 3);
assertEqual(graph.size(), 2);
}
};
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes the test suites
////////////////////////////////////////////////////////////////////////////////
jsunity.run(measurementSuite);
return jsunity.done();