Help us shape the future of Go! Take the 2026 Go Developer Survey

gogl

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2014 License: MIT Imports: 2 Imported by: 4

README

gogl

Build Status Coverage Status

gogl is a graph library in Go. Its goal is to provide simple, unifying interfaces and implementations of graph algorithms and datastructures that can scale from use cases for small graphs up to very large graphs.

TODO: just about everything. yay!

Documentation

Overview

gogl provides a framework for representing and working with graphs.

gogl is divided chiefly into two parts: datastructures and algorithms. These components are loosely coupled; the algorithms rely strictly on gogl's various exported Graph interfaces to do their work.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseEdge

type BaseEdge struct {
	U Vertex
	V Vertex
}

BaseEdge is a struct used internally to represent edges and meet the Edge interface requirements. It uses the standard notation, (u,v), for vertex pairs in an edge.

func (BaseEdge) Both

func (e BaseEdge) Both() (Vertex, Vertex)

func (BaseEdge) Source

func (e BaseEdge) Source() Vertex

func (BaseEdge) Target

func (e BaseEdge) Target() Vertex

type BaseWeightedEdge added in v0.1.0

type BaseWeightedEdge struct {
	BaseEdge
	W int
}

func (BaseWeightedEdge) Weight added in v0.1.0

func (e BaseWeightedEdge) Weight() int

type Directed added in v0.1.0

type Directed struct {
	// contains filtered or unexported fields
}

func NewDirected added in v0.1.0

func NewDirected() *Directed

Composite literal to create a new Directed.

func (*Directed) AddEdges added in v0.1.0

func (g *Directed) AddEdges(edges ...Edge)

Adds edges to the graph.

func (*Directed) Density added in v0.1.0

func (g *Directed) Density() float64

Returns the density of the graph. Density is the ratio of edge count to the number of edges there would be in complete graph (maximum edge count).

func (*Directed) EachAdjacent added in v0.1.0

func (g *Directed) EachAdjacent(vertex Vertex, f func(target Vertex))

Given a vertex present in the graph, passes each vertex adjacent to the provided vertex to the provided closure.

func (*Directed) EachEdge added in v0.1.0

func (g *Directed) EachEdge(f func(edge Edge))

Traverses the set of edges in the graph, passing each edge to the provided closure.

func (*Directed) EachVertex added in v0.1.0

func (g *Directed) EachVertex(f func(vertex Vertex))
Base adjacencyList methods

Traverses the graph's vertices in random order, passing each vertex to the provided closure.

func (*Directed) EnsureVertex added in v0.1.0

func (g *Directed) EnsureVertex(vertices ...Vertex)

Adds the provided vertices to the graph. If a provided vertex is already present in the graph, it is a no-op (for that vertex only).

func (*Directed) HasEdge added in v0.1.0

func (g *Directed) HasEdge(edge Edge) bool

Indicates whether or not the given edge is present in the graph.

func (*Directed) HasVertex added in v0.1.0

func (g *Directed) HasVertex(vertex Vertex) (exists bool)

Indicates whether or not the given vertex is present in the graph.

func (*Directed) InDegree added in v0.1.0

func (g *Directed) InDegree(vertex Vertex) (degree int, exists bool)

Returns the indegree of the provided vertex. If the vertex is not present in the graph, the second return value will be false.

Note that getting indegree is inefficient for directed adjacency lists; it requires a full scan of the graph's edge set.

func (*Directed) Order added in v0.1.0

func (g *Directed) Order() int

Returns the order (number of vertices) in the graph.

func (*Directed) OutDegree added in v0.1.0

func (g *Directed) OutDegree(vertex Vertex) (degree int, exists bool)

Returns the outdegree of the provided vertex. If the vertex is not present in the graph, the second return value will be false.

func (*Directed) RemoveEdges added in v0.1.0

func (g *Directed) RemoveEdges(edges ...Edge)

Removes edges from the graph. This does NOT remove vertex members of the removed edges.

func (*Directed) RemoveVertex added in v0.1.0

func (g *Directed) RemoveVertex(vertices ...Vertex)

Removes a vertex from the graph. Also removes any edges of which that vertex is a member.

func (*Directed) Size added in v0.1.0

func (g *Directed) Size() int

Returns the size (number of edges) in the graph.

func (*Directed) Transpose added in v0.1.0

func (g *Directed) Transpose() DirectedGraph

type DirectedGraph

type DirectedGraph interface {
	Graph
	Transpose() DirectedGraph
}

DirectedGraph describes a Graph all of whose edges are directed.

Implementing DirectedGraph is the only unambiguous signal gogl provides that a graph's edges are directed.

type Edge

type Edge interface {
	Source() Vertex
	Target() Vertex
	Both() (Vertex, Vertex)
}

type Graph

type Graph interface {
	EachVertex(f func(vertex Vertex))
	EachEdge(f func(edge Edge))
	EachAdjacent(vertex Vertex, f func(adjacent Vertex))
	HasVertex(vertex Vertex) bool
	HasEdge(e Edge) bool
	Order() int
	Size() int
	InDegree(vertex Vertex) (int, bool)
	OutDegree(vertex Vertex) (int, bool)
}

Graph is gogl's most basic interface: it contains only the methods that *every* type of graph implements.

Graph is intentionally underspecified: both directed and undirected graphs implement it; simple graphs, multigraphs, weighted, labeled, or any combination thereof.

The semantics of some of these methods vary slightly from one graph type to another, but in general, the baisc Graph methods are supplemented, not superceded, by the methods in more specific interfaces.

Graph is a purely read oriented interface; the various Mutable*Graph interfaces contain the methods for writing.

type MutableGraph added in v0.1.0

type MutableGraph interface {
	Graph
	EnsureVertex(vertices ...Vertex)
	RemoveVertex(vertices ...Vertex)
	AddEdges(edges ...Edge)
	RemoveEdges(edges ...Edge)
}

MutableGraph describes a graph with basic edges (no weighting, labeling, etc.) that can be modified freely by adding or removing vertices or edges.

type MutableWeightedGraph added in v0.1.0

type MutableWeightedGraph interface {
	WeightedGraph
	EnsureVertex(vertices ...Vertex)
	RemoveVertex(vertices ...Vertex)
	AddEdges(edges ...WeightedEdge)
	RemoveEdges(edges ...WeightedEdge)
}

MutableWeightedGraph is the mutable version of a weighted graph. Its AddEdges() method is incompatible with MutableGraph, guaranteeing only weighted edges can be present in the graph.

func NewWeightedDirected added in v0.1.0

func NewWeightedDirected() MutableWeightedGraph

func NewWeightedUndirected added in v0.1.0

func NewWeightedUndirected() MutableWeightedGraph

type SimpleGraph

type SimpleGraph interface {
	Graph
	Density() float64
}

A simple graph is in opposition to a multigraph: it disallows loops and parallel edges.

type Undirected added in v0.1.0

type Undirected struct {
	// contains filtered or unexported fields
}

func NewUndirected added in v0.1.0

func NewUndirected() *Undirected

func (*Undirected) AddEdges added in v0.1.0

func (g *Undirected) AddEdges(edges ...Edge)

Adds edges to the graph.

func (*Undirected) Density added in v0.1.0

func (g *Undirected) Density() float64

Returns the density of the graph. Density is the ratio of edge count to the number of edges there would be in complete graph (maximum edge count).

func (*Undirected) EachAdjacent added in v0.1.0

func (g *Undirected) EachAdjacent(vertex Vertex, f func(target Vertex))

Given a vertex present in the graph, passes each vertex adjacent to the provided vertex to the provided closure.

func (*Undirected) EachEdge added in v0.1.0

func (g *Undirected) EachEdge(f func(edge Edge))

Traverses the set of edges in the graph, passing each edge to the provided closure.

func (*Undirected) EachVertex added in v0.1.0

func (g *Undirected) EachVertex(f func(vertex Vertex))
Base adjacencyList methods

Traverses the graph's vertices in random order, passing each vertex to the provided closure.

func (*Undirected) EnsureVertex added in v0.1.0

func (g *Undirected) EnsureVertex(vertices ...Vertex)

Adds the provided vertices to the graph. If a provided vertex is already present in the graph, it is a no-op (for that vertex only).

func (*Undirected) HasEdge added in v0.1.0

func (g *Undirected) HasEdge(edge Edge) bool

Indicates whether or not the given edge is present in the graph.

func (*Undirected) HasVertex added in v0.1.0

func (g *Undirected) HasVertex(vertex Vertex) (exists bool)

Indicates whether or not the given vertex is present in the graph.

func (*Undirected) InDegree added in v0.1.0

func (g *Undirected) InDegree(vertex Vertex) (degree int, exists bool)

Returns the indegree of the provided vertex. If the vertex is not present in the graph, the second return value will be false.

func (*Undirected) Order added in v0.1.0

func (g *Undirected) Order() int

Returns the order (number of vertices) in the graph.

func (*Undirected) OutDegree added in v0.1.0

func (g *Undirected) OutDegree(vertex Vertex) (degree int, exists bool)

Returns the outdegree of the provided vertex. If the vertex is not present in the graph, the second return value will be false.

func (*Undirected) RemoveEdges added in v0.1.0

func (g *Undirected) RemoveEdges(edges ...Edge)

Removes edges from the graph. This does NOT remove vertex members of the removed edges.

func (*Undirected) RemoveVertex added in v0.1.0

func (g *Undirected) RemoveVertex(vertices ...Vertex)

Removes a vertex from the graph. Also removes any edges of which that vertex is a member.

func (*Undirected) Size added in v0.1.0

func (g *Undirected) Size() int

Returns the size (number of edges) in the graph.

type Vertex

type Vertex interface{}

As a rule, gogl tries to place as low a requirement on its vertices as possible. This is because, from a purely graph theoretic perspective, vertices are inert. Boring, even. Graphs are more about the topology, the characteristics of the edges connecting the points than the points themselves. Your use case cares about the content of your vertices, but gogl does not. Consequently, anything can act as a vertex.

type WeightedEdge added in v0.1.0

type WeightedEdge interface {
	Edge
	Weight() int
}

type WeightedGraph added in v0.1.0

type WeightedGraph interface {
	Graph
	HasWeightedEdge(e WeightedEdge) bool
	EachWeightedEdge(f func(edge WeightedEdge))
}

A weighted graph is a graph subtype where the edges have a numeric weight; as described by the WeightedEdge interface, this weight is a signed int.

WeightedGraphs have both the HasEdge() and HasWeightedEdge() methods. Correct implementations should treat the difference as a matter of strictness:

HasEdge() should return true as long as an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), regardless of its weight.

HasWeightedEdge() should return true iff an edge exists connecting the two given vertices (respecting directed or undirected as appropriate), AND if the edge weights are the same.

Directories

Path Synopsis
Contains algos and logic related to depth-first graph traversal.
Contains algos and logic related to depth-first graph traversal.

Jump to

Keyboard shortcuts

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