# README
Simple Golang library for graph processing
Introduction
This library provides simple API (data structures and set of algorithms) to use in programs that run algorithms on graphs.
Sample Usage
package main
// This is a sample usage of topological sort algorithm
import (
"fmt"
"os"
"github.com/baburkin/graphs"
)
func main() {
// Initialize a directed graph from a file
graph, err := graphs.LoadDirectedGraph("examples/directed_7.txt")
if err != nil {
fmt.Printf("Got an error: %v\n", err)
os.Exit(2)
}
fmt.Printf("Initial graph: %v\n", graph)
// Get a reversed graph
reversedGraph := graph.Reverse()
fmt.Printf("Reversed graph: %v\n", reversedGraph)
// Try to topologically sort the graph
if order, err := graphs.TopoSort(graph); err != nil {
fmt.Printf("Cannot do topological sort: %v\n", err)
} else {
fmt.Printf("Topologically sorted graph (vertex order): %v\n", order)
}
// Try to topologically sort the reversed graph
if order, err := graphs.TopoSort(reversedGraph); err != nil {
fmt.Printf("Cannot do topological sort for reversed graph: %v\n", err)
} else {
fmt.Printf("Topologically sorted reversed graph (vertex order): %v\n", order)
}
}
# Packages
No description provided by the author
# Functions
DFSPostOrder provides the post-DFS order of vertices.
DFSReversePostOrder provides the reverse post-DFS order.
GetCycleInGraph returns the list of vertices that form a cycle in a directed graph.
InitConnectedComponents searches the given graph for connected components and returns arrays c []int and s []int, where the value c[v] is an id of a component, to which the given vertex 'v' belongs, and s[i] is the size of component with id 'i'.
InitDirectedGraph initializes a new instance of DirectedGraph with a given number of vertices.
InitGraph initializes a new instance of Graph with a given number of vertices.
InitGraphFromGraph initializes a new instance of undirected graph from a given instance of graph by copying all edges.
IsDAG determines if the directed graph is a DAG (directed acyclic graph).
LoadDirectedGraph initializes a new instance of DirectedGraph from a file.
LoadDirectedGraphFromReader initializes a new instance of DirectedGraph from any implementor of io.Reader interface.
Rank shows the rank of the vertex if topological sort exists, -1 otherwise.
ShortestPathBFS returns the number of step that form the shortest path between `vSource` and `vTarget` vertices:
vSource -> v1 -> v2 -> ..
TopoSort sorts the directed graph in topological order and returns the ordered slice of vertices.
TopoSortReverse returns the slice of vertices ordered reversely to topological order.
# Interfaces
ConnectedComponents interface provides the API for getting the number of connected components in a graph.
DirectedEdge provides a special kind of edge which has direction.
DirectedGraph interface provides API to work with directed graphs.
EdgeIterator interface provides a way to.
Graph interface provides API to work with undirected graphs The edges are unweighted.
Vertex type is a generic type for graph's vertices.
VertexIterator type is a generic iterable collection.