ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - hmdsefi/gograph: A golang generic graph library that provides mathematical graph-theory and algorithms.
A golang generic graph library that provides mathematical graph-theory and algorithms. - hmdsefi/gograph
Visit Site

GitHub - hmdsefi/gograph: A golang generic graph library that provides mathematical graph-theory and algorithms.

GitHub - hmdsefi/gograph: A golang generic graph library that provides mathematical graph-theory and algorithms.

build Go Report Card codecov Go Reference Mentioned in Awesome Go

gograph

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 := New[int](gograph.Directed())

graph.AddEdge(gograph.NewVertex(1), gograph.NewVertex(2))
graph.AddEdge(gograph.NewVertex(1), gograph.NewVertex(3))
graph.AddEdge(gograph.NewVertex(2), gograph.NewVertex(2))
graph.AddEdge(gograph.NewVertex(3), gograph.NewVertex(4))
graph.AddEdge(gograph.NewVertex(4), gograph.NewVertex(5))
graph.AddEdge(gograph.NewVertex(5), gograph.NewVertex(6))

Acyclic

acyclic-graph

graph := New[int](gograph.Directed())

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

Undirected

undirected-graph

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

graph.AddEdge(gograph.NewVertex("A"), gograph.NewVertex("B"))
graph.AddEdge(gograph.NewVertex("A"), gograph.NewVertex("D"))
graph.AddEdge(gograph.NewVertex("B"), gograph.NewVertex("C"))
graph.AddEdge(gograph.NewVertex("B"), gograph.NewVertex("D"))

Weighted

weighted-edge

graph := New[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 := New[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.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory