1
0
Fork 0

Added two measurements to graphs

* diameter
* radius
This commit is contained in:
Lucas Dohmen 2012-06-26 15:25:26 +02:00
parent ecf09fc4dc
commit 07366afcfc
2 changed files with 72 additions and 2 deletions

View File

@ -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);
};
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -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");
});
}
};
}