Skip to content

Commit 5b15f8b

Browse files
authored
Add examples of calling two algorithms to README (#293)
Closes #103 Reword most of the README. - Use "callback" consistently to describe the arguments to the algorithms. - Add generics and more specific names to the tiny example data structures. - Use consistent phrasing in the bullet points about the types of callbacks needed. - Reorder the bullet points since the edges callbacks are always used and worth mentioning first. Add a code block with skeleton examples of calls using each of the example data structures. One example of calls `shortestPath` on a graph with adjacency lists stored in a `Map` using terms related to networking. The other calls `topologicalSort` on a graph represented by a tree of node objects which store outgoing edges using terms related to build dependencies.
1 parent ece541c commit 5b15f8b

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

pkgs/graphs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 2.3.3-wip
22

3+
- Add an example usage to the README.
4+
35
## 2.3.2
46

57
- Require Dart 3.4

pkgs/graphs/README.md

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,53 @@
55
Graph algorithms that do not specify a particular approach for representing a
66
Graph.
77

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:
1111

1212
```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+
// ...
1816
}
1917
```
2018

2119
```dart
22-
class Graph {
23-
Node root;
20+
class TreeGraph<T> {
21+
Node<T> root;
22+
// ...
2423
}
25-
class Node {
26-
List<Node> edges;
27-
// Interesting data
24+
class Node<T> {
25+
List<Node<T>> edges;
26+
T value;
2827
}
2928
```
3029

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`.
3241

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';
3944
45+
void sendMessage() {
46+
final network = AdjacencyListGraph();
47+
// ...
48+
final route = shortestPath(
49+
sender, receiver, (node) => network.nodes[node] ?? const []);
50+
}
4051
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

Comments
 (0)