All Projects → dmbaturin → ocaml-tsort

dmbaturin / ocaml-tsort

Licence: MIT License
Easy to use and user-friendly topological sort module for OCaml

Programming Languages

ocaml
1615 projects
shell
77523 projects
Makefile
30231 projects

Projects that are alternatives of or similar to ocaml-tsort

topological-sort
Topological sort implemented in Javascript / Typescript
Stars: ✭ 16 (-23.81%)
Mutual labels:  topological-sort
parallel-dfs-dag
A parallel implementation of DFS for Directed Acyclic Graphs (https://research.nvidia.com/publication/parallel-depth-first-search-directed-acyclic-graphs)
Stars: ✭ 29 (+38.1%)
Mutual labels:  graph
profdump
Processes profiling output of the D compiler
Stars: ✭ 15 (-28.57%)
Mutual labels:  graph
ocaml-bpf
OCaml embedded eBPF assembler
Stars: ✭ 18 (-14.29%)
Mutual labels:  ocaml-library
ux-charts
Simple, responsive, modern Charts with zero dependencies
Stars: ✭ 22 (+4.76%)
Mutual labels:  graph
graph
Generic graph library and algorithms for Racket.
Stars: ✭ 53 (+152.38%)
Mutual labels:  graph
ocamline
👨🏻‍💻 Command line interface for user input
Stars: ✭ 32 (+52.38%)
Mutual labels:  ocaml-library
Subway
Out-of-GPU-Memory Graph Processing with Minimal Data Transfer
Stars: ✭ 30 (+42.86%)
Mutual labels:  graph
agda
The theory of algebraic graphs formalised in Agda
Stars: ✭ 67 (+219.05%)
Mutual labels:  graph
Data-structures
Data Structures in Java
Stars: ✭ 13 (-38.1%)
Mutual labels:  graph
racket-graphviz
Library to enable using graphviz in Racket programs
Stars: ✭ 20 (-4.76%)
Mutual labels:  graph
purescript-d3-tagless-II
Tagless final style interpreter / wrapper for D3 in PureScript, latest of many re-writes
Stars: ✭ 28 (+33.33%)
Mutual labels:  graph
nodify
High performance and modular controls for node-based editors designed for data-binding and MVVM.
Stars: ✭ 282 (+1242.86%)
Mutual labels:  graph
secp256k1-ml
Elliptic curve library secp256k1 wrapper for Ocaml
Stars: ✭ 18 (-14.29%)
Mutual labels:  ocaml-library
tRakt-shiny
Using trakt to graph show data and such. The on-it's-way-out incarnation of trakt.jemu.name
Stars: ✭ 17 (-19.05%)
Mutual labels:  graph
directed graph
Dart implementation of a directed graph. Provides algorithms for sorting vertices, retrieving a topological ordering or detecting cycles.
Stars: ✭ 37 (+76.19%)
Mutual labels:  topological-sort
gun-scape
GunDB Cytoscape Graph Visualizer + Live Editor
Stars: ✭ 49 (+133.33%)
Mutual labels:  graph
graphire
An unopinionated react graph visualization library.
Stars: ✭ 256 (+1119.05%)
Mutual labels:  graph
LGCN
Tensorflow Implementation of Large-Scale Learnable Graph Convolutional Networks (LGCN) KDD18
Stars: ✭ 45 (+114.29%)
Mutual labels:  graph
arrowic
Quick and dirty directed graph viewer for REPL explorations.
Stars: ✭ 15 (-28.57%)
Mutual labels:  graph

ocaml-tsort CircleCI badge

This module provides topological sort based on Kahn's algorithm. It's not very fast, but it's easy to use and provides friendly error reporting.

It works on assoc lists (('a * 'a list) list). The keys are "tasks" and the values are lists of their dependencies.

Sorting DAGs

Most of the time cyclic dependencies are bad. The main function, Tsort.sort returns value of this type:

type 'a sort_result =
  Sorted of 'a list 
| ErrorCycle of 'a list

The function is:

val sort : ('a * 'a list) list -> 'a sort_result

Examples:

# Tsort.sort [("foundation", []); ("walls", ["foundation"]); ("roof", ["walls"])] ;;
- : string Tsort.sort_result = Tsort.Sorted ["foundation"; "walls"; "roof"]

# Tsort.sort [("foundation", ["building permit"]); ("walls", ["foundation"]); ("roof", ["walls"])] ;;
- : string Tsort.sort_result =
Tsort.Sorted ["building permit"; "foundation"; "walls"; "roof"]

# Tsort.sort [("foundation", ["roof"]); ("walls", ["foundation"]); ("roof", ["walls"])] ;;
- : string Tsort.sort_result = Tsort.ErrorCycle ["roof"; "foundation"; "walls"]

As you can see from the second example, if there's a dependency on a node that doesn't exist in the assoc list keys, it's automatically inserted, and assumed to have no dependencies.

Detecting non-existent dependencies

If your graph comes directly from user input, there's a good chance that dependency on a non-existent node is a user error.

To prevent it, use Tsort.find_nonexistent_nodes. Example:

# Tsort.find_nonexistent_nodes [("foundation", ["building permit"]); ("walls", ["foundation"]); ("roof", ["walls"])] ;;
- : (string * string list) list = [("foundation", ["building permit"])]

Sorting graphs with cycles

Sometimes cycles are fine. In this case you can use Tsort.sort_strongly_connected_components to split your graph into strongly connected components and sort its condensation.

Contrived example: you want to line up the Addams family so that children come after parents, but spouse and sibling pairs are not separated.

Tsort.sort_strongly_connected_components [
  "Morticia",  ["Gomez"; "Grandmama"];
  "Gomez",     ["Morticia"; "Grandmama"];
  "Wednesday", ["Morticia"; "Gomez"; "Pugsley"];
  "Pugsley",   ["Morticia"; "Gomez"; "Wednesday"];
  "Grandmama", [];
  "Fester",    []
]
;;

- : string list list =
[["Fester"]; ["Grandmama"]; ["Morticia"; "Gomez"]; ["Wednesday"; "Pugsley"]]

There's also Tsort.find_strongly_connected_components if you just want to find what them. For the data above, it would return [["Morticia"; "Gomez"]; ["Wednesday"; "Pugsley"]; ["Grandmama"]; ["Fester"]].

Contributing

To run our complete test suite, run make test-complete (requires docker).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].