Skip to content

Commit 4e476ad

Browse files
committed
Amended functions path, and shortestPaths.
1 parent 2bfdbfc commit 4e476ad

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

benchmark/graph_crawler_benchmark.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var graph = DirectedGraph<String>({
3131
k: {g, f},
3232
}, comparator: comparator);
3333

34-
var gc = GraphCrawler<String>(graph.edges);
34+
final gc = graph.crawler;
3535

3636
void main() {
3737
group('Topology:', () {
@@ -48,7 +48,7 @@ void main() {
4848
gc.mappedTree(a);
4949
});
5050

51-
benchmark('shortestPathsI', () {
51+
benchmark('shortestPaths', () {
5252
gc.shortestPaths(a);
5353
});
5454
});

lib/src/graphs/graph_crawler.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:collection' show HashSet;
2+
13
/// Constant holding the largest (smi) int.
24
///
35
/// `largeInt = pow(2, 30) - 1;`
@@ -49,6 +51,7 @@ class GraphCrawler<T extends Object> {
4951
var startIndex = 0;
5052
var endIndex = 0;
5153
var length = tree.length;
54+
final visited = HashSet<T>()..add(start);
5255
do {
5356
endIndex = tree.length;
5457
for (var i = startIndex; i < endIndex; ++i) {
@@ -57,7 +60,7 @@ class GraphCrawler<T extends Object> {
5760
// Discard walks which reach the same (inner) vertex twice.
5861
// Note: Each path starts with [start] even though it is not
5962
// listed!
60-
if (path.contains(vertex) || path.contains(start)) {
63+
if (path.contains(vertex) || visited.contains(vertex)) {
6164
continue;
6265
} else {
6366
if (vertex == target) {
@@ -70,6 +73,7 @@ class GraphCrawler<T extends Object> {
7073
}
7174
}
7275
}
76+
visited.add(path.last);
7377
}
7478
startIndex = endIndex;
7579
} while (endIndex < length);
@@ -106,6 +110,7 @@ class GraphCrawler<T extends Object> {
106110
var startIndex = 0;
107111
var endIndex = 0;
108112
var length = tree.length;
113+
109114
do {
110115
endIndex = tree.length;
111116
for (var i = startIndex; i < endIndex; ++i) {
@@ -114,7 +119,7 @@ class GraphCrawler<T extends Object> {
114119
// Discard walks which reach the same (inner) vertex twice.
115120
// Note: Each path starts with [start] even though it is not
116121
// listed!
117-
if (path.contains(vertex) || path.contains(start)) {
122+
if (path.contains(vertex)) {
118123
continue;
119124
} else {
120125
if (vertex == target) {
@@ -136,24 +141,22 @@ class GraphCrawler<T extends Object> {
136141

137142
/// Returns a map containing the shortest paths from
138143
/// [start] to each reachable vertex.
139-
/// The map keys represent the set of vertices reachable from [start].
144+
/// The map keys are vertices reachable from [start].
140145
Map<T, Iterable<T>> shortestPaths(T start) {
141146
final pathMap = <T, Iterable<T>>{};
142147

143148
final tree = <Set<T>>[];
144149
for (final connected in edges(start)) {
145150
pathMap[connected] = ([connected]);
146-
if (connected != start) {
147-
// Do not follow self-loops.
148-
// Store first branches of tree.
149-
tree.add({connected});
150-
}
151+
// Store first branches of tree.
152+
tree.add({connected});
151153
}
152154

153155
if (tree.isNotEmpty) {
154156
var startIndex = 0;
155157
var endIndex = 0;
156158
var length = tree.length;
159+
final visited = HashSet<T>()..add(start);
157160
do {
158161
endIndex = tree.length;
159162
for (var i = startIndex; i < endIndex; ++i) {
@@ -162,7 +165,7 @@ class GraphCrawler<T extends Object> {
162165
// Discard walks which reach the same (inner) vertex twice.
163166
// Note: Each path starts with [start] even though it is not
164167
// listed!
165-
if (path.contains(vertex) || path.contains(start)) {
168+
if (path.contains(vertex) || visited.contains(vertex)) {
166169
continue;
167170
} else {
168171
// Store path to new vertex.
@@ -173,6 +176,7 @@ class GraphCrawler<T extends Object> {
173176
length++;
174177
}
175178
}
179+
visited.add(path.last);
176180
}
177181
startIndex = endIndex;
178182
} while (endIndex < length);

0 commit comments

Comments
 (0)