graph

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

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 5 Imported by: 0

README

Graph

Go implementations of common graph algorithms like Dijkstra, A*, Minimam spanning tree, etc.

Current support

  • Directional graph.
  • Bidirectional graph.
  • Shortest paths. (Dijkstra)

Install

go get -d github.com/isutare412/graph

Documentation

Overview

Package graph implements several graph algorithms like Dijkstra, A*, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

type Graph struct {
	Type
	// contains filtered or unexported fields
}

Graph implements an adjacency list. You should create a Graph by calling New(Type) function. As Graph doest have any location or coordinate, Graph cannot use A* algorithm. If you want A* algorithm, use CGraph instead.

func New

func New(t Type) *Graph

New returns initialized Graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(from, to Vertex, weight int)

AddEdge adds a new edge with weight from Vertex to Vertex.

func (*Graph) NewVertex

func (g *Graph) NewVertex() Vertex

NewVertex returns a new vertex which is ready to use.

func (*Graph) RemoveEdges

func (g *Graph) RemoveEdges(from, to Vertex)

RemoveEdges removes all edges from 'from' to 'to'. If Type of g is Directional, the other edges (from 'to' to 'from') are not removed.

func (*Graph) RemoveVertex

func (g *Graph) RemoveVertex(id VertexID) bool

RemoveVertex removes the vertex with 'id'. Then edges that point to the removed vertex are also removed. Returns true if the vertex is removed.

func (*Graph) ShortestPath

func (g *Graph) ShortestPath(src, dest Vertex) (p Path)

ShortestPath returns shortest path p from src to dest. You can check whether the path exists by checking p.Destination() or p.Distance(). As g cannot be applied A* algorithm, ShortestPath uses Dijkstra's one instead.

func (*Graph) ShortestPaths

func (g *Graph) ShortestPaths(source Vertex) map[Vertex]Path

ShortestPaths returns shortest paths from source to every vertices which are reachable from source.

func (*Graph) String

func (g *Graph) String() string

type Path

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

Path implements specific path to a vertex.

func (Path) Destination

func (p Path) Destination() (dest Vertex, ok bool)

Destination returns the destination of current path. ok is false if the Path does not include any Vertex yet.

func (Path) Distance

func (p Path) Distance() int

Distance returns a total distance to the destination. Returns negative number if the Path does not include any Vertex.

func (Path) IterateEdge

func (p Path) IterateEdge(handler func(to Vertex, weight int) bool)

IterateEdge iterates edges of p sequentially. If handler returns false, iteration will stop.

func (Path) String

func (p Path) String() string

type Type

type Type int8

Type enumerates type of Graph.

const (
	// Directional graph. A -> B != B -> A.
	Directional Type = iota
	// Bidirectional graph. A <-> B.
	Bidirectional
)

type Vertex

type Vertex struct {

	// Value stores user defined values.
	Value *interface{}
	// contains filtered or unexported fields
}

Vertex of graph. It is safe to copy Vertex.

func (Vertex) ID

func (v Vertex) ID() VertexID

ID returns VertexID(int).

type VertexID

type VertexID int

VertexID is an identity for each vertex.

func (VertexID) String

func (id VertexID) String() string

Jump to

Keyboard shortcuts

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