graph

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

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

Go to latest
Published: Jan 9, 2014 License: MIT Imports: 6 Imported by: 0

README

Graph

Package graph is an implementation of various graphs.

Status

Everything in here is tested. However there are still essential parts missing, such as a symbolic implementation of each graphs and APIs to load the graphs with data from an io.Reader. This will be implemented in a not too far future.

This library has also not been optimized. However, the graphs can handle sizes in the hundred millions vertices. Some algorithms will be very slow on such sizes, most will have an acceptable running time.

Credits

Pretty much all of the algorithms are taken from Algorithm 4th Ed, by Sedgewick and Wayne. The book is written in Java, I've ported it to Go as an exercise in studying the algorithms.

I hope to one day add more algorithms that could make use of the concurrent capabilities of Go.

License

The MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AvgDegree

func AvgDegree(g Graph) float64

AvgDegree is the average degree in graph g. In a directed graph, this is the average out-degree in g.

func Degree

func Degree(g Graph, v int) int

Degree is the degree of vertex v in graph g. In a directed graph, this is the out-degree of v.

func DirectedCycle

func DirectedCycle(di Digraph) []int

DirectedCycle returns a cycle in digraph di, if there is one.

func HasCycle

func HasCycle(g Graph) bool

HasCycle returns if graph g has any cycle.

func IsBipartite

func IsBipartite(g Graph) bool

IsBipartite returns if every vertex in graph g can be colored with only two colors, while never sharing the same color of an adjacent vertex

func MaxDegree

func MaxDegree(g Graph) int

MaxDegree is the maximum degree in graph g. In a directed graph, this is the max out-degree in g.

func MinDegree

func MinDegree(g Graph) int

MinDegree is the minimum degree in graph g. In a directed graph, this is the min out-degree in g.

Types

type DAG

type DAG struct {
	*Digraph
}

DAG is a directed acyclic graph implemented with an adjacency list digraph.

func NewDAG

func NewDAG(d Digraph) (DAG, error)

NewDAG returns a DAG built from digraph d, if d has no cycle. Otherwise it returns an error.

func (*DAG) Sort

func (d *DAG) Sort() []int

Sort gives the topological sort of this DAG.

type Digraph

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

Digraph is a directed graph implementation using an adjacency list

func NewDigraph

func NewDigraph(v int) Digraph

NewDigraph returns a digraph with v vertices, all disconnected

func ReadDigraph

func ReadDigraph(input io.Reader) (Digraph, error)

ReadDigraph constructs an undirected graph from the io.Reader expecting to find data formed such as:

v
e
a b
c d
...
y z

where `v` is the vertex count, `e` the number of edges and `a`, `b`, `c`, `d`, ..., `y` and `z` are edges between `a` and `b`, `c` and `d`, ..., and `y` and `z` respectively.

func (Digraph) AddEdge

func (di Digraph) AddEdge(v, w int)

AddEdge adds an edge from v to w, but not from w to v. This is O(1).

func (Digraph) Adj

func (di Digraph) Adj(v int) []int

Adj is a slice of vertices adjacent to v. This is O(E)

func (Digraph) E

func (di Digraph) E() int

E is the number of edges.

func (Digraph) GoString

func (di Digraph) GoString() string

GoString represents this graph as a string.

func (Digraph) Reverse

func (di Digraph) Reverse() Digraph

Reverse returns the reverse of this digraph

func (Digraph) V

func (di Digraph) V() int

V is the number of vertices.

type Edge

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

Edge is a weighted edge in a weighted graph

func NewEdge

func NewEdge(v, w int, weight float64) Edge

NewEdge creates a weigthed edge to be used by a WeightGraph

func (*Edge) Either

func (e *Edge) Either() int

Either returns either vertices of this edge.

func (*Edge) GoString

func (e *Edge) GoString() string

GoString represents this edge in a directed, weighted fashion

func (*Edge) Less

func (e *Edge) Less(other Edge) bool

Less tells if this edge is less than the other edge

func (*Edge) Other

func (e *Edge) Other(v int) int

Other tells the other end of this edge, from v's perspective.

func (*Edge) Weight

func (e *Edge) Weight() float64

Weight tells the weight of this edge

type Graph

type Graph interface {
	fmt.GoStringer
	// AddEdge adds an edge from v to w.
	AddEdge(v, w int)
	// Adj is a slice of vertices adjacent to v
	Adj(v int) []int
	// V is the number of vertices
	V() int
	// E is the number of edges
	E() int
}

Graph is a graph with V vertices.

type Ungraph

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

Ungraph is an adjacency list undirected graph. It consumes 2E + V spaces.

func NewGraph

func NewGraph(v int) Ungraph

NewGraph returns a Graph of size v implemented with an adjacency vertex list.

func ReadGraph

func ReadGraph(input io.Reader) (Ungraph, error)

ReadGraph constructs an undirected graph from the io.Reader expecting to find data formed such as:

v
e
a b
c d
...
y z

where `v` is the vertex count, `e` the number of edges and `a`, `b`, `c`, `d`, ..., `y` and `z` are edges between `a` and `b`, `c` and `d`, ..., and `y` and `z` respectively.

func (Ungraph) AddEdge

func (a Ungraph) AddEdge(v, w int)

AddEdge adds an edge from v to w. This is O(1).

func (Ungraph) Adj

func (a Ungraph) Adj(v int) []int

Adj is a slice of vertices adjacent to v. This is O(E).

func (Ungraph) E

func (a Ungraph) E() int

E is the number of edges. This is O(1).

func (Ungraph) GoString

func (a Ungraph) GoString() string

GoString represents this graph as a string.

func (Ungraph) V

func (a Ungraph) V() int

V is the number of vertices. This is O(1).

type WeightGraph

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

WeightGraph is a graph with weighted edges.

func NewWeightGraph

func NewWeightGraph(v int) WeightGraph

NewWeightGraph creates an empty graph with v vertices

func ReadWeightGraph

func ReadWeightGraph(input io.Reader) (WeightGraph, error)

ReadWeightGraph constructs an undirected graph from the io.Reader expecting to find data formed such as:

v
e
a b w0
c d w1
...
y z wN

where `v` is the vertex count, `e` the number of edges and `a`, `b`, `c`, `d`, ..., `y` and `z` are edges between `a` and `b`, `c` and `d`, ..., and `y` and `z` respectively, and `wN` is the weight of that edge.

func (*WeightGraph) AddEdge

func (wg *WeightGraph) AddEdge(e Edge)

AddEdge adds weigthed edge e to this graph

func (*WeightGraph) Adj

func (wg *WeightGraph) Adj(v int) []Edge

Adj gives the edges incident to v

func (*WeightGraph) E

func (wg *WeightGraph) E() int

E is the number of edges

func (*WeightGraph) Edges

func (wg *WeightGraph) Edges() []Edge

Edges gives all the edges in this graph

func (*WeightGraph) GoString

func (wg *WeightGraph) GoString() string

GoString represents this weighted graph

func (*WeightGraph) V

func (wg *WeightGraph) V() int

V is the number of vertives

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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