vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.9 KiB

  1. /**
  2. * @class FloydWarshall
  3. */
  4. class FloydWarshall {
  5. /**
  6. * @constructor FloydWarshall
  7. */
  8. constructor() {
  9. }
  10. /**
  11. *
  12. * @param {Object} body
  13. * @param {Array<Node>} nodesArray
  14. * @param {Array<Edge>} edgesArray
  15. * @returns {{}}
  16. * @static
  17. */
  18. getDistances(body, nodesArray, edgesArray) {
  19. let D_matrix = {};
  20. let edges = body.edges;
  21. // prepare matrix with large numbers
  22. for (let i = 0; i < nodesArray.length; i++) {
  23. let node = nodesArray[i];
  24. let cell = {};
  25. D_matrix[node] = cell;
  26. for (let j = 0; j < nodesArray.length; j++) {
  27. cell[nodesArray[j]] = (i == j ? 0 : 1e9);
  28. }
  29. }
  30. // put the weights for the edges in. This assumes unidirectionality.
  31. for (let i = 0; i < edgesArray.length; i++) {
  32. let edge = edges[edgesArray[i]];
  33. // edge has to be connected if it counts to the distances. If it is connected to inner clusters it will crash so we also check if it is in the D_matrix
  34. if (edge.connected === true && D_matrix[edge.fromId] !== undefined && D_matrix[edge.toId] !== undefined) {
  35. D_matrix[edge.fromId][edge.toId] = 1;
  36. D_matrix[edge.toId][edge.fromId] = 1;
  37. }
  38. }
  39. let nodeCount = nodesArray.length;
  40. // Adapted FloydWarshall based on unidirectionality to greatly reduce complexity.
  41. for (let k = 0; k < nodeCount; k++) {
  42. let knode = nodesArray[k];
  43. let kcolm = D_matrix[knode];
  44. for (let i = 0; i < nodeCount - 1; i++) {
  45. let inode = nodesArray[i];
  46. let icolm = D_matrix[inode];
  47. for (let j = i + 1; j < nodeCount; j++) {
  48. let jnode = nodesArray[j];
  49. let jcolm = D_matrix[jnode];
  50. let val = Math.min(icolm[jnode], icolm[knode] + kcolm[jnode]);
  51. icolm[jnode] = val;
  52. jcolm[inode] = val;
  53. }
  54. }
  55. }
  56. return D_matrix;
  57. }
  58. }
  59. export default FloydWarshall;