bfs

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: 3 Imported by: 0

Documentation

Overview

Package bfs implements the breadth-first-search algorithm for the graphs.

The BFS is implemented using on demand calculations, meaning that only that part of the search space will be expanded as requested, iteratively expanding it if needed.

Neighbor traversal order currently is random due to the graph implementation. Specific order may be added in the future.

Example (Usage)

Small API demo based on a trie graph and a few disconnected vertices.

package main

import (
	"fmt"

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

func main() {
	// Create the graph
	g := graph.New(7)
	g.Connect(0, 1)
	g.Connect(1, 2)
	g.Connect(1, 4)
	g.Connect(2, 3)
	g.Connect(4, 5)

	// Create the breadth first search algo structure for g and source node #2
	b := bfs.New(g, 0)

	// Get the path between #0 (source) and #2
	fmt.Println("Path 0->5:", b.Path(5))
	fmt.Println("Order:", b.Order())
	fmt.Println("Reachable #4 #6:", b.Reachable(4), b.Reachable(6))

}
Output:

Path 0->5: [0 1 4 5]
Order: [0 1 2 4 3 5]
Reachable #4 #6: true false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bfs

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

Breadth-first-search algorithm data structure.

func New

func New(g *graph.Graph, src int) *Bfs

Creates a new random-order bfs structure.

func (*Bfs) Order

func (d *Bfs) Order() []int

Generates the full order in which nodes were traversed.

func (*Bfs) Path

func (d *Bfs) Path(dst int) []int

Generates the path from the source node to the destination.

func (*Bfs) Reachable

func (d *Bfs) Reachable(dst int) bool

Checks whether a given vertex is reachable from the source.

Jump to

Keyboard shortcuts

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