toposort

package module
v0.0.0-...-a504135 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

toposort: a topological sorting library

This package implements a basic topological sorting library using Khan's algorithm. Sorts from this library should be deterministic.

Topological Sorting

In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. —Wikipedia

Image of graph and its topological sorting

Sorts generated should be deterministic, i.e. if you run the sort many times the output will be the same for all sorts.

Example

Your node types must implement the toposort.Node interface, which is very simple:

type Node interface {
	Id() string
}

Id() should return an identifier that uniquely identifies a given node in the graph.

A simple program with a topological sort:

package main

import "github.com/oko/toposort"

type TopoNode struct {
	ID string
}
func (t *TopoNode) Id() string {
	return t.ID
}
var _ toposort.Node = &TopoNode{}

func main() {
	t := toposort.NewTopology()
	a := &TopoNode{ID: "a"}
	b := &TopoNode{ID: "b"}
	t.AddNode(a)
	t.AddNode(b)
	t.AddEdge(a, b)
	sorted, err := t.Sort()
	if err != nil {
		panic("error sorting")
	}
	for _, s := range sorted {
		print(s.Id() + "\n")
	}
}
Example Sorts

If you install Graphviz's dot binary, you can use scripts/example.sh to generate images like the one used at the top of this readme:

$ cd toposort
$ sudo apt install graphviz
$ scripts/example.sh example/bigger.topo

This will generate and open (using xdg-open) a rendering of the example/bigger.topo topology.

Documentation

Overview

Package toposort implements a topological sorting library for Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNodeExists       = errors.New("node already exists in topology")
	ErrNodeDoesNotExist = errors.New("node does not exist in topology")
	ErrRuntimeExceeded  = errors.New("sort runtime exceeded bound")
)

Functions

This section is empty.

Types

type Edge

type Edge struct {
	From Node
	To   Node
}

Edge represents an edge from one node to another

type Edges

type Edges map[string]EdgesFromNode

Edges represents the set of all Edges in a topology grouped by origin node

func (Edges) AddEdge

func (e Edges) AddEdge(from, to Node)

AddEdge adds an edge to this edge set

func (Edges) Copy

func (e Edges) Copy() Edges

Copy returns a copy of the entire Edges structure

func (Edges) Count

func (e Edges) Count() int

Count returns the total count of Edges

func (Edges) HasIncoming

func (e Edges) HasIncoming(n Node) bool

HasIncoming returns whether a node has incoming Edges

type EdgesFromNode

type EdgesFromNode map[string]Edge

EdgesFromNode represents a set of Edges leaving a node

func (EdgesFromNode) Copy

func (ne EdgesFromNode) Copy() EdgesFromNode

Copy returns a copy of EdgesFromNode

type ErrCycleInTopology

type ErrCycleInTopology struct {
	OriginalEdges  Edges
	RemainingEdges Edges
}

func (*ErrCycleInTopology) Error

func (e *ErrCycleInTopology) Error() string

type Node

type Node interface {
	Id() string
}

Node is the interface type for a single node in a topology

type Nodes

type Nodes map[string]Node

Nodes represents the set of Nodes in a topology

func (Nodes) Add

func (n Nodes) Add(node Node) error

Add adds a node to this Nodes set

func (Nodes) Has

func (n Nodes) Has(node Node) bool

Has checks whether a given node is in a Nodes set

type Topology

type Topology struct {
	Nodes Nodes
	Edges Edges
}

Topology represents an entire graph topology

func NewTopology

func NewTopology() *Topology

NewTopology returns a new topology

func (*Topology) AddEdge

func (t *Topology) AddEdge(from, to Node) error

func (*Topology) AddNode

func (t *Topology) AddNode(n Node) error

func (*Topology) Sort

func (t *Topology) Sort() ([]Node, error)

Sort returns a valid topological sorting of this topology's Nodes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL