intersection method

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

Returns the intersection of this graph and other. This is a graph with the nodes and edges present in both graphs.

Implementation

Graph<V, E> intersection(Graph<V, E> other) {
  final result = copyEmpty<V, E>(this);
  // Create all the vertices present in both graphs.
  for (final vertex in vertices) {
    if (other.vertices.contains(vertex)) {
      result.addVertex(vertex);
    }
  }
  // Create all edges that have vertices present in the result, and edges
  // in both graphs.
  for (final edge in edges) {
    if (result.vertices.contains(edge.source) &&
        result.vertices.contains(edge.target) &&
        other
            .getEdges(edge.source, edge.target)
            .where((each) => edge.data == each.data)
            .isNotEmpty) {
      result.addEdge(edge.source, edge.target, data: edge.data);
    }
  }
  return result;
}