1
0
Fork 0

fixed betweeness and bug in floyd warshall

This commit is contained in:
scottashton 2014-09-04 15:22:29 +02:00
parent 2eb42db0d7
commit ff8791f6fb
1 changed files with 38 additions and 23 deletions

View File

@ -5183,7 +5183,7 @@ function CALCULATE_SHORTEST_PATHES_WITH_FLOYD_WARSHALL (graphData, options) {
if (paths[e._to][e._from]) { if (paths[e._to][e._from]) {
paths[e._to][e._from].distance = paths[e._to][e._from].distance =
Math.min(paths[e._to][e._from].distance, DETERMINE_WEIGHT(e, options.weight, Math.min(paths[e._to][e._from].distance, DETERMINE_WEIGHT(e, options.weight,
options.defaultWeight)); options.defaultWeight));
} else { } else {
paths[e._to][e._from] = {distance : DETERMINE_WEIGHT(e, options.weight, paths[e._to][e._from] = {distance : DETERMINE_WEIGHT(e, options.weight,
options.defaultWeight), paths : [{edges : [e], vertices : [e._from, e._to]}]}; options.defaultWeight), paths : [{edges : [e], vertices : [e._from, e._to]}]};
@ -5204,7 +5204,7 @@ function CALCULATE_SHORTEST_PATHES_WITH_FLOYD_WARSHALL (graphData, options) {
return self.indexOf(elem) === pos; return self.indexOf(elem) === pos;
}; };
Object.keys(graph.fromVerticesIDs).forEach(function (v) { Object.keys(graph.fromVerticesIDs).forEach(function (v) {
vertices[v] = 1; vertices[v] = 1;
}); });
@ -5212,20 +5212,20 @@ function CALCULATE_SHORTEST_PATHES_WITH_FLOYD_WARSHALL (graphData, options) {
allVertices.forEach(function (k) { allVertices.forEach(function (k) {
allVertices.forEach(function (i) { allVertices.forEach(function (i) {
allVertices.forEach(function (j) { allVertices.forEach(function (j) {
if (i === j ) { if (i === j ) {
if (!paths[i]) { if (!paths[i]) {
paths[i] = {}; paths[i] = {};
}
paths[i][j] = null;
return;
} }
if (paths[i] && paths[i][k] && paths[i][k].distance >=0 paths[i][j] = null;
&& paths[i][k].distance < Infinity && return;
paths[k] && paths[k][j] && paths[k][j].distance >=0 }
&& paths[k][j].distance < Infinity && if (paths[i] && paths[i][k] && paths[i][k].distance >=0
( !paths[i][j] || && paths[i][k].distance < Infinity &&
paths[i][k].distance + paths[k][j].distance <= paths[i][j].distance paths[k] && paths[k][j] && paths[k][j].distance >=0
) && paths[k][j].distance < Infinity &&
( !paths[i][j] ||
paths[i][k].distance + paths[k][j].distance <= paths[i][j].distance
)
) { ) {
if (!paths[i][j]) { if (!paths[i][j]) {
paths[i][j] = {paths : [], distance : paths[i][k].distance + paths[k][j].distance}; paths[i][j] = {paths : [], distance : paths[i][k].distance + paths[k][j].distance};
@ -5243,8 +5243,6 @@ function CALCULATE_SHORTEST_PATHES_WITH_FLOYD_WARSHALL (graphData, options) {
}); });
}); });
}); });
} else {
delete paths[i][j].paths;
} }
} }
@ -5252,6 +5250,22 @@ function CALCULATE_SHORTEST_PATHES_WITH_FLOYD_WARSHALL (graphData, options) {
}); });
}); });
var transformPath = function (paths) {
paths.forEach(function (p) {
var vTmp = [];
p.vertices.forEach(function (v) {
if (graph.fromVerticesIDs[v]) {
vTmp.push(graph.fromVerticesIDs[v]);
} else {
vTmp.push(graph.toVerticesIDs[v]);
}
});
p.vertices = vTmp;
});
return paths;
};
Object.keys(paths).forEach(function (from) { Object.keys(paths).forEach(function (from) {
if (!graph.fromVerticesIDs[from]) { if (!graph.fromVerticesIDs[from]) {
return; return;
@ -5272,7 +5286,7 @@ function CALCULATE_SHORTEST_PATHES_WITH_FLOYD_WARSHALL (graphData, options) {
result.push({ result.push({
startVertex : from, startVertex : from,
vertex : graph.toVerticesIDs[to], vertex : graph.toVerticesIDs[to],
paths : options.noPaths ? null : paths[from][to].paths, paths : options.noPaths ? null : transformPath(paths[from][to].paths),
distance : paths[from][to].distance distance : paths[from][to].distance
}); });
}); });
@ -5392,7 +5406,8 @@ function IS_EXAMPLE_SET (example) {
return ( return (
example && ( example && (
(Array.isArray(example) && example.length > 0) || (Array.isArray(example) && example.length > 0) ||
(typeof example === "object" && Object.keys(example) > 0) (typeof example === "object" && Object.keys(example) > 0) ||
typeof example === "string"
) )
); );
@ -6902,13 +6917,13 @@ function GENERAL_GRAPH_ABSOLUTE_BETWEENNESS (graphName, options) {
} }
d.paths.forEach(function (p) { d.paths.forEach(function (p) {
p.vertices.forEach(function (v) { p.vertices.forEach(function (v) {
if (v === d.startVertex || v === d.vertex._id) { if (v._id === d.startVertex || v._id === d.vertex._id) {
return; return;
} }
if (!tmp[v]) { if (!tmp[v._id]) {
tmp[v] = 1; tmp[v._id] = 1;
} else { } else {
tmp[v]++; tmp[v._id]++;
} }
}); });
}); });