2.0.2 • Published 6 years ago

toposort v2.0.2

Weekly downloads
4,120,604
License
MIT
Repository
github
Last release
6 years ago

Toposort

Sort directed acyclic graphs

Build Status

Installation

npm install toposort or component install marcelklehr/toposort

then in your code:

toposort = require('toposort')

Usage

We want to sort the following graph.

graph

// First, we define our edges.
var graph = [
  ['put on your shoes', 'tie your shoes']
, ['put on your shirt', 'put on your jacket']
, ['put on your shorts', 'put on your jacket']
, ['put on your shorts', 'put on your shoes']
]


// Now, sort the vertices topologically, to reveal a legal execution order.
toposort(graph)
// [ 'put on your shirt'
// , 'put on your shorts'
// , 'put on your jacket'
// , 'put on your shoes'
// , 'tie your shoes' ]

(Note that there is no defined order for graph parts that are not connected -- you could also put on your jacket after having tied your shoes...)

Sorting dependencies

It is usually more convenient to specify dependencies instead of "sequences".

// This time, edges represent dependencies.
var graph = [
  ['tie your shoes', 'put on your shoes']
, ['put on your jacket', 'put on your shirt']
, ['put on your shoes', 'put on your shorts']
, ['put on your jacket', 'put on your shorts']
]

toposort(graph) 
// [ 'tie your shoes'
// , 'put on your shoes'
// , 'put on your jacket'
// , 'put on your shirt'
// , 'put on your shorts' ]

// Now, reversing the list will reveal a legal execution order.
toposort(graph).reverse() 
// [ 'put on your shorts'
// , 'put on your shirt'
// , 'put on your jacket'
// , 'put on your shoes'
// , 'tie your shoes' ]

API

toposort(edges)

  • 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).

Returns: {Array} a list of vertices, sorted from "start" to "end"

Throws an error if there are any cycles in the graph.

toposort.array(nodes, edges)

  • nodes {Array} An array of nodes
  • edges {Array} An array of directed edges. You don't need to mention all nodes here.

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.

Returns: {Array} a list of vertices, sorted from "start" to "end"

Throws an error if there are any cycles in the graph.

Tests

Run the tests with node test.js.

Legal

MIT License

yupcuc-gaf@microsoft/teamsfx-corearchetype-libraryattunevuedragdropuploadimagesmio-cli-service@amra-finance/ding@guomio/cli-servicebb-chatsage-yupswagger-parameter-extactor@dxos/theme-docsneilsonz-msync@l1nyanm1ng/react-picture-viewer@fredrikvang/fv-initcthpb-plugin-socialsid-npm-test3sid-npmtest-1sid-npmtest-2sid-test-create-npm@leni-msft/oavarthcoin.js@volley/solid-localizationmodulux@infinitebrahmanuniverse/nolb-topfusion-core@everything-registry/sub-chunk-2961@essential-projects/data_model@essential-projects/graphqldeseruntpariaturreact-misc-toolboxsimple-html-webpack-pluginsfdhanautohintshanshrewshipp-servershyftshyft-storage-connectordulcet-html-webpack-pluginsingularsina-html-webpack-plugindx-scannersnap-toolstimpembark-framework-nosimsr-html-webpack-plugin-for-multihtmlenfielddickerdesignfundot-versiondigiassets-full-node@digiforce-cloud/xflow-hook@d-fischer/documen.ts@darrinholst/mocha-webpack@data-wrangling-components/corerandom-object-languagerecipejsrx-streaming-viewssaitosalesleg-api2schema-modulesrefgenreyhan-projectrete-kitrheactor-yadda-feature-runnerember-css-modulesspusteasyplayer-myeasy-abi-geneasy-html-webpack-pluginsolregex2sort-pkgssqlpatch@darkobits/marin@datawheel/bespokecsqgethtml2019dbcpdigest-brunchdockestbx-wxapp-webpack-plugin@axah/polyfill-librarycarmipack-workspacecdk-dependency-graph@bifravst/e2e-bdd-test-runnerparcelifyparcelify2@calibur/skr-clipineapple-alphapinelang@brn/jspm-loader-css@cautionyourblast/polyfill-librarypkg-graphpkg-taskspolyfill-library@cfn/graphcoloredcoins-full-node@clarigen/cliplasma-coreplug-and-play
2.0.2

6 years ago

2.0.1

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

8 years ago

0.2.12

9 years ago

0.2.11

9 years ago

0.2.10

11 years ago

0.2.9

11 years ago

0.2.8

11 years ago

0.2.7

11 years ago

0.2.5

11 years ago

0.2.4

11 years ago

0.2.3

11 years ago

0.2.2

11 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.0

11 years ago