graphlib

package module
v2.2.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: 3 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 Edge

type Edge = core.Edge

type Graph

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

func NewGraph

func NewGraph() *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, label string, healthy bool)

func (*Graph) ClearGraphHealthyStatus

func (g *Graph) ClearGraphHealthyStatus()

func (*Graph) GetVertex

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

func (*Graph) GraphStats

func (g *Graph) GraphStats() Stats

func (*Graph) Path

func (g *Graph) Path(src, tgt 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, check time.Duration)

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 = core.Stats

type Subgraph

type Subgraph = core.Subgraph

type Vertex

type Vertex = core.Vertex

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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