Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of calling two algorithms to README #293

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/graphs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 2.3.3-wip

- Add an example usage to the README.

## 2.3.2

- Require Dart 3.4
Expand Down
59 changes: 37 additions & 22 deletions pkgs/graphs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,53 @@
Graph algorithms that do not specify a particular approach for representing a
Graph.

Functions in this package will take arguments that provide the mechanism for
traversing the graph. For example two common approaches 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:

```dart
class Graph {
Map<Node, List<Node>> nodes;
}
class Node {
// Interesting data
class AdjacencyListGraph<T> {
Map<T, List<T>> nodes;
// ...
}
```

```dart
class Graph {
Node root;
class TreeGraph<T> {
Node<T> root;
// ...
}
class Node {
List<Node> edges;
// Interesting data
class Node<T> {
List<Node<T>> edges;
T value;
}
```

Any representation can be adapted to the needs of the algorithm:
Any representation can be adapted to the callback arguments.

- Algorithms which need to traverse the graph take an `edges` callback which
provides the immediate neighbors of a given node.
- Algorithms which need to associate unique data with each node in the graph
allow passing `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`.

- Some algorithms need to associate data with each node in the graph. If the
node type `T` does not correctly or efficiently implement `hashCode` or `==`,
you may provide optional `equals` and/or `hashCode` functions are parameters.
- Algorithms which need to traverse the graph take a `edges` function which provides the reachable nodes.
- `(node) => graph[node]`
- `(node) => node.edges`
```dart
import 'package:graphs/graphs.dart';

void sendMessage() {
final network = AdjacencyListGraph();
// ...
final route = shortestPath(
sender, receiver, (node) => network.nodes[node] ?? const []);
}

Graphs that are resolved asynchronously will have similar functions which
return `FutureOr`.
void resolveBuildOrder() {
final dependencies = TreeGraph();
// ...
final buildOrder = topologicalSort([dependencies.root], (node) => node.edges);
}
```