{"modulePath":"github.com/yourbasic/graph","version":"v0.0.0-20210606180040-8ecfec1c2869","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package graph\n\nPackage graph contains generic implementations of basic graph algorithms.\n\n### Generic graph algorithms {#hdr-Generic_graph_algorithms}\n\nThe algorithms in this library can be applied to any graph data structure implementing the two Iterator methods: Order, which returns the number of vertices, and Visit, which iterates over the neighbors of a vertex.\n\nAll algorithms operate on directed graphs with a fixed number of vertices, labeled from 0 to n-1, and edges with integer cost. An undirected edge {v, w} of cost c is represented by the two directed edges (v, w) and (w, v), both of cost c. A self-loop, an edge connecting a vertex to itself, is both directed and undirected.\n\n### Graph data structures {#hdr-Graph_data_structures}\n\nThe type Mutable represents a directed graph with a fixed number of vertices and weighted edges that can be added or removed. The implementation uses hash maps to associate each vertex in the graph with its adjacent vertices. This gives constant time performance for all basic operations.\n\nThe type Immutable is a compact representation of an immutable graph. The implementation uses lists to associate each vertex in the graph with its adjacent vertices. This makes for fast and predictable iteration: the Visit method produces its elements by reading from a fixed sorted precomputed list. This type supports multigraphs.\n\n### Virtual graphs {#hdr-Virtual_graphs}\n\nThe subpackage graph/build offers a tool for building virtual graphs. In a virtual graph no vertices or edges are stored in memory, they are instead computed as needed. New virtual graphs are constructed by composing and filtering a set of standard graphs, or by writing functions that describe the edges of a graph.\n\n### Tutorial {#hdr-Tutorial}\n\nThe Basics example shows how to build  a plain graph and how to efficiently use the Visit iterator, the key abstraction of this package.\n\nThe DFS example contains a full implementation of depth-first search.\n\n## Constants\n\n```go\nconst (\n\tMax int64 = 1\u003c\u003c63 - 1\n\tMin int64 = -1 \u003c\u003c 63\n)\n```\nThe maximum and minimum value of an edge cost.\n\n## Functions\n\n```go\nfunc Acyclic(g Iterator) bool\n```\nAcyclic tells if g has no cycles.\n\n```go\nfunc BFS(g Iterator, v int, do func(v, w int, c int64))\n```\nBFS traverses g in breadth-first order starting at v. When the algorithm follows an edge (v, w) and finds a previously unvisited vertex w, it calls do(v, w, c) with c equal to the cost of the edge (v, w).\n\n```go\nfunc Bipartition(g Iterator) (part []int, ok bool)\n```\nBipartition returns a subset U of g's vertices with the property that every edge of g connects a vertex in U to one outside of U. If g isn't bipartite, it returns an empty slice and sets ok to false.\n\n```go\nfunc Components(g Iterator) [][]int\n```\nComponents produces a partition of g's vertices into its (weakly) connected components.\n\n```go\nfunc Connected(g Iterator) bool\n```\nConnected tells if g has exactly one (weakly) connected component.\n\n```go\nfunc Equal(g, h Iterator) bool\n```\nEqual tells if g and h have the same number of vertices, and the same edges with the same costs.\n\n```go\nfunc EulerDirected(g Iterator) (walk []int, ok bool)\n```\nEulerDirected returns an Euler walk in a directed graph. If no such walk exists, it returns an empty walk and sets ok to false.\n\n```go\nfunc EulerUndirected(g Iterator) (walk []int, ok bool)\n```\nEulerUndirected returns an Euler walk following undirected edges in only one direction. If no such walk exists, it returns an empty walk and sets ok to false.\n\n```go\nfunc MST(g Iterator) (parent []int)\n```\nMST computes a minimum spanning tree for each connected component of an undirected weighted graph. The forest of spanning trees is returned as a slice of parent pointers: parent\\[v] is either the parent of v in a tree, or -1 if v is the root of a tree.\n\nThe time complexity is O(|E|⋅log|V|), where |E| is the number of edges and |V| the number of vertices in the graph.\n\n```go\nfunc ShortestPath(g Iterator, v, w int) (path []int, dist int64)\n```\nShortestPath computes a shortest path from v to w. Only edges with non-negative costs are included. The number dist is the length of the path, or -1 if w cannot be reached.\n\nThe time complexity is O((|E| + |V|)⋅log|V|), where |E| is the number of edges and |V| the number of vertices in the graph.\n\n```go\nfunc ShortestPaths(g Iterator, v int) (parent []int, dist []int64)\n```\nShortestPaths computes the shortest paths from v to all other vertices. Only edges with non-negative costs are included. The number parent\\[w] is the predecessor of w on a shortest path from v to w, or -1 if none exists. The number dist\\[w] equals the length of a shortest path from v to w, or is -1 if w cannot be reached.\n\nThe time complexity is O((|E| + |V|)⋅log|V|), where |E| is the number of edges and |V| the number of vertices in the graph.\n\n```go\nfunc String(g Iterator) string\n```\nString returns a description of g with two elements: the number of vertices, followed by a sorted list of all edges.\n\n```go\nfunc StrongComponents(g Iterator) [][]int\n```\nStrongComponents produces a partition of g's vertices into its strongly connected components.\n\nA component is strongly connected if all its vertices are reachable from every other vertex in the component. Each vertex of the graph appears in exactly one of the strongly connected components, and any vertex that is not on a directed cycle forms a strongly connected component all by itself.\n\n```go\nfunc TopSort(g Iterator) (order []int, ok bool)\n```\nTopSort returns a topological ordering of the vertices in a directed acyclic graph; if the graph is not acyclic, no such ordering exists and ok is set to false.\n\nIn a topological order v comes before w for every directed edge from v to w.\n\n## Types\n\n```go\ntype Immutable struct {\n\t// contains filtered or unexported fields\n}\n```\nImmutable is a compact representation of an immutable graph. The implementation uses lists to associate each vertex in the graph with its adjacent vertices. This makes for fast and predictable iteration: the Visit method produces its elements by reading from a fixed sorted precomputed list. This type supports multigraphs.\n\n```go\nfunc Sort(g Iterator) *Immutable\n```\nSort returns an immutable copy of g with a Visit method that returns its neighbors in increasing numerical order.\n\n```go\nfunc Transpose(g Iterator) *Immutable\n```\nTranspose returns the transpose graph of g. The transpose graph has the same set of vertices as g, but all of the edges are reversed compared to the orientation of the corresponding edges in g.\n\n```go\nfunc (g *Immutable) Degree(v int) int\n```\nDegree returns the number of outward directed edges from v.\n\n```go\nfunc (g *Immutable) Edge(v, w int) bool\n```\nEdge tells if there is an edge from v to w.\n\n```go\nfunc (g *Immutable) Order() int\n```\nOrder returns the number of vertices in the graph.\n\n```go\nfunc (g *Immutable) String() string\n```\nString returns a string representation of the graph.\n\n```go\nfunc (g *Immutable) Visit(v int, do func(w int, c int64) bool) bool\n```\nVisit calls the do function for each neighbor w of v, with c equal to the cost of the edge from v to w. The neighbors are visited in increasing numerical order. If do returns true, Visit returns immediately, skipping any remaining neighbors, and returns true.\n\n```go\nfunc (g *Immutable) VisitFrom(v int, a int, do func(w int, c int64) bool) bool\n```\nVisitFrom calls the do function starting from the first neighbor w for which w ≥ a, with c equal to the cost of the edge from v to w. The neighbors are then visited in increasing numerical order. If do returns true, VisitFrom returns immediately, skipping any remaining neighbors, and returns true.\n\n```go\ntype Iterator interface {\n\t// Order returns the number of vertices in a graph.\n\tOrder() int\n\n\t// Visit calls the do function for each neighbor w of vertex v,\n\t// with c equal to the cost of the edge from v to w.\n\t//\n\t//  • If do returns true, Visit returns immediately, skipping\n\t//    any remaining neighbors, and returns true.\n\t//\n\t//  • The calls to the do function may occur in any order,\n\t//    and the order may vary.\n\t//\n\tVisit(v int, do func(w int, c int64) (skip bool)) (aborted bool)\n}\n```\nIterator describes a weighted graph; an Iterator can be used to describe both ordinary graphs and multigraphs.\n\n```go\nfunc MaxFlow(g Iterator, s, t int) (flow int64, graph Iterator)\n```\nMaxFlow computes a maximum flow from s to t in a graph with nonnegative edge capacities. The time complexity is O(|E|²⋅|V|), where |E| is the number of edges and |V| the number of vertices in the graph.\n\n```go\ntype Mutable struct {\n\t// contains filtered or unexported fields\n}\n```\nMutable represents a directed graph with a fixed number of vertices and weighted edges that can be added or removed. The implementation uses hash maps to associate each vertex in the graph with its adjacent vertices. This gives constant time performance for all basic operations.\n\n```go\nfunc Copy(g Iterator) *Mutable\n```\nCopy returns a copy of g. If g is a multigraph, any duplicate edges in g will be lost.\n\n```go\nfunc New(n int) *Mutable\n```\nNew constructs a new graph with n vertices, numbered from 0 to n-1, and no edges.\n\n```go\nfunc (g *Mutable) Add(v, w int)\n```\nAdd inserts a directed edge from v to w with zero cost. It removes the previous cost if this edge already exists.\n\n```go\nfunc (g *Mutable) AddBoth(v, w int)\n```\nAddBoth inserts edges with zero cost between v and w. It removes the previous costs if these edges already exist.\n\n```go\nfunc (g *Mutable) AddBothCost(v, w int, c int64)\n```\nAddBothCost inserts edges with cost c between v and w. It overwrites the previous costs if these edges already exist.\n\n```go\nfunc (g *Mutable) AddCost(v, w int, c int64)\n```\nAddCost inserts a directed edge from v to w with cost c. It overwrites the previous cost if this edge already exists.\n\n```go\nfunc (g *Mutable) Cost(v, w int) int64\n```\nCost returns the cost of an edge from v to w, or 0 if no such edge exists.\n\n```go\nfunc (g *Mutable) Degree(v int) int\n```\nDegree returns the number of outward directed edges from v.\n\n```go\nfunc (g *Mutable) Delete(v, w int)\n```\nDelete removes an edge from v to w.\n\n```go\nfunc (g *Mutable) DeleteBoth(v, w int)\n```\nDeleteBoth removes all edges between v and w.\n\n```go\nfunc (g *Mutable) Edge(v, w int) bool\n```\nEdge tells if there is an edge from v to w.\n\n```go\nfunc (g *Mutable) Order() int\n```\nOrder returns the number of vertices in the graph.\n\n```go\nfunc (g *Mutable) String() string\n```\nString returns a string representation of the graph.\n\n```go\nfunc (g *Mutable) Visit(v int, do func(w int, c int64) bool) bool\n```\nVisit calls the do function for each neighbor w of v, with c equal to the cost of the edge from v to w. If do returns true, Visit returns immediately, skipping any remaining neighbors, and returns true.\n\nThe iteration order is not specified and is not guaranteed to be the same every time. It is safe to delete, but not to add, edges adjacent to v during a call to this method.\n\n```go\ntype Stats struct {\n\tSize     int // Number of unique edges.\n\tMulti    int // Number of duplicate edges.\n\tWeighted int // Number of edges with non-zero cost.\n\tLoops    int // Number of self-loops.\n\tIsolated int // Number of vertices with outdegree zero.\n}\n```\nStats holds basic data about an Iterator.\n\n```go\nfunc Check(g Iterator) Stats\n```\nCheck collects data about an Iterator.\n\n","path":"github.com/yourbasic/graph","name":"graph","synopsis":"Package graph contains generic implementations of basic graph algorithms.","isRedistributable":true}
