graphlib

package module
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2025 License: MIT Imports: 6 Imported by: 2

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 BidirectionalEdgeErr added in v2.3.0

type BidirectionalEdgeErr struct {
	Src, Tgt string
}

func (BidirectionalEdgeErr) Error added in v2.3.0

func (e BidirectionalEdgeErr) Error() string

type CycleErr added in v2.3.0

type CycleErr struct {
	Src string
	Tgt string
}

func (CycleErr) Error added in v2.3.0

func (e CycleErr) Error() string

type Edge

type Edge struct {
	Key    string
	Source string
	Target string
}

type Graph

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

func NewSoAGraph added in v2.3.0

func NewSoAGraph(logger *slog.Logger) *Graph

func (*Graph) AddEdge added in v2.2.0

func (g *Graph) AddEdge(src, tgt string) error

func (*Graph) AddVertex added in v2.2.0

func (g *Graph) AddVertex(key string, label string, class string, healthy bool)

func (*Graph) ClearHealthyStatus added in v2.3.0

func (g *Graph) ClearHealthyStatus()

func (*Graph) GetVertex

func (g *Graph) GetVertex(key string) (Vertex, error)

func (*Graph) Path

func (g *Graph) Path(srcKey, tgtKey string) (Subgraph, error)

func (*Graph) SetVertexHealth

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

func (*Graph) StartHealthCheckLoop

func (g *Graph) StartHealthCheckLoop(ctx context.Context, checkInterval time.Duration)

func (*Graph) Stats added in v2.3.0

func (g *Graph) Stats() Stats

func (*Graph) VertexDependencies

func (g *Graph) VertexDependencies(key string, all bool) (Subgraph, error)

func (*Graph) VertexDependents

func (g *Graph) VertexDependents(key string, all bool) (Subgraph, error)

func (*Graph) VertexNeighbors

func (g *Graph) VertexNeighbors(key string) (Subgraph, error)

type Stats

type Stats struct {
	TotalVertices          int
	TotalUnhealthyVertices int
	TotalEdges             int
	TotalHealthyVertices   int

	UnhealthyVertices []Vertex
}

type Subgraph

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

type Vertex

type Vertex struct {
	Key       string
	Label     string
	Class     string
	Healthy   bool
	LastCheck int64
}

type VertexNotFoundErr added in v2.3.0

type VertexNotFoundErr struct {
	Key string
}

func (VertexNotFoundErr) Error added in v2.3.0

func (e VertexNotFoundErr) Error() string

type VertexPathErr added in v2.3.0

type VertexPathErr struct {
	Src string
	Dst string
}

func (VertexPathErr) Error added in v2.3.0

func (e VertexPathErr) Error() string

Jump to

Keyboard shortcuts

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