graphs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: MIT Imports: 6 Imported by: 5

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 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[Vertex]map[Vertex]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 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[Vertex]int, err error)

Types

type BFSWalkFunc

type BFSWalkFunc func(Vertex, *bool)

type DFSWalkFunc

type DFSWalkFunc func(Vertex, *bool)

type Edge

type Edge struct {
	Start Vertex
	End   Vertex
	Cost  float64
}

An Edge connects two vertices with a cost.

type Graph

type Graph struct {
	Adjacency map[Vertex]*Set
	Directed  bool
}

A Graph is defined by its vertices and edges stored as an adjacency set.

func Kruskal

func Kruskal(g *Graph) *Graph

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

func NewDigraph

func NewDigraph() *Graph

NewDigraph creates a new empty directed graph.

func NewGraph

func NewGraph() *Graph

NewGraph creates a new empty 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 (*Graph) AddEdge

func (g *Graph) AddEdge(v1, v2 Vertex, c float64)

AddEdge adds an edge to the graph. The edge connects vertex v1 and vertex v2 with cost c.

func (*Graph) AddVertex

func (g *Graph) AddVertex(v Vertex)

AddVertex adds the given vertex to the graph.

func (*Graph) Dump

func (g *Graph) Dump()

Dump prints all edges with their cost to stdout.

func (*Graph) EdgesIter

func (g *Graph) EdgesIter() chan Edge

EdgesIter returns a channel with all edges of the graph.

func (*Graph) Equals

func (g *Graph) Equals(g2 *Graph) bool

Equals returns whether the graph is equal to the given graph. Two graphs are equal of their adjacency is equal.

func (*Graph) HalfedgesIter

func (g *Graph) HalfedgesIter(v Vertex) chan Halfedge

HalfedgesIter returns a channel with all halfedges for the given start vertex.

func (*Graph) NEdges

func (g *Graph) NEdges() int

NEdges returns the number of edges.

func (*Graph) NVertices

func (g *Graph) NVertices() int

NVertices returns the number of vertices.

func (*Graph) SortedEdges

func (g *Graph) SortedEdges() SortedEdges

SortedEdges returns an array of edges sorted by their cost.

func (*Graph) VerticesIter

func (g *Graph) VerticesIter() chan Vertex

VerticesIter returns a channel where all vertices are sent to.

type Halfedge

type Halfedge struct {
	End  Vertex
	Cost float64
}

A Halfedge is an edge where just the end vertex is stored. The start vertex is inferred from the context.

type Network

type Network struct {
	Graph    *Graph
	Source   Vertex
	Sink     Vertex
	Flow     map[Edge]uint
	Capacity map[Edge]uint
}

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.

type SortedEdges

type SortedEdges []Edge

SortedEdges is an array of edges that can be sorted by their cost.

func (SortedEdges) Len

func (se SortedEdges) Len() int

func (SortedEdges) Less

func (se SortedEdges) Less(i, j int) bool

func (SortedEdges) Swap

func (se SortedEdges) Swap(i, j int)

type Vertex

type Vertex interface{}

A Vertex can be just anything.

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.

Jump to

Keyboard shortcuts

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