Repository navigation
v0.10.0
This release adds iterator-based traversals, DFSEvents, FindCycle, Diff and the gographtest package. It also makes AddEdge on acyclic graphs, EdgesOf, InDegree, RemoveVertices, BellmanFord and FloydWarshall much faster. Every existing function and type keeps its signature.
go get github.com/hmdsefi/gograph@v0.10.0
Added
Vertex.AllNeighborsreturns an iterator over the neighbors of a vertex.
For a vertex of a graph, it yields the graph's own vertices without copying
them.traverse.BFS,BFSWithDepth,DFS,ClosestFirstandTopological
return traversals as sequences for arangeloop, in the order of the
matching iterators.BFSWithDepthgives each vertex with its number of
edges from the start, andClosestFirstwith its distance.VerticesandEdgesrange over all the vertices and edges of a graph. For
a graph fromNewthey allocate nothing: ranging over the edges of a graph
with 10,000 vertices and 50,000 edges takes 0.44 ms, whereAllEdgestakes
0.93 ms and 2 MiB.traverse.DFSEventsandDFSEventsAllgive the steps of a depth-first
search as a sequence: each vertex when the search discovers and finishes
it, and each edge as a tree, back, forward or cross edge, with its depth.
They keep their own stack, so a path of 100,000 vertices is safe to search,
and arangeloop can stop at any event.connectivity.FindCyclereturns the vertices of one cycle in order, such
as an import cycle to show in an error. It reports a cycle exactly when
HasCycledoes, in O(V+E) time.- The
gographtestpackage helps test code that uses gograph.
IsTopologicalOrderaccepts any valid topological order of a graph, and
SameGroupscompares groups of vertices, such as components or cliques, in
any order.Path,Cycle,Complete,Grid,RandomandRandomDAG
build graphs with the labels 0 to n-1.RandomandRandomDAGtake a
seeded*rand.Randand run in O(n + m) expected time for m edges: a random
directed graph with 10,000 vertices takes 9 ms, where a draw for each pair
takes 362 ms. Diffreturns the vertices, edges, weights and edge labels that changed
between two graphs, and whether they became weighted or acyclic. It matches
vertices by label, so the graphs can be built in any order.
GraphDiff.Stringprints one change per line, the diff encodes as JSON,
andApplymakes the same changes to a copy of a graph.
WithWeightTolerancetreats close weights as equal. On a graph with 10,000
vertices and 50,000 edges,Difftakes 3.2 ms and allocates only the diff
it returns.gographtest.Equalfails a test unless two graphs have the same vertices,
edges, weights, edge labels and properties, and lists what differs, one
change per line.
Changed
- Each vertex of a graph from
Newkeeps the edges that end at it.
InDegreereads that list in O(1),EdgesOftakes O(d log d) time for a
vertex with d edges, andRemoveVerticesvisits only the neighbors of each
removed vertex. On a graph with 2,000 vertices and 10,000 edges, calling
EdgesOfon every vertex takes 0.3 ms instead of 54 ms, and removing every
vertex takes 0.7 ms instead of 37 ms. - On a graph created with
Acyclic,AddEdgekeeps a topological order of
the vertices to check each new edge. An edge that agrees with the order, an
edge from a vertex with no incoming edges, and an edge to a vertex with no
outgoing edges go in at once. Any other edge searches around its
endpoints, in at most O(R log R) time for the R vertices and edges reachable
from its destination. Building a chain of 4,000 edges takes 1.1 ms instead
of 618 ms, and a random DAG with 2,000 vertices and 10,000 edges 1.8 ms
instead of 2.2 s. - The traversal iterators,
Dijkstra, the strongly connected component
functions, the dag helpers and the encoders read the stored neighbors
throughAllNeighbors. The breadth-first and depth-first iterators run
about 60% faster on a graph of 10,000 vertices and allocate 99% less
often. - A vertex takes 16 more bytes, and each edge one more pointer in the list of
its destination. An acyclic graph also keeps one int per vertex for its
order. Removing an edge takes time proportional to the in-degree of its
destination as well as the out-degree of its source. BellmanFordkeeps its distances in slices and stops after a pass that
changes none of them.FloydWarshallkeeps its distances in one flat
matrix and skips each row with no path to the middle vertex. On a random
graph with 10,000 vertices and 50,000 edges,BellmanFordtakes 2.7 ms
instead of 11.9 s, andFloydWarshallon 300 vertices takes 9 ms instead
of 1.66 s..agents/skills/gograph/SKILL.mdhas a row for each function added in this
release, and theconnectivity.HasCyclerow points toFindCycle. Eight
rows link to the gograph.dev pages added since v0.9.0.
Fixed
BellmanFordandFloydWarshallgive-Inffor a path through a-Inf
weight on a graph with no cycle, as v0.9.0 did. A cycle through a-Inf
weight, and a negative cycle that a-Infweight leads into, return
ErrNegativeWeightCycle.FloydWarshallreturnsErrNegativeWeightCycleonly for a negative cycle.
A check after the main loop added the weights in another order, and its
rounding reported a cycle on some graphs with none, such as the path
1 -> 3 -> 2 -> 0 with weights 3.3, 3.3 and 1.1.
Full Changelog: v0.9.1...v0.10.0