From 07366afcfc68968a1f5dfd55f21426b51efe5aa8 Mon Sep 17 00:00:00 2001 From: Lucas Dohmen Date: Tue, 26 Jun 2012 15:25:26 +0200 Subject: [PATCH] Added two measurements to graphs * diameter * radius --- js/common/modules/graph.js | 41 ++++++++++++++++++++++ js/common/tests/shell-graph-measurement.js | 33 +++++++++++++++-- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/js/common/modules/graph.js b/js/common/modules/graph.js index c54f884034..d4faed0eff 100644 --- a/js/common/modules/graph.js +++ b/js/common/modules/graph.js @@ -1493,6 +1493,47 @@ Graph.prototype.size = function () { return this._edges.count(); }; +//////////////////////////////////////////////////////////////////////////////// +/// @brief calculate a measurement +/// +/// @FUN{@FA{vertex}.measurement(@FA{measurement})} +/// +/// Calculates the eccentricity or closeness of the vertex +/// +//////////////////////////////////////////////////////////////////////////////// + +Graph.prototype.measurement = function (measurement) { + var graph = this, + vertices = graph._vertices.toArray(), + start_value; + + switch (measurement) { + case "diameter": + start_value = 0; + break; + case "radius": + start_value = Infinity; + break; + default: + throw "Unknown Measurement '" + measurement + "'"; + } + + return vertices.reduce(function (calculated, vertex) { + vertex = graph.getVertex(vertex._id); + + switch (measurement) { + case "diameter": + calculated = Math.max(calculated, vertex.measurement("eccentricity")); + break; + case "radius": + calculated = Math.min(calculated, vertex.measurement("eccentricity")); + break; + } + + return calculated; + }, start_value); +}; + //////////////////////////////////////////////////////////////////////////////// /// @} //////////////////////////////////////////////////////////////////////////////// diff --git a/js/common/tests/shell-graph-measurement.js b/js/common/tests/shell-graph-measurement.js index a2c302b018..529d1659aa 100644 --- a/js/common/tests/shell-graph-measurement.js +++ b/js/common/tests/shell-graph-measurement.js @@ -179,14 +179,43 @@ function measurementSuite() { /// @brief test to get an unknown measurement for a vertex //////////////////////////////////////////////////////////////////////////////// - testUnknownMeasurement : function () { + testUnknownMeasurementOnVertex : function () { var v1 = graph.addVertex(1); assertException(function () { v1.measurement("unknown"); }); - } + }, +//////////////////////////////////////////////////////////////////////////////// +/// @brief test to get diameter and radius for a graph +//////////////////////////////////////////////////////////////////////////////// + + testDiameterAndRadius : function () { + var v1 = graph.addVertex(1), + v2 = graph.addVertex(2), + v3 = graph.addVertex(3), + v4 = graph.addVertex(4), + v5 = graph.addVertex(5); + + graph.addEdge(v1, v2); + graph.addEdge(v1, v3); + graph.addEdge(v1, v4); + graph.addEdge(v4, v5); + + assertEqual(graph.measurement("diameter"), 3); + assertEqual(graph.measurement("radius"), 2); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test to get an unknown measurement for a graph +//////////////////////////////////////////////////////////////////////////////// + + testUnknownMeasurementOnGraph : function () { + assertException(function () { + graph.measurement("unknown"); + }); + } }; }