A collection of values, or "nodes", that can be traversed incrementally.
The nodes of the walkable collection are accessed by calling the successors method with a source vertex as an argument, which returns an Iterable of vertices that are accessible from the source node.
The Walkable declaration provides a default implementation, which can
be extended or mixed-in to implement the Walkable interface. It
implements every member other than successors and roots. An
implementation of Walkable should provide a more efficient
implementation of members of Walkable
when it can do so.
Directed and Undirected Graphs
A Walkable by definition describes a directed graph, where the edges between nodes have a direction. For an undirected graph, where the edges are bidirectional, they should be represented as two directed edges in either direction.
See Walkable.undirected as an example implementation of an undirected graph.
Adapting to a WeightedWalkable
A Walkable can be adapted to a WeightedWalkable by calling the asWeighted method with a function that determines the weight of an edge between two nodes. This allows for a Walkable to be used in algorithms that require a WeightedWalkable:
final unweighted = Walkable.generate(
successors: (node) => switch (node) {
'a' => ['b'],
_ => const [],
},
roots: () => ['a'],
);
final weighted = unweighted.asWeighted((source, target) => 1);
print(weighted.successors('a')); // [('b', 1)]
Example
Here is a sample implementation of a graph using pattern matching:
final class AbcGraph with Walkable<String> {
@override
Iterable<String> successors(String node) {
return switch (node) {
'a' => ['b', 'c'],
'b' => ['c'],
'c' => ['d'],
_ => const [],
};
}
@override
Iterable<String> get roots => ['a', 'b', 'c'];
}
void main() {
final graph = AbcGraph();
print(graph.successors('a')); // ['b', 'c']
print(graph.roots); // ['a', 'b', 'c']
}
- Implemented types
-
- WalkableBase<
E>
- WalkableBase<
- Implementers
Constructors
-
Walkable.circular(Set<
E> nodes) -
Creates a walkable with a circular topology.
factory
- Walkable.empty()
-
Creates an empty walkable.
constfactory
-
Walkable.from(Map<
E, Iterable< edges)E> > -
Creates a walkable from a map of source nodes to target nodes.
constfactory
-
Walkable.generate({required Iterable<
E> successors(E node), required Iterable<E> roots()}) -
Creates a walkable which generates successors dynamically.
constfactory
-
Walkable.linear(Set<
E> nodes) -
Creates a walkable with a linear topology.
factory
-
Walkable.star(Set<
E> points) -
Creates a walkable with a star topology.
factory
-
Walkable.undirected(Set<
(E, E)> edges) -
Creates a walkable with an undirected graph topology.
factory
Properties
-
edges
→ Iterable<
Edge< E> > -
Each edge in the collection.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEmpty → bool
-
Returns
true
if the walkable collection has no nodes.no setteroverride - isNotEmpty → bool
-
Returns
true
if the walkable collection has one or more nodes.no setteroverride -
roots
→ Iterable<
E> -
Each node that is exposed as a root node of the walkable collection.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
asUnweighted(
) → Walkable< E> -
Returns a view of this walkable as an unweighted walkable.
override
-
asWeighted(
double weight(E source, E target)) → WeightedWalkable< E> - Returns a view of this walkable as a weighted walkable.
-
containsEdge(
Edge< E> edge) → bool -
Returns whether the collection contains an
edge
connecting two nodes.override -
containsRoot(
E node) → bool -
Returns whether the collection contains a root node.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
successors(
E node) → Iterable< E> -
Returns each distinct node that is a direct successor of the given
node
.override -
toString(
) → String -
Returns a string representation of (some of) the nodes of
this
.override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited