All Projects → glebec → batching-toposort

glebec / batching-toposort

Licence: MIT license
Efficiently sort interdependent tasks into a sequence of concurrently-executable batches

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to batching-toposort

django-postgresql-dag
Directed Acyclic Graphs with a variety of methods for both Nodes and Edges, and multiple exports (NetworkX, Pandas, etc). This project is the foundation for a commercial product, so expect regular improvements. PR's and other contributions are welcomed.
Stars: ✭ 23 (+9.52%)
Mutual labels:  dag, directed-acyclic-graph
breaking cycles in noisy hierarchies
breaking cycles in noisy hierarchies
Stars: ✭ 59 (+180.95%)
Mutual labels:  dag, directed-acyclic-graph
daglib
Directed Acyclic Graphs With Modern Fortran
Stars: ✭ 20 (-4.76%)
Mutual labels:  toposort, directed-acyclic-graph
obyte-hub
Hub for Obyte network
Stars: ✭ 17 (-19.05%)
Mutual labels:  dag, directed-acyclic-graph
laravel-dag-manager
A SQL-based Directed Acyclic Graph (DAG) solution for Laravel.
Stars: ✭ 24 (+14.29%)
Mutual labels:  dag, directed-acyclic-graph
vue-dag
🏗 Data-driven directed acyclic graph (DAG) builder for Vue.js
Stars: ✭ 37 (+76.19%)
Mutual labels:  dag, directed-acyclic-graph
Sorty
Fast Concurrent / Parallel Sorting in Go
Stars: ✭ 74 (+252.38%)
Mutual labels:  concurrency, sort
go-left-right
A faster RWLock primitive in Go, 2-3 times faster than RWMutex. A Go implementation of concurrency control algorithm in paper <Left-Right - A Concurrency Control Technique with Wait-Free Population Oblivious Reads>
Stars: ✭ 42 (+100%)
Mutual labels:  concurrency
concurrent-ll
concurrent linked list implementation
Stars: ✭ 66 (+214.29%)
Mutual labels:  concurrency
sublime-postcss-sorting
Sublime Text plugin to sort CSS rules content with specified order.
Stars: ✭ 19 (-9.52%)
Mutual labels:  sort
pipeline
Pipelines using goroutines
Stars: ✭ 46 (+119.05%)
Mutual labels:  concurrency
gbp
Golang Best Practices (GBP™)
Stars: ✭ 25 (+19.05%)
Mutual labels:  concurrency
java-red
Effective Concurrency Modules for Java
Stars: ✭ 25 (+19.05%)
Mutual labels:  concurrency
workerpool
A workerpool that can get expanded & shrink dynamically.
Stars: ✭ 55 (+161.9%)
Mutual labels:  concurrency
archery
Abstract over the atomicity of reference-counting pointers in rust
Stars: ✭ 107 (+409.52%)
Mutual labels:  concurrency
reactor-workshop
Spring Reactor hands-on training (3 days)
Stars: ✭ 114 (+442.86%)
Mutual labels:  concurrency
rockgo
A developing game server framework,based on Entity Component System(ECS).
Stars: ✭ 617 (+2838.1%)
Mutual labels:  concurrency
Causing
Causing: CAUsal INterpretation using Graphs
Stars: ✭ 47 (+123.81%)
Mutual labels:  dag
dragonfly-dag
完全支持Vue3和Vitejs的DAG流程图组件
Stars: ✭ 54 (+157.14%)
Mutual labels:  dag
Static-Sort
A simple C++ header-only library for fastest sorting of small arrays. Generates sorting networks on compile time via templates.
Stars: ✭ 30 (+42.86%)
Mutual labels:  sort

Batching-Toposort

npm version Build Status code style: prettier

Efficiently sort interdependent tasks into a sequence of concurrently-executable batches.

batchingToposort :: { DependencyId : [DependentId] } -> [[TaskId]]
  • O(t + d) time complexity (for t tasks and d dependency relationships)
  • O(t) space complexity
  • Zero package dependencies
  • Thoroughly tested (including invariant tests)
  • Errors on cyclic graphs

Motivation

Often one needs to schedule interdependent tasks. In order to determine task order, the classic solution is to use topological sort. However, toposort typically outputs a list of individual tasks, without grouping those that can be executed concurrently. Batching-Toposort takes this additional consideration into account, producing a list of lists of tasks. The outer list is ordered; each inner list is unordered.

Usage

npm install batching-toposort

Batching-Toposort expects a directed acyclic graph (DAG) implemented via adjacency list. In other words, construct an object whose keys are dependency IDs, and whose values are lists of dependent IDs.

const batchingToposort = require('batching-toposort')

// DAG :: { DependencyId : [DependentId] }
const DAG = {
    a: ['c', 'f'], // `a` is a dependency of `c` and `f`
    b: ['d', 'e'],
    c: ['f'],
    d: ['f', 'g'],
    e: ['h'],
    f: ['i'],
    g: ['j'],
    h: ['j'],
    i: [],
    j: [],
}

// batchingToposort :: DAG -> [[TaskId]]
const taskBatches = batchingToposort(DAG)
// [['a', 'b'], ['c', 'd', 'e'], ['f', 'g', 'h'], ['i', 'j']]

(If there is demand, Batching-Toposort may one day include a small DAG API and/or DOT support for convenience, but as of now it is the developer's role to construct the graph.)

Implementation

In short, Batching-Toposort adapts Kahn's Algorithm by inserting each round of root tasks into sublists rather than appending tasks directly to the main output list.

The classic DAG toposort keeps track of each task's in-degree (number of dependencies). As root tasks (those with no dependencies) are added to the output list, their dependents' in-degree counts are decremented. For a task to become a root, all of its dependencies must have been accounted for. The core algorithm is illustrated below in pseudocode (the actual implementation is in src/index.js).

given G = adjacency list of tasks and dependents (~O(1) lookup):

let N = map from tasks to in-degree counts (~O(1) lookup / update)
let L = [] (empty output list) (~O(1) append)
let R1 = list of root tasks (~O(1) addition, ~O(n) iteration)

while R1 is nonempty
    append R1 to L
    let R2 = [] (empty list for next batch) (~O(1) append)
    for each task T in R1
        for each dependent D of T (as per G)
            decrement in-degree count for D (in N)
            if D's in-degree (as per N) is 0
                add D to R2
    R1 = R2

return L

Performance

The time complexity is O(|V| + |E|) for V task vertices and E dependency edges.

  • The algorithm loops through rounds of roots, and every task is only a root only once, contributing to O(|V|) rounds (worst case is a linked list of tasks).
  • Each round handles a disjoint set of dependency edges (those rooted in that round's tasks), so the O(|E|) handling of all edges is effectively distributed across rounds.
  • Other operations, e.g. querying a node's in-degree (average case O(1)), are carefully managed to preserve the time complexity.

The space complexity is slightly better at O(|V|).

  • The in-degree map size is proportional to the number of vertices |V|, but not edges, as those are folded into an integer count during map construction.
  • The output by definition contains |V| tasks (distributed among as many or fewer lists).
  • Again, other operations are controlled to keep space complexity low.
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].