graph

package
v0.0.0-...-353d915 Latest Latest
Warning

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

Go to latest
Published: May 5, 2018 License: MIT Imports: 7 Imported by: 0

README

graph

Build Status Coverage Status

グラフ理論のアルゴリズム向けのデータ構造をGolangで実装したものになります。

How to Install

$ go get github.com/cipepser/goGraphAlgo/...

可視化もできるようにしているので、可視化したい場合は以下もインストールしてください。

$ brew install graphviz
$ go get github.com/awalterschulze/gographviz

How to Use

基本的な操作として、以下ができます。

  • 無向グラフ/有向グラフ
  • 頂点(Vertex)の追加/削除
  • 辺(Edge)の追加/削除

具体的な使い方を以下に示します。 いずれもerrorを返すようにしているのて適宜エラーチェックをしてください。

無向グラフの作成
g := graph.NewGraph()

※内部でisDirectedフィールドを持っていますが、ゼロ値がfalseのためデフォルトで無向グラフになります。

有向グラフの作成
g := graph.NewGraph()
g.SetDir(true)
頂点(Vertex)の追加/削除
g.AddVertex(0) // vertex 0を追加
g.AddVertex(1) // vertex 1を追加
g.AddVertex(2) // vertex 2を追加

g.RemoveVertex(2) // vertex 2を削除
g.RemoveVertex(1) // vertex 1を削除
辺(Edge)の追加/削除

AddEdgeの第3引数はedgeの重み(int)になります。

// edgeを追加する前にvertexの追加が必要
g.AddVertex(0)
g.AddVertex(1)

g.AddEdge(0, 1, 3) // 0→1に重み3のedgeを追加

g.RemoveEdge(0, 1) // 0→1のedgeを削除
可視化
g.Visualize()

Example

簡単な有向グラフを作成し、可視化してみます。

package main

import (
	"github.com/cipepser/goGraphAlgo/graph"
)

func main() {

	g := graph.NewGraph()
	g.SetDir(true)

	g.AddVertex(0)
	g.AddVertex(1)
	g.AddVertex(2)
	g.AddVertex(3)

	g.AddEdge(0, 1, 0)
	g.AddEdge(0, 2, 0)
	g.AddEdge(0, 3, 0)
	g.AddEdge(2, 3, 0)

	if err := g.Visualize(); err != nil {
		panic(err)
	}

}

上記をmain.goとして実行(go run main.go)するとgv.dotファイルが生成されます。 これを以下のようにgraphgizでpngにします。

$ go run main.go
$ dot -T png ./gv.dot -o ./gv.png
$ open ./gv.png

結果

References

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisjointSetAlgorithm

func DisjointSetAlgorithm(g *graph) bool

DisjointSetAlgorithm check whether a cycle exists or not.

func NewGraph

func NewGraph() *graph

NewGraph constructs a new graph

Types

type DisjointSet

type DisjointSet []VerSet

DisjointSet is a type consist of VerSets, each of the VerSet is disjoint.

func (DisjointSet) Equal

func (d DisjointSet) Equal(other DisjointSet) bool

Equal checks whether d is same as other or not.

func (DisjointSet) FindSet

func (d DisjointSet) FindSet(e Edge) (F, T VerSet)

FindSet returns VerSets. Each VerSet contains the Vertex represented by `e.From` and `e.To`.

func (DisjointSet) Union

func (d DisjointSet) Union(A, B VerSet) DisjointSet

Union unions 2 VerSets to one.

type Edge

type Edge struct {
	From Vertex
	To   Vertex
}

Edge represents a edge connects a vertex with another of the graph

type VerSet

type VerSet map[Vertex]struct{}

VerSet represents a set of vertices.

func NewVerSet

func NewVerSet() VerSet

NewVerSet constructs a new VerSet.

func (VerSet) Add

func (s VerSet) Add(v Vertex) error

Add adds an `v` to `s`.

func (VerSet) Cardinality

func (s VerSet) Cardinality() int

Cardinality returns the number of elements in the Set.

func (VerSet) Contains

func (s VerSet) Contains(v Vertex) bool

Contains checks whether an `v` exists in `s` or not.

func (VerSet) Difference

func (s VerSet) Difference(other VerSet) VerSet

Difference returns the difference of `s` and `other`. Difference have a referential transparency.

func (VerSet) Equal

func (s VerSet) Equal(other VerSet) bool

Equal checks whether `other` is same as `s` or not.

func (VerSet) Intersect

func (s VerSet) Intersect(other VerSet) VerSet

Intersect returns the Intersection of `s` and `other`. Intersect have a referential transparency.

func (VerSet) Remove

func (s VerSet) Remove(v Vertex) error

Remove removes an `v` from `s`.

func (VerSet) Union

func (s VerSet) Union(other VerSet) VerSet

Union returns the Union of `s` and `other` Union have a referential transparency.

type Vertex

type Vertex uint

Vertex represents a vertex of the graph

Jump to

Keyboard shortcuts

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