WeightedWalkable<E> class abstract mixin Graphs

A collection of nodes and edges 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 and their respective weights that are accessible from the source node.

The WeightedWalkable declaration provides a default implementation, which can be extended or mixed-in to implement the WeightedWalkable interface. It implements every member other than successors and roots. An implementation of WeightedWalkable should provide a more efficient implementation of members of WeightedWalkable when it can do so.

Adapting

Example

Here is a sample implementation of a weighted graph using pattern matching:

final class AbcGraph with WeightedWalkable<String> {
  @override
  Iterable<(String, int)> successors(String node) {
    return switch (node) {
      'a' => const [('b', 1), ('c', 2)],
      'b' => const [('c', 3)],
      'c' => const [('d', 4)],
      _ => const [],
    };
  }

  @override
  Iterable<String> get roots => ['a', 'b', 'c'];
}

void main() {
  final graph = AbcGraph();
  print(graph.successors('a')); // [('b', 1), ('c', 2.0)]
  print(graph.roots); // ['a', 'b', 'c']
}
Implemented types
Implementers

Constructors

WeightedWalkable.empty()
Creates an empty weighted walkable.
const
factory
WeightedWalkable.from(Map<E, Iterable<(E, double)>> edges)
Creates a walkable from a map of source nodes to target nodes and weights.
const
factory
WeightedWalkable.generate({required Iterable<(E, double)> successors(E node), required Iterable<E> roots()})
Creates a weighted walkable which generates successors dynamically.
const
factory

Properties

edges → Iterable<WeightedEdge<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 setterinherited
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
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, double)>
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

Static Methods

weightedWalkableToString(WeightedWalkable<Object?> walkable, [String start = '{', String end = '}']) → String
Converts a WeightedWalkable to a string like toString.