/// <summary> /// Return a complement graph /// If the graph is not initialized throws GraphInitializationException /// </summary> /// <param name="graph">graph</param> /// <returns>complement graph</returns> public static IGraphInterface ComplementGraph(IGraphInterface graph) { // Variable IGraphEdgeListInterface complementGraph; List <IVertexInterface> vertexList; List <IVertexInterface> neighboursList; List <IVertexInterface> intersectionVertexAndNeighboursList; if (!graph.GetIsInitialized()) { throw new MyException.GraphException.GraphInitializationException(); } complementGraph = new GraphEdgeList(graph.GetRealCountVertices()); complementGraph.SetName("Complement graph - " + graph.GetName()); vertexList = graph.AllVertices(); // Add edges foreach (IVertexInterface vertex in vertexList) { neighboursList = graph.Neighbours(vertex); neighboursList.Add(vertex); intersectionVertexAndNeighboursList = vertexList.FindAll(v => !neighboursList.Contains(v)).ToList(); if (intersectionVertexAndNeighboursList.Count == 0) { complementGraph.AddVertex(vertex.GetUserName()); continue; } foreach (IVertexInterface neighbour in intersectionVertexAndNeighboursList) { complementGraph.AddEdge(vertex.GetUserName(), neighbour.GetUserName()); } } complementGraph.InitializeGraph(); return(complementGraph); }