-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_explorer_example.dart
67 lines (55 loc) · 1.24 KB
/
graph_explorer_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'dart:convert';
import 'package:graph_explorer/graph_explorer.dart';
void main() async {
var graph = Graph<String>();
graph.node('a').getOrAddOutput('b')
..addOutput('c')
..addOutput('d');
graph.node('c').addOutput('f');
graph.node('d').getOrAddOutput('e').addOutput('f');
graph.node('f').addOutput('x');
var result = await graph.scanPathsFrom('a', 'f', findAll: true);
print("Paths from `a` to `f`:");
for (var p in result.paths) {
var lStr = p.toListOfString();
print('- $lStr');
}
var shortest = result.paths.shortestPaths().toListOfStringPaths();
print('\nShortest paths:');
for (var p in shortest) {
print('- $p');
}
var tree = graph.toTree();
print('\nGraph to JSON Tree:');
print(_encodeJsonPretty(tree));
}
String _encodeJsonPretty(dynamic json) =>
JsonEncoder.withIndent(' ').convert(json);
/////////////
// OUTPUT: //
/////////////
// Paths from `a` to `f`:
// - [a, b, c, f]
// - [a, b, d, e, f]
//
// Shortest paths:
// - [a, b, c, f]
//
// Graph to JSON Tree:
// {
// "a": {
// "b": {
// "c": {
// "f": {
// "x": null
// }
// },
// "d": {
// "e": {
// "f": "f"
// }
// }
// }
// }
// }
//