index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { longestPath } from './util'
  2. import feasibleTree from './feasible-tree'
  3. import networkSimplex from './network-simplex'
  4. /*
  5. * Assigns a rank to each node in the input graph that respects the "minlen"
  6. * constraint specified on edges between nodes.
  7. *
  8. * This basic structure is derived from Gansner, et al., "A Technique for
  9. * Drawing Directed Graphs."
  10. *
  11. * Pre-conditions:
  12. *
  13. * 1. Graph must be a connected DAG
  14. * 2. Graph nodes must be objects
  15. * 3. Graph edges must have "weight" and "minlen" attributes
  16. *
  17. * Post-conditions:
  18. *
  19. * 1. Graph nodes will have a "rank" attribute based on the results of the
  20. * algorithm. Ranks can start at any index (including negative), we'll
  21. * fix them up later.
  22. */
  23. function rank (g) {
  24. switch (g.graph().ranker) {
  25. case 'network-simplex': networkSimplexRanker(g); break
  26. case 'tight-tree': tightTreeRanker(g); break
  27. case 'longest-path': longestPathRanker(g); break
  28. default: networkSimplexRanker(g)
  29. }
  30. }
  31. // A fast and simple ranker, but results are far from optimal.
  32. const longestPathRanker = longestPath
  33. function tightTreeRanker (g) {
  34. longestPath(g)
  35. feasibleTree(g)
  36. }
  37. function networkSimplexRanker (g) {
  38. networkSimplex(g)
  39. }
  40. export default rank