complement method

Graph<V, E> complement({
  1. E? edge(
    1. V source,
    2. V target
    )?,
})

Returns the complement of this graph, that is a graph with the same vertices but with edges between vertices that had no edge.

Implementation

Graph<V, E> complement({E? Function(V source, V target)? edge}) {
  final result = copyEmpty<V, E>(this);
  // Copy all the vertices over.
  for (final vertex in vertices) {
    result.addVertex(vertex);
  }
  // Identify all the edges to be created.
  for (final source in vertices) {
    final targets = vertexStrategy.createSet()
      ..addAll(vertices)
      ..removeAll(successorsOf(source))
      ..remove(source);
    for (final target in targets) {
      result.addEdge(source, target, data: edge?.call(source, target));
    }
  }
  return result;
}