graphs

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package graphs implements a simple graph.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

type Graph[T comparable] struct {
	// contains filtered or unexported fields
}

Graph is a simple graph. It contains a set of vertices and a set of edges, which are pairs of vertices.

func New

func New[T comparable]() *Graph[T]

New returns an empty graph.

func (*Graph[T]) AddEdge

func (g *Graph[T]) AddEdge(a, b T)

AddEdge adds a and b to the vertex set and adds an edge between them. The edge is undirected, meaning that AddEdge(a,b) is equivalent to AddEdge(b,a). If the edge already exists, this is a no-op.

func (*Graph[T]) AddVertices

func (g *Graph[T]) AddVertices(t ...T)

AddVertices adds the given values as vertices. Values that already exist are ignored.

func (*Graph[T]) BFS added in v1.1.0

func (g *Graph[T]) BFS(start T) iter.Seq[T]

BFS iterates over this graph's nodes in a breadth-first ordering, including start.

func (*Graph[T]) ConnectedComponents

func (g *Graph[T]) ConnectedComponents() [][]T

ConnectedComponents returns a slice of connected components. In each component, the elements are ordered by order of addition to the graph. The components are ordered by the order of addition of their first elements.

func (*Graph[T]) DFS added in v1.1.0

func (g *Graph[T]) DFS(start T) iter.Seq[T]

DFS iterates over this graph's nodes in a depth-first ordering, including start.

func (*Graph[T]) DeleteEdge

func (g *Graph[T]) DeleteEdge(a, b T)

DeleteEdge removes the edge between a and b, while keeping them in the vertex set.

func (*Graph[T]) Edges

func (g *Graph[T]) Edges() iter.Seq2[T, T]

Edges iterates over current set of edges.

func (*Graph[T]) HasEdge

func (g *Graph[T]) HasEdge(a, b T) bool

HasEdge returns whether there is an edge between a and b.

func (*Graph[T]) NumEdges

func (g *Graph[T]) NumEdges() int

NumEdges returns the current number of edges.

func (*Graph[T]) NumVertices

func (g *Graph[T]) NumVertices() int

NumVertices returns the current number of vertices.

func (*Graph[T]) Vertices

func (g *Graph[T]) Vertices() iter.Seq[T]

Vertices iterates over current set of vertices, by order of addition to the graph.

Jump to

Keyboard shortcuts

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