Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Topologically sort directed acyclic graphs (such as dependency lists) in javascript

License

NotificationsYou must be signed in to change notification settings

marcelklehr/toposort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sort directed acyclic graphs

Build Status

Installation

npm install toposort orcomponent install marcelklehr/toposort

then in your code:

toposort=require('toposort')

Usage

We want to sort the following graph.

graph

// First, we define our edges.vargraph=[['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 specifydependencies instead of "sequences".

// This time, edges represent dependencies.vargraph=[['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 allnodes 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 withnode test.js.

Legal

MIT License

About

Topologically sort directed acyclic graphs (such as dependency lists) in javascript

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp