Coder Social home page Coder Social logo

topologicalsorting's Introduction

Topological Sorting

Topological sorting is the process of ordering a directed graph, such that all vertices with a link to another vertex are considered before the destination vertex. Topological sorting is often used for dependency resolution, when a series of tasks need to be executed, and a certain task cannot be executed until another one it depends upon has already executed.

TopologicalSorting

This is a class library to provide topological sorting to .net programs using a fluent interface or class attributes to express relationships between processes. The system also provides a system for expressing if some tasks use a resource which cannot be concurrently accessed and thus two tasks which use that resource must not be run in parallel.

Fluent Interface

Tasks are represented by OrderedProcess objects. OrderedProcess objects have names to uniquely identifty them, or you can inherit off OrderedProcess directly in your code where you implement your application specific tasks. Order of tasks is simple (find more examples in the Tests project):

DependencyGraph g = new DependencyGraph();

OrderedProcess a = new OrderedProcess(g, "Task A");
OrderedProcess b = new OrderedProcess(g, "Task B");
OrderedProcess c = new OrderedProcess(g, "Task C");

a.Before(b);
b.Before(c);

The ordering relationships can be chained, so the previous ordering could alternatively be expressed like so:

a.Before(b).Before(c);

Of course, the expression of ordering can also be reversed:

c.After(b).After(a);

Multiple process relationships can be expressed together:

a.Before(b, c, d);

Even more can be chained on to the end of this:

a.Before(b, c, d).After(e);

This states that a must run before b, c or d, and that e must run before b, c and d.

Output

A sort operation is calculated by calling CalculateSort on a dependency graph after all the process relationships have been set up. The output is a TopologicalSort object (or an InvalidOperationException if an impossible set of constraints is in place). A topological sort object is enumerable in two different ways. enumerating it as an IEnumerable will return all the processes in a valid order to execute them. Alternatively enumerating as an IEnumerable<ISet> will enumerate sets of processes which may be executed in parallel.

Example

a.Before(b, c).Before(d);

In this example, "a" must execute before "b", "c" and "d", and "d" must execute before "b" and "c", but there are no constraints between "b" and "c". Therefore either of these results would be valid:

a -> b -> c -> d
a -> c -> b -> d

However, if enumerated as an IEnumerable<ISet> we would instead get the one single valid result:

a -> {b, c} -> d

topologicalsorting's People

Contributors

martindevans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.