graphs

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeSCC

func ComputeSCC[T comparable](graph Graph[T]) [][]T

ComputeSCC computes strongly connected components of a graph and returns them.

func Example

func Example()

Example runs a strongly connected algorithm on a simple graph

Types

type DFSWalk

type DFSWalk[T comparable] struct {
	// contains filtered or unexported fields
}

DFSWalk persists the state of the dfs state so that we can use it computing components

func (*DFSWalk[T]) Explore

func (d *DFSWalk[T]) Explore(node T) []T

Explore walks the dfs tree root at node and returns all the nodes reachable from node including itself

func (*DFSWalk[T]) ExploreGraph

func (d *DFSWalk[T]) ExploreGraph(node T) []NodeStatus[T]

ExploreGraph traverses the entire graph starting at node. Returned NodeStatus contains newly finished nodes at the end of the slice, so traversing the slice in reverse order gives the nodes in the decreasing order of finish time

type Edge

type Edge[T comparable] struct {
	Src, Dst T
}

type Graph

type Graph[T comparable] struct {
	// contains filtered or unexported fields
}

Graph We don't really need a generic type but an int would suffice as node id and let caller keep the mapping. Created a generic parameter so that I provide a string for demonstration.

func CreateGraph

func CreateGraph[T comparable](edges []Edge[T], additionalNodes ...T) Graph[T]

CreateGraph addtionalNodes are for nodes which are isolated (no incoming and outgoing rawEdges)

func (*Graph[T]) Neighbours

func (g *Graph[T]) Neighbours(node T) []T

func (*Graph[T]) NodeCount

func (g *Graph[T]) NodeCount() int

func (*Graph[T]) Nodes

func (g *Graph[T]) Nodes() []T

func (*Graph[T]) Transpose

func (g *Graph[T]) Transpose() Graph[T]

Transpose reverses the direction of rawEdges

type NodeStatus

type NodeStatus[T comparable] struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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