UndirectedGraph constructor

UndirectedGraph(
  1. Map<Vertex, List<UndirectedEdge>> graph
)

Implementation

UndirectedGraph(Map<Vertex, List<UndirectedEdge>> graph) {
  Map<Vertex, List<UndirectedEdge>> secondList = {};
  graph.forEach((key, value) {
    secondList[key] = value;
  });

  graph.forEach((vertex, edgeList) {
    for (var edge in edgeList) {
      bool modify = true;
      Vertex vertex =
          secondList.keys.firstWhere((element) => edge.b.id == element.id);

      for (var targets in secondList[vertex]!) {
        if (targets.a == edge.reversedEdge().a &&
            targets.b == edge.reversedEdge().b) modify = false;
      }

      if (modify) secondList[vertex]!.add(edge.reversedEdge());
    }
  });
  this.graph = secondList;
}