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.

57 lines
1.7 KiB

  1. /**
  2. * Created by Alex on 10-Aug-15.
  3. */
  4. class FloydWarshall {
  5. constructor() {
  6. }
  7. getDistances(body, nodesArray, edgesArray) {
  8. let D_matrix = {};
  9. let edges = body.edges;
  10. // prepare matrix with large numbers
  11. for (let i = 0; i < nodesArray.length; i++) {
  12. let node = nodesArray[i];
  13. let cell = {};
  14. D_matrix[node] = cell;
  15. for (let j = 0; j < nodesArray.length; j++) {
  16. cell[nodesArray[j]] = (i == j ? 0 : 1e9);
  17. }
  18. }
  19. // put the weights for the edges in. This assumes unidirectionality.
  20. for (let i = 0; i < edgesArray.length; i++) {
  21. let edge = edges[edgesArray[i]];
  22. // 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
  23. if (edge.connected === true && D_matrix[edge.fromId] !== undefined && D_matrix[edge.toId] !== undefined) {
  24. D_matrix[edge.fromId][edge.toId] = 1;
  25. D_matrix[edge.toId][edge.fromId] = 1;
  26. }
  27. }
  28. let nodeCount = nodesArray.length;
  29. // Adapted FloydWarshall based on unidirectionality to greatly reduce complexity.
  30. for (let k = 0; k < nodeCount; k++) {
  31. let knode = nodesArray[k];
  32. let kcolm = D_matrix[knode];
  33. for (let i = 0; i < nodeCount - 1; i++) {
  34. let inode = nodesArray[i];
  35. let icolm = D_matrix[inode];
  36. for (let j = i + 1; j < nodeCount; j++) {
  37. let jnode = nodesArray[j];
  38. let jcolm = D_matrix[jnode];
  39. let val = Math.min(icolm[jnode], icolm[knode] + kcolm[jnode]);
  40. icolm[jnode] = val;
  41. jcolm[inode] = val;
  42. }
  43. }
  44. }
  45. return D_matrix;
  46. }
  47. }
  48. export default FloydWarshall;