union method

Graph<V, E> union(
  1. Graph<V, E> other
)

Returns the union of this graph and other. This is a graph with the nodes and edges present in either of the two graphs.

Implementation

Graph<V, E> union(Graph<V, E> other) {
  final result = copyEmpty<V, E>(this);
  for (final graph in [this, other]) {
    // Create all vertices present in any graph.
    for (final vertex in graph.vertices) {
      result.addVertex(vertex);
    }
    // Create all edges present in any graph, but avoid duplicates.
    for (final vertex in graph.vertices) {
      for (final edge in graph.outgoingEdgesOf(vertex)) {
        if (result
            .getEdges(edge.source, edge.target)
            .where((each) => edge.data == each.data)
            .isEmpty) {
          result.addEdge(edge.source, edge.target, data: edge.data);
        }
      }
    }
  }
  return result;
}