Skip to content

仓库 files navigation

build Go Report Card codecov Go Reference Mentioned in Awesome Go

golang generic graph package

GoGraph



GoGraph is a lightweight, efficient, and easy-to-use graph data structure implementation written in Go. It provides a versatile framework for representing graphs and performing various operations on them, making it ideal for both educational purposes and practical applications.




Table of Contents

Install

Use go get command to get the latest version of the gograph:

go get github.com/hmdsefi/gograph

Then you can use import the gograph to your code:

package main

import "github.com/hmdsefi/gograph"

How to Use

Graph

gograph contains the Graph[T comparable] interface that provides all needed APIs to manage a graph. All the supported graph types in gograph library implemented this interface.

type Graph[T comparable] interface {
GraphType

AddEdge(from, to *Vertex[T], options ...EdgeOptionFunc) (*Edge[T], error)
GetAllEdges(from, to *Vertex[T]) []*Edge[T]
GetEdge(from, to *Vertex[T]) *Edge[T]
EdgesOf(v *Vertex[T]) []*Edge[T]
RemoveEdges(edges ...*Edge[T])
AddVertexByLabel(label T, options ...VertexOptionFunc) *Vertex[T]
AddVertex(v *Vertex[T])
GetVertexByID(label T) *Vertex[T]
GetAllVerticesByID(label ...T) []*Vertex[T]
GetAllVertices() []*Vertex[T]
RemoveVertices(vertices ...*Vertex[T])
ContainsEdge(from, to *Vertex[T]) bool
ContainsVertex(v *Vertex[T]) bool
}

The generic type of the T in Graph interface represents the vertex label. The type of T should be comparable. You cannot use slices and function types for T.

Directed

directed-graph

graph := 新建[int](gograph.Directed())

graph.AddEdge(gograph.新建Vertex(1), gograph.新建Vertex(2))
graph.AddEdge(gograph.新建Vertex(1), gograph.新建Vertex(3))
graph.AddEdge(gograph.新建Vertex(2), gograph.新建Vertex(2))
graph.AddEdge(gograph.新建Vertex(3), gograph.新建Vertex(4))
graph.AddEdge(gograph.新建Vertex(4), gograph.新建Vertex(5))
graph.AddEdge(gograph.新建Vertex(5), gograph.新建Vertex(6))

Acyclic

acyclic-graph

graph := 新建[int](gograph.Acyclic())

graph.AddEdge(gograph.新建Vertex(1), gograph.新建Vertex(2))
graph.AddEdge(gograph.新建Vertex(2), gograph.新建Vertex(3))
_, err := graph.AddEdge(gograph.新建Vertex(3), gograph.新建Vertex(1))
if err != nil {
// do something
}

Undirected

undirected-graph

// by default graph is undirected
graph := 新建[string]()

graph.AddEdge(gograph.新建Vertex("A"), gograph.新建Vertex("B"))
graph.AddEdge(gograph.新建Vertex("A"), gograph.新建Vertex("D"))
graph.AddEdge(gograph.新建Vertex("B"), gograph.新建Vertex("C"))
graph.AddEdge(gograph.新建Vertex("B"), gograph.新建Vertex("D"))

Weighted

weighted-edge

graph := 新建[string]()

vA := gograph.AddVertexByLabel("A")
vB := gograph.AddVertexByLabel("B")
vC := gograph.AddVertexByLabel("C")
vD := gograph.AddVertexByLabel("D")

graph.AddEdge(vA, vB, gograph.WithEdgeWeight(4))
graph.AddEdge(vA, vD, gograph.WithEdgeWeight(3))
graph.AddEdge(vB, vC, gograph.WithEdgeWeight(3))
graph.AddEdge(vB, vD, gograph.WithEdgeWeight(1))
graph.AddEdge(vC, vD, gograph.WithEdgeWeight(2))

weighted-vertex

graph := 新建[string]()
vA := gograph.AddVertexByLabel("A", gograph.WithVertexWeight(3))
vB := gograph.AddVertexByLabel("B", gograph.WithVertexWeight(2))
vC := gograph.AddVertexByLabel("C", gograph.WithVertexWeight(4))

graph.AddEdge(vA, vB)
graph.AddEdge(vB, vC)

Traverse

Traverse package provides the iterator interface that guarantees all the algorithm export the same APIs:

type Iterator[T comparable] interface {
	HasNext() bool
	Next() *gograph.Vertex[T]
	Iterate(func(v *gograph.Vertex[T]) error) error
	Reset()
}

This package contains the following iterators:

License

Apache License, please see LICENSE for details.

关于

A golang generic graph library that provides mathematical graph-theory and algorithms.

Topics

Resources

Stars

126 stars

关注者

3 watching

复刻s

发布

Used by

贡献者

Languages