weightAlong method

W weightAlong(
  1. Iterable<T> walk
)

Returns the weight obtained by traversing walk and summing all edge weights.

  • The vertices must be traversable in the specified order.
  • Vertices and edges may be repeated.
  • Throws an error if the walk cannot be traversed.
  • Returns zero if walk is empty.

Implementation

W weightAlong(Iterable<T> walk) {
  final edge = walk.take(2);
  if (edge.length < 2) {
    return zero;
  }
  final vertex = edge.first;
  final connectedVertex = edge.last;

  if (!_edges.containsKey(vertex)) {
    throw ErrorOfType<UnkownVertex>();
  }
  if (!_edges[vertex]!.containsKey(connectedVertex)) {
    throw ErrorOfType<NotAnEdge>(
      message: 'Could not calculate weight of walk: $walk.',
      invalidState: 'Vertex $vertex not connected to $connectedVertex.}',
    );
  }
  return summation(
    _edges[vertex]![connectedVertex]!,
    weightAlong(walk.skip(1)),
  );
}