Graph algorithms that do not specify a particular approach for representing a Graph.
Each algorithm is a top level function which takes callback arguments that provide the mechanism for traversing the graph. For example, two common approaches for representing a graph:
class AdjacencyListGraph<T> { Map<T, List<T>> nodes; // ... }
class TreeGraph<T> { Node<T> root; // ... } class Node<T> { List<Node<T>> edges; T value; }
Any representation can be adapted to the callback arguments.
edges
callback which provides the immediate neighbors of a given node.equals
and/or hashCode
callbacks if the unique data type does not correctly or efficiently implement operator==
or get hashCode
.Algorithms that support graphs which are resolved asynchronously will have similar callbacks which return FutureOr
.
import 'package:graphs/graphs.dart'; void sendMessage() { final network = AdjacencyListGraph(); // ... final route = shortestPath( sender, receiver, (node) => network.nodes[node] ?? const []); } void resolveBuildOrder() { final dependencies = TreeGraph(); // ... final buildOrder = topologicalSort([dependencies.root], (node) => node.edges); }