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.

47 lines
1.5 KiB

  1. /**
  2. * Created by Alex on 10-Aug-15.
  3. */
  4. class FloydWarshall {
  5. constructor(){}
  6. getDistances(body, nodesArray, edgesArray) {
  7. let D_matrix = {};
  8. let edges = body.edges;
  9. // prepare matrix with large numbers
  10. for (let i = 0; i < nodesArray.length; i++) {
  11. D_matrix[nodesArray[i]] = {};
  12. D_matrix[nodesArray[i]] = {};
  13. for (let j = 0; j < nodesArray.length; j++) {
  14. D_matrix[nodesArray[i]][nodesArray[j]] = (i == j ? 0 : 1e9);
  15. D_matrix[nodesArray[i]][nodesArray[j]] = (i == j ? 0 : 1e9);
  16. }
  17. }
  18. // put the weights for the edges in. This assumes unidirectionality.
  19. for (let i = 0; i < edgesArray.length; i++) {
  20. let edge = edges[edgesArray[i]];
  21. if (edge.connected === true) { // edge has to be connected if it counts to the distances.
  22. D_matrix[edge.fromId][edge.toId] = 1;
  23. D_matrix[edge.toId][edge.fromId] = 1;
  24. }
  25. }
  26. let nodeCount = nodesArray.length;
  27. // Adapted FloydWarshall based on unidirectionality to greatly reduce complexity.
  28. for (let k = 0; k < nodeCount; k++) {
  29. for (let i = 0; i < nodeCount-1; i++) {
  30. for (let j = i+1; j < nodeCount; j++) {
  31. D_matrix[nodesArray[i]][nodesArray[j]] = Math.min(D_matrix[nodesArray[i]][nodesArray[j]],D_matrix[nodesArray[i]][nodesArray[k]] + D_matrix[nodesArray[k]][nodesArray[j]])
  32. D_matrix[nodesArray[j]][nodesArray[i]] = D_matrix[nodesArray[i]][nodesArray[j]];
  33. }
  34. }
  35. }
  36. return D_matrix;
  37. }
  38. }
  39. export default FloydWarshall;