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.3 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. for (let j = 0; j < nodesArray.length; j++) {
  13. D_matrix[nodesArray[i]][nodesArray[j]] = 1e9;
  14. }
  15. }
  16. // put the weights for the edges in. This assumes unidirectionality.
  17. for (let i = 0; i < edgesArray.length; i++) {
  18. let edge = edges[edgesArray[i]];
  19. D_matrix[edge.fromId][edge.toId] = 1;
  20. D_matrix[edge.toId][edge.fromId] = 1;
  21. }
  22. // calculate all pair distances
  23. for (let k = 0; k < nodesArray.length; k++) {
  24. for (let i = 0; i < nodesArray.length; i++) {
  25. for (let j = 0; j < nodesArray.length; j++) {
  26. 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]])
  27. }
  28. }
  29. }
  30. // remove the self references from the matrix
  31. for (let i = 0; i < nodesArray.length; i++) {
  32. delete D_matrix[nodesArray[i]][nodesArray[i]];
  33. }
  34. return D_matrix;
  35. }
  36. }
  37. export default FloydWarshall;