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.

45 lines
1.4 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. D_matrix[edge.fromId][edge.toId] = 1;
  22. D_matrix[edge.toId][edge.fromId] = 1;
  23. }
  24. let nodeCount = nodesArray.length;
  25. // Adapted FloydWarshall based on unidirectionality to greatly reduce complexity.
  26. for (let k = 0; k < nodeCount; k++) {
  27. for (let i = 0; i < nodeCount-1; i++) {
  28. for (let j = i+1; j < nodeCount; j++) {
  29. 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]])
  30. D_matrix[nodesArray[j]][nodesArray[i]] = D_matrix[nodesArray[i]][nodesArray[j]];
  31. }
  32. }
  33. }
  34. return D_matrix;
  35. }
  36. }
  37. export default FloydWarshall;