graph

package
v2.0.0-...-8dcd6a7 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2015 License: BSD-2-Clause Imports: 1 Imported by: 4

Documentation

Overview

Package graph implements a simple graph data structure and supporting API to allow implementing graph alogirthms on top.

Example (Usage)

Creates a simple graph, and prints the degree of each vertex.

package main

import (
	"fmt"

	"gopkg.in/karalabe/cookiejar.v2/graph"
)

func main() {
	// Create a star shaped 5 vertex graph
	g := graph.New(5)
	g.Connect(0, 2)
	g.Connect(0, 3)
	g.Connect(1, 3)
	g.Connect(1, 4)
	g.Connect(2, 4)

	// For each vertex, count the outgoing edges
	for v := 0; v < g.Vertices(); v++ {
		degree := 0
		g.Do(v, func(peer interface{}) {
			degree++
		})
		fmt.Printf("%v: %v\n", v, degree)
	}
}
Output:

0: 2
1: 2
2: 2
3: 2
4: 2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

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

Data structure for representing a graph.

func New

func New(vertices int) *Graph

Creates a new undirected graph.

func (*Graph) Assign

func (g *Graph) Assign(id int, data interface{})

Assigns some data to a graph node.

func (*Graph) Connect

func (g *Graph) Connect(a, b int)

Connects two vertices of a graph (may be a loopback).

func (*Graph) Disconnect

func (g *Graph) Disconnect(a, b int)

Disconnects two vertices of a graph (may be a loopback).

func (*Graph) Do

func (g *Graph) Do(v int, f func(interface{}))

Executes a function for every neighbor of a vertex.

func (*Graph) Retrieve

func (g *Graph) Retrieve(id int) interface{}

Retrieves the data associated with a graph node.

func (*Graph) Vertices

func (g *Graph) Vertices() int

Returns the number of vertices in the graph.

Directories

Path Synopsis
Package bfs implements the breadth-first-search algorithm for the graphs.
Package bfs implements the breadth-first-search algorithm for the graphs.
Package dfs implements the depth-first-search algorithm for the graphs.
Package dfs implements the depth-first-search algorithm for the graphs.

Jump to

Keyboard shortcuts

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