graphs

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 4 Imported by: 0

README

Graphs

This implements a simple graph (network) package.

History

It was first written to support an implementation of the Dollar Game so it has some vagaries more applicable to that. Examples include: the Vertex object value is an int and has Increment() and Decrement() helper functions.

Resources

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCycle added in v0.0.8

func IsCycle(path []*Vertex) bool

IsCycle returns true if the nodes form a cycle

func VisitAll added in v0.0.8

func VisitAll(node *Vertex, visited map[string]bool)

Types

type AdjacencyList

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

AdjacencyList implements an undirected graph

func NewAL

func NewAL() AdjacencyList

NewAL returns a new, empty adjacdency list

func (*AdjacencyList) AddEdge

func (a *AdjacencyList) AddEdge(n1, n2 *Vertex)

AddEdge adds an edge, adding the nodes if they are not already present

func (*AdjacencyList) AddNode

func (a *AdjacencyList) AddNode(node *Vertex)

AddNode adds a node to the adjacency list if not already present

func (*AdjacencyList) Connected added in v0.0.8

func (a *AdjacencyList) Connected() bool

Connected returns true if every vertex is reachable from every other vertex

func (*AdjacencyList) Copy

func (a *AdjacencyList) Copy() AdjacencyList

Copy returns a copy of the AdjacencyList

func (*AdjacencyList) EdgeCount

func (a *AdjacencyList) EdgeCount() int

EdgeCount returns the number of distinct edges in the adjacency list

func (*AdjacencyList) EulerianCycle added in v0.0.8

func (a *AdjacencyList) EulerianCycle() []*Vertex

EulerianCycle returns a vertex slice, the traversal of which will touch each edge once, returning to the start vertex

func (*AdjacencyList) EulerianPath added in v0.0.8

func (a *AdjacencyList) EulerianPath() []*Vertex

EulerianPath returns a vertex slice, the traversal of which will touch each edge once

func (*AdjacencyList) FirstNode

func (a *AdjacencyList) FirstNode() *Vertex

FirstNode returns a node from the map

func (*AdjacencyList) Genus

func (a *AdjacencyList) Genus() int

Genus returns the genus number of the adjacency list

func (*AdjacencyList) HamiltonianCycle added in v0.0.8

func (a *AdjacencyList) HamiltonianCycle() []*Vertex

HamiltonianCycle returns a vertex slice, the traversal of which will touch each vertex once

func (*AdjacencyList) HamiltonianPath added in v0.0.8

func (a *AdjacencyList) HamiltonianPath() []*Vertex

HamiltonianPath returns a vertex slice, the traversal of which will touch each vertex once

func (*AdjacencyList) HasNode

func (a *AdjacencyList) HasNode(node Vertex) bool

HasNode returns true if the node is already in the adjacency list

func (*AdjacencyList) MinimalVertexCover

func (a *AdjacencyList) MinimalVertexCover() map[string]*Vertex

MinimalVertexCover returns vertices that make a minimal (not guaranteed to be minimum) vertex cover

func (*AdjacencyList) NodeCount

func (a *AdjacencyList) NodeCount() int

NodeCount returns the number of nodes in the adjacency list

func (*AdjacencyList) NodeWithMostEdges

func (a *AdjacencyList) NodeWithMostEdges() *Vertex

NodeWithMostEdges returns the node with the highest edge count

func (*AdjacencyList) Nodes

func (a *AdjacencyList) Nodes() map[string]*Vertex

Nodes returns the map of nodes in the adjacency list

func (*AdjacencyList) RemoveNode

func (a *AdjacencyList) RemoveNode(node *Vertex)

RemoveNode removes a node from the adjacency list

func (*AdjacencyList) RemoveOrphans

func (a *AdjacencyList) RemoveOrphans()

RemoveOrphans removes all vertices that have no edges

func (*AdjacencyList) Serialize

func (a *AdjacencyList) Serialize(title string) string

Serialize returns the adjacency list in graphviz format

func (*AdjacencyList) ValueLowest

func (a *AdjacencyList) ValueLowest() *Vertex

ValueLowest returns the node with the lowest value

func (*AdjacencyList) ValueSum

func (a *AdjacencyList) ValueSum() int

ValueSum returns the sum of all node values

func (*AdjacencyList) Whiskers

func (a *AdjacencyList) Whiskers() map[string]*Vertex

Whiskers returns a map of vertices that have only one edge

type Vertex

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

Vertex implements a graph Vertex

func NewVertex

func NewVertex(name string, value int) Vertex

NewVertex returns a new Vertex

func (*Vertex) AddNeighbor

func (v *Vertex) AddNeighbor(node *Vertex)

AddNeighbor adds a neighbor vertex

func (*Vertex) Decrement

func (v *Vertex) Decrement()

Decrement decrements the vertex value by 1

func (*Vertex) Degree added in v0.0.8

func (v *Vertex) Degree() int

Degree returns the degree of the incoming edges (loops count as 2)

func (*Vertex) FirstNeighbor

func (v *Vertex) FirstNeighbor() *Vertex

FirstNeighbor returns the first neighbor

func (*Vertex) HasNeighbor

func (v *Vertex) HasNeighbor(node Vertex) bool

HasNeighbor returns true if the given vertex is already a neighbor

func (*Vertex) ID

func (v *Vertex) ID() string

ID returns the id of the vertex

func (*Vertex) Increment

func (v *Vertex) Increment()

Increment increments the vertex value by 1

func (*Vertex) Name

func (v *Vertex) Name() string

Name returns the vertex name

func (*Vertex) NeighborCount added in v0.0.8

func (v *Vertex) NeighborCount() int

NeighborCount returns the number of edges (neighbors)

func (*Vertex) Neighbors

func (v *Vertex) Neighbors() map[string]*Vertex

Neighbors returns a map of all neighbors

func (*Vertex) RemoveNeighbor

func (v *Vertex) RemoveNeighbor(node Vertex)

RemoveNeighbor removes a neighbor vertex

func (*Vertex) SetName

func (v *Vertex) SetName(name string)

SetName sets the vertex name

func (*Vertex) SetValue

func (v *Vertex) SetValue(value int)

SetValue sets the vertex value

func (*Vertex) Value

func (v *Vertex) Value() int

Value returns the vertex value

Jump to

Keyboard shortcuts

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