e63543e7de08c75d35595c7bd472a3844c74f6fb30533aca5ce04139e1685023fbc277b7cd69f3d73fd834a0bf9737b5f0fafcd407922b62258d12c5d43395 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Toposort
  2. Sort directed acyclic graphs
  3. [![Build Status](https://travis-ci.org/marcelklehr/toposort.png)](https://travis-ci.org/marcelklehr/toposort)
  4. ## Installation
  5. `npm install toposort` or `component install marcelklehr/toposort`
  6. then in your code:
  7. ```js
  8. toposort = require('toposort')
  9. ```
  10. ## Usage
  11. We want to sort the following graph.
  12. ![graph](https://cdn.rawgit.com/marcelklehr/toposort/8b14e9fd/graph.svg)
  13. ```js
  14. // First, we define our edges.
  15. var graph = [
  16. ['put on your shoes', 'tie your shoes']
  17. , ['put on your shirt', 'put on your jacket']
  18. , ['put on your shorts', 'put on your jacket']
  19. , ['put on your shorts', 'put on your shoes']
  20. ]
  21. // Now, sort the vertices topologically, to reveal a legal execution order.
  22. toposort(graph)
  23. // [ 'put on your shirt'
  24. // , 'put on your shorts'
  25. // , 'put on your jacket'
  26. // , 'put on your shoes'
  27. // , 'tie your shoes' ]
  28. ```
  29. (Note that there is no defined order for graph parts that are not connected
  30. -- you could also put on your jacket after having tied your shoes...)
  31. ### Sorting dependencies
  32. It is usually more convenient to specify *dependencies* instead of "sequences".
  33. ```js
  34. // This time, edges represent dependencies.
  35. var graph = [
  36. ['tie your shoes', 'put on your shoes']
  37. , ['put on your jacket', 'put on your shirt']
  38. , ['put on your shoes', 'put on your shorts']
  39. , ['put on your jacket', 'put on your shorts']
  40. ]
  41. toposort(graph)
  42. // [ 'tie your shoes'
  43. // , 'put on your shoes'
  44. // , 'put on your jacket'
  45. // , 'put on your shirt'
  46. // , 'put on your shorts' ]
  47. // Now, reversing the list will reveal a legal execution order.
  48. toposort(graph).reverse()
  49. // [ 'put on your shorts'
  50. // , 'put on your shirt'
  51. // , 'put on your jacket'
  52. // , 'put on your shoes'
  53. // , 'tie your shoes' ]
  54. ```
  55. ## API
  56. ### toposort(edges)
  57. + edges {Array} An array of directed edges describing a graph. An edge looks like this: `[node1, node2]` (vertices needn't be strings but can be of any type).
  58. Returns: {Array} a list of vertices, sorted from "start" to "end"
  59. Throws an error if there are any cycles in the graph.
  60. ### toposort.array(nodes, edges)
  61. + nodes {Array} An array of nodes
  62. + edges {Array} An array of directed edges. You don't need to mention all `nodes` here.
  63. This is a convenience method that allows you to define nodes that may or may not be connected to any other nodes. The ordering of unconnected nodes is not defined.
  64. Returns: {Array} a list of vertices, sorted from "start" to "end"
  65. Throws an error if there are any cycles in the graph.
  66. ## Tests
  67. Run the tests with `node test.js`.
  68. ## Legal
  69. MIT License