|  | 
| 5 | 5 | Graph algorithms that do not specify a particular approach for representing a | 
| 6 | 6 | Graph. | 
| 7 | 7 | 
 | 
| 8 |  | -Functions in this package will take arguments that provide the mechanism for | 
| 9 |  | -traversing the graph. For example two common approaches for representing a | 
| 10 |  | -graph: | 
|  | 8 | +Each algorithm is a top level function which takes callback arguments that | 
|  | 9 | +provide the mechanism for traversing the graph. For example, two common | 
|  | 10 | +approaches for representing a graph: | 
| 11 | 11 | 
 | 
| 12 | 12 | ```dart | 
| 13 |  | -class Graph { | 
| 14 |  | -  Map<Node, List<Node>> nodes; | 
| 15 |  | -} | 
| 16 |  | -class Node { | 
| 17 |  | -  // Interesting data | 
|  | 13 | +class AdjacencyListGraph<T> { | 
|  | 14 | +  Map<T, List<T>> nodes; | 
|  | 15 | +  // ... | 
| 18 | 16 | } | 
| 19 | 17 | ``` | 
| 20 | 18 | 
 | 
| 21 | 19 | ```dart | 
| 22 |  | -class Graph { | 
| 23 |  | -  Node root; | 
|  | 20 | +class TreeGraph<T> { | 
|  | 21 | +  Node<T> root; | 
|  | 22 | +  // ... | 
| 24 | 23 | } | 
| 25 |  | -class Node { | 
| 26 |  | -  List<Node> edges; | 
| 27 |  | -  // Interesting data | 
|  | 24 | +class Node<T> { | 
|  | 25 | +  List<Node<T>> edges; | 
|  | 26 | +  T value; | 
| 28 | 27 | } | 
| 29 | 28 | ``` | 
| 30 | 29 | 
 | 
| 31 |  | -Any representation can be adapted to the needs of the algorithm: | 
|  | 30 | +Any representation can be adapted to the callback arguments. | 
|  | 31 | + | 
|  | 32 | +- Algorithms which need to traverse the graph take an `edges` callback which | 
|  | 33 | +  provides the immediate neighbors of a given node. | 
|  | 34 | +- Algorithms which need to associate unique data with each node in the graph | 
|  | 35 | +  allow passing `equals` and/or `hashCode` callbacks if the unique data type | 
|  | 36 | +  does not correctly or efficiently implement `operator==` or `get hashCode`. | 
|  | 37 | + | 
|  | 38 | + | 
|  | 39 | +Algorithms that support graphs which are resolved asynchronously will have | 
|  | 40 | +similar callbacks which return `FutureOr`. | 
| 32 | 41 | 
 | 
| 33 |  | -- Some algorithms need to associate data with each node in the graph. If the | 
| 34 |  | -  node type `T` does not correctly or efficiently implement `hashCode` or `==`, | 
| 35 |  | -  you may provide optional `equals` and/or `hashCode` functions are parameters. | 
| 36 |  | -- Algorithms which need to traverse the graph take a `edges` function which provides the reachable nodes. | 
| 37 |  | -  - `(node) => graph[node]` | 
| 38 |  | -  - `(node) => node.edges` | 
|  | 42 | +```dart | 
|  | 43 | +import 'package:graphs/graphs.dart'; | 
| 39 | 44 | 
 | 
|  | 45 | +void sendMessage() { | 
|  | 46 | +  final network = AdjacencyListGraph(); | 
|  | 47 | +  // ... | 
|  | 48 | +  final route = shortestPath( | 
|  | 49 | +      sender, receiver, (node) => network.nodes[node] ?? const []); | 
|  | 50 | +} | 
| 40 | 51 | 
 | 
| 41 |  | -Graphs that are resolved asynchronously will have similar functions which | 
| 42 |  | -return `FutureOr`. | 
|  | 52 | +void resolveBuildOrder() { | 
|  | 53 | +  final dependencies = TreeGraph(); | 
|  | 54 | +  // ... | 
|  | 55 | +  final buildOrder = topologicalSort([dependencies.root], (node) => node.edges); | 
|  | 56 | +} | 
|  | 57 | +``` | 
0 commit comments