algo

package
v0.0.0-...-152623e Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2017 License: MIT Imports: 6 Imported by: 0

README

Graph Algorithms in Go

Travis CI status

This repository contains implementations of various graph algorithms written in Go. I’ve written them to learn about these algorithms.

Implemented

Contributors

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDAG = errors.New("graphs: graph is not a DAG")

Functions

func BFS

func BFS(g Graph, start Vertex, walkFunc BFSWalkFunc)

func BellmanFord

func BellmanFord(g Graph, start, end Vertex) []Vertex

BellmanFord implements the Bellman-Ford algorithm. It returns the shortest-weight path from start to end vertex as a slice, or nil if the graph contains a negative-weight cycle.

func DFS

func DFS(g Graph, start Vertex, walkFunc DFSWalkFunc)

func Dijkstra

func Dijkstra(g Graph, start, end Vertex) *list.List

func FloydWarshall

func FloydWarshall(g Graph) map[string]map[string]float64

FloydWarshall implements the Floyd–Warshall algorithm. It returns the cost matrix for each vertex to each other vertex of the given graph. It does not check for negative weight cycles.

func Kruskal

func Kruskal(g Graph) Graph

Kruskal implements Kruskal’s algorithm. It returns a minimal spanning tree for the given graph.

func Prim

func Prim(g Graph, start Vertex) Graph

Prim implements Prim’s algorithm. It returns a minimal spanning tree for the given graph, starting with vertex start.

func TarjanStrongCC

func TarjanStrongCC(g Graph) *list.List

TarjanStrongCC returns a list of sets of vertices. Each set is a strongly connected component of the graph.

func TopologicalSort

func TopologicalSort(g Graph) (topologicalOrder *list.List, topologicalClasses map[string]int, err error)

Types

type BFSWalkFunc

type BFSWalkFunc func(Vertex, *bool)

type DFSWalkFunc

type DFSWalkFunc func(Vertex, *bool)

type Network

type Network struct {
	Graph    Graph
	Source   Vertex
	Sink     Vertex
	Flow     map[string]uint // by edge id
	Capacity map[string]uint // by edge id
}

func NewNetwork

func NewNetwork(graph Graph, source, sink Vertex) *Network

func (*Network) Equals

func (n *Network) Equals(n2 *Network) bool

func (*Network) ResidualNetwork

func (n *Network) ResidualNetwork() *Network

func (*Network) SetCapacity

func (n *Network) SetCapacity(v, w Vertex, c uint)

func (*Network) SetFlow

func (n *Network) SetFlow(v, w Vertex, f uint)

func (*Network) SetFlowAndCapacity

func (n *Network) SetFlowAndCapacity(v, w Vertex, f, c uint)

type Set

type Set map[interface{}]struct{}

A Set is a container that contains each element just once.

func NewSet

func NewSet() *Set

NewSet creates a new empty set.

func NewSetWithElements

func NewSetWithElements(elements ...interface{}) *Set

NewSetWithElements creates a new set with the given arguments as elements.

func (*Set) Add

func (s *Set) Add(element interface{}) bool

Add adds an element to the set. It returns true if the element has been added and false if the set already contained that element.

func (*Set) Any

func (s *Set) Any() interface{}

Any returns any element from the set.

func (*Set) Contains

func (s *Set) Contains(element interface{}) bool

Contains returns whether the set contains the given element.

func (*Set) Each

func (s *Set) Each(f func(interface{}, *bool))

Each executes the given function for each element in the set.

func (*Set) Equals

func (s *Set) Equals(s2 *Set) bool

Equals returns whether the given set is equal to the receiver.

func (*Set) Iter

func (s *Set) Iter() chan interface{}

Iter returns a channel where all elements of the set are sent to.

func (*Set) Len

func (s *Set) Len() int

Len returns the number of elements.

func (*Set) Merge

func (s *Set) Merge(s2 *Set)

Merge adds the elements of the given set to the receiver.

func (*Set) Remove

func (s *Set) Remove(element interface{}) bool

Remove removes the given element from the set and returns whether the element was removed from the set.

Jump to

Keyboard shortcuts

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