• Stars
    star
    296
  • Rank 140,464 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

changesets

Changeset library with operational transformation -- for node and the browser!
JavaScript
281
star
2

smokesignal

Build your own small (or larger) peer to peer network with node.js
JavaScript
149
star
3

vdom-virtualize

Virtualize a DOM node
JavaScript
131
star
4

tivoka

JSON-RPC client/server library for PHP (supports v1.0 and v2.0 specs)
PHP
74
star
5

dom-ot

Transform DOM tree patches against each other (operational transformation)
JavaScript
51
star
6

buzzmap

draw and edit mindmaps interactively, using force-directed layouts (jQuery plugin)
JavaScript
22
star
7

prism.io

Share and edit documents in real-time. Superseded by http://github.com/marcelklehr/gulf
JavaScript
18
star
8

chordreader

Search for, display, transpose and save chords on your phone, that you get from the interwebs. 🎢
Java
13
star
9

beardless

Beardless templating for node.js. Start shaving!
JavaScript
12
star
10

warp

Proof of concept dom-ot editor -- superseded by https://github.com/gulf and https://github.com/hivejs
JavaScript
9
star
11

r-string

Commutative conflic-free replicated String (CRDT/Scuttlebutt)
JavaScript
7
star
12

magnet

An experiment: A duck-typed, object-oriented, syntax-driven (and potentially awesome) programming language.
JavaScript
6
star
13

zoocache

Output cache that neatly integrates into your PHP application.
PHP
6
star
14

p2p-rpc-stream

rpc streams for node. done right (TM)
JavaScript
5
star
15

vdom-serialize

JavaScript
5
star
16

y-connector-dat

A dat transport for y.js
JavaScript
4
star
17

PeerPad

peer-to-peer collaborative editing
JavaScript
3
star
18

ep_push2delete

delete your etherpad with one click.
JavaScript
3
star
19

minetest-conveyor

Conveyor mod for minetest, the awesome voxel mining game.
Lua
3
star
20

ep_infopanel

An "about" section for Etherpad-lite. Donuts do usually help.
JavaScript
3
star
21

waterline-to-jsonapi

Convert your waterline query results to jsonapi compliant responses.
JavaScript
3
star
22

intervarl

A variable interval. The timeout period can be changed dynamically.
JavaScript
2
star
23

ot-socialcalc

Operational transformation for socialcalc commands (shareJS compatible)
JavaScript
2
star
24

observ-emitter

an observable atomic emitter
JavaScript
2
star
25

browser-stream

Node.js streams in your browser
JavaScript
2
star
26

mutation-summary

Makes observing the DOM fast and easy
JavaScript
2
star
27

deep-reinforcement-learning-2022

Course work for the Deep Reinforcement Learning Course in Spring 2022 at University OsnabrΓΌck
Python
2
star
28

umbilical

Bidirectional rpc over tcp for node.js
JavaScript
1
star
29

magpie-typicality-mt

HTML
1
star
30

domnode-at-path

Supply a path and a root node and receive the domnode at that path.
JavaScript
1
star
31

ep_timeslider

The timeslider of etherpad lite, extracted and squashed into a plugin...
JavaScript
1
star
32

lseq-between

Sort sequences without re-indexing on insert ✨
JavaScript
1
star
33

ep_ether-o-meter

Display metrics. For etherpad. In real-time. With swag.
HTML
1
star
34

obj-to-argv

Takes an object and spits out the argv/args array as libraries like optimist would expect it.
JavaScript
1
star
35

theatre

my own, chocolate-flavoured lisp dialect
JavaScript
1
star
36

path-to-domnode

JavaScript
1
star
37

nextcloud-apps-ranking

Nextcloud apps listed by popularity
JavaScript
1
star
38

wutsdis

πŸ§™ Auto-tag your photo collection using an ImageNet object detection model.
Python
1
star
39

shoe2

Binary-safe sockJS streams with all necessary events
JavaScript
1
star
40

atomic-emitter

Emitters as values that you can pass around and assign, optionally with private event writing access.
JavaScript
1
star