graphlib

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 5 Imported by: 1

README

📦 graphlib

graphlib é uma biblioteca Go para representar e manipular grafos direcionados acíclicos (DAGs) de forma simples, segura e concorrente. Ela permite criar vértices e arestas, navegar pelas relações de dependência, explorar caminhos e extrair subgrafos. A biblioteca é dividida em dois níveis:

  • core.go: lógica interna (não exportada)
  • graph.go: API pública, segura para concorrência

✨ Features

  • Criação de vértices e arestas com controle de ciclo
  • Suporte a dependentes e dependências
  • Caminhos entre vértices (Path)
  • Subgrafos com vizinhos, dependentes, dependências
  • Thread-safe via sync.RWMutex

📦 Instalação

go get github.com/opsminded/graphlib@latest

🧑‍💻 Exemplo de uso

package main

import (
	"fmt"
	"github.com/opsminded/graphlib"
)

func main() {
	g := graphlib.NewGraph()

	g.NewVertex("A")
	g.NewVertex("B")
	g.NewVertex("C")
	g.NewVertex("D")

	g.NewEdge("A->B", "A", "B")
	g.NewEdge("B->C", "B", "C")
	g.NewEdge("A->D", "A", "D")

	// Neighbors
	sub := g.Neighbors("A")
	fmt.Println("Vizinhos de A:", sub.Vertices)

	// Caminho de A para C
	path := g.Path("A", "C")
	fmt.Println("Caminho A -> C:", path.Vertices)
}

🚧 Restrições

  • Arestas bidirecionais entre dois nós não são permitidas
  • Ciclos não são permitidos (DAG)
  • O nome dos vértices (Label) deve ser único

✅ Testes

  • A biblioteca inclui testes unitários para:
  • Neighbors
  • Dependents/Dependencies (diretos e recursivos)
  • Path (com múltiplos caminhos)
  • Subgrafos

Você pode executar os testes clonando o repositório:

git clone https://github.com/opsminded/graphlib.git
go test ./...

📄 Licença

Licença MIT

MIT License

Copyright (c) 2025 opsminded

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BidirectionalEdgeError added in v1.4.0

type BidirectionalEdgeError struct {
	LabelA string
	LabelB string
}

func (BidirectionalEdgeError) Error added in v1.4.0

func (e BidirectionalEdgeError) Error() string

type CycleError added in v1.4.0

type CycleError struct {
	LabelA string
	LabelB string
}

func (CycleError) Error added in v1.4.0

func (e CycleError) Error() string

type Edge

type Edge struct {
	Label       string
	Source      Vertex
	Destination Vertex
}

type EdgeNotFoundError added in v1.4.0

type EdgeNotFoundError struct {
	LabelA string
	LabelB string
}

func (EdgeNotFoundError) Error added in v1.4.0

func (e EdgeNotFoundError) Error() string

type Graph

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

func NewGraph

func NewGraph() *Graph

func (*Graph) EdgeLen

func (g *Graph) EdgeLen() int

func (*Graph) GetVertexByLabel

func (g *Graph) GetVertexByLabel(label string) (Vertex, error)

func (*Graph) GetVertexDependencies

func (g *Graph) GetVertexDependencies(label string, all bool) (Subgraph, error)

func (*Graph) GetVertexDependents

func (g *Graph) GetVertexDependents(label string, all bool) (Subgraph, error)

func (*Graph) Neighbors

func (g *Graph) Neighbors(label string) (Subgraph, error)

func (*Graph) NewEdge

func (g *Graph) NewEdge(label, a, b string) error

func (*Graph) NewVertex

func (g *Graph) NewVertex(label string)

func (*Graph) Path

func (g *Graph) Path(from, to string) (Subgraph, error)

func (*Graph) SetVertexHealth added in v1.2.0

func (g *Graph) SetVertexHealth(label string, health bool) error

func (*Graph) UnhealthyNodes added in v1.5.0

func (g *Graph) UnhealthyNodes() []Vertex

func (*Graph) VertexLen

func (g *Graph) VertexLen() int

type Subgraph

type Subgraph struct {
	Vertices []Vertex
	Edges    []Edge
}

type Vertex

type Vertex struct {
	Label  string
	Health bool
}

type VertexNilError added in v1.4.0

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

func (VertexNilError) Error added in v1.4.0

func (e VertexNilError) Error() string

type VertexNotFoundError added in v1.4.0

type VertexNotFoundError struct {
	Label string
}

func (VertexNotFoundError) Error added in v1.4.0

func (e VertexNotFoundError) Error() string

Jump to

Keyboard shortcuts

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