/// <summary> /// Return a subgraph /// If the graph is not initialized throws GraphInitializationException /// Time complexity: O(V + E) /// Space complexity: O(V + E) /// </summary> /// <param name="graph">graph</param> /// <param name="vertexList">list of vertices which will be in the subgraph</param> /// <returns>subgraph</returns> public static IGraphInterface SubGraph(IGraphInterface graph, List <IVertexInterface> vertexList) { // Variable IGraphEdgeListInterface subGraph; List <IVertexInterface> neighboursList; if (!graph.GetIsInitialized()) { throw new MyException.GraphException.GraphInitializationException(); } subGraph = new GraphEdgeList(vertexList.Count); subGraph.SetName("Subgraph - " + graph.GetName()); foreach (IVertexInterface vertex in vertexList) { neighboursList = graph.Neighbours(vertex).Intersect(vertexList).ToList(); if (neighboursList.Count == 0) { subGraph.AddVertex(vertex.GetUserName()); continue; } foreach (IVertexInterface neighbour in neighboursList) { subGraph.AddEdge(vertex.GetUserName(), neighbour.GetUserName()); } } subGraph.InitializeGraph(); return(subGraph); }
/// <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); }
/// <summary> /// Copy a graph /// If the graph is not initialized throws GraphInitializationException /// Time complexity: O(V + E) /// Space complexity: O(V + E) /// </summary> /// <param name="graph">graph</param> /// <returns>graph copy</returns> public static IGraphInterface CopyGraph(IGraphInterface graph) { if (!graph.GetIsInitialized()) { throw new MyException.GraphException.GraphInitializationException(); } // Variable IGraphEdgeListInterface graphCopy; List <IVertexInterface> neighboursVertexList; List <IVertexInterface> allVerticesList = graph.AllVertices(); graphCopy = new GraphEdgeList(graph.GetGraphProperty().GetCountVertices()); graphCopy.SetName(graph.GetName()); foreach (IVertexInterface vertex1 in allVerticesList) { neighboursVertexList = graph.Neighbours(vertex1); if (neighboursVertexList.Count == 0) { graphCopy.AddVertex(vertex1.GetUserName()); continue; } foreach (IVertexInterface vertex2 in neighboursVertexList) { graphCopy.AddEdge(vertex1.GetUserName(), vertex2.GetUserName()); } } graphCopy.InitializeGraph(); return(graphCopy); }
public void VertexAdd(IVertexInterface vertex) { // Component if (componentsList != null) { if (countComponents > 2) { // Create a new component with the vertex IGraphEdgeListInterface newGraph = new GraphEdgeList(1); newGraph.AddVertex(vertex.GetUserName()); newGraph.InitializeGraph(); componentsList.Add(newGraph); countComponents++; isConnected = false; } else { componentsList = null; countComponents = null; isConnected = null; } } // Properties //isCyclic = isCyclic; isEulerian = EulerianGraphEnum.notEulerian; if (countComponents != order) { isRegular = false; } else { isRegular = true; } // IntegralInvariants //circuitRank = circuitRank; //girth = girth; cayleysFormula = null; // SequencesAndPolynomialsOthers spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>(); //matching = matching; //cutVertices = cutVertices; //bridges = bridges; // DegreeSequence if (degreeSequence != null) { if (!isDegreeSequenceSorted) { degreeSequence.Add(new KeyValuePair <IVertexInterface, int>(vertex, 0)); degreeSequenceVertex.Add(vertex); degreeSequenceInt.Add(0); } else { degreeSequence = null; degreeSequenceVertex = null; degreeSequenceInt = null; isDegreeSequenceSorted = false; } if (0 < minimumVertexDegree) { minimumVertexDegree = 0; } //maximumVertexDegree = maximumVertexDegree; averageVertexDegree = null; medianVertexDegree = null; } // Chordal //isChordal = isChordal; if (isChordal.HasValue && (bool)isChordal) { perfectEliminationOrderingList.Add(vertex); } // GraphClass //graphClass = graphClass; }
/// <summary> /// The get path. /// </summary> /// <param name="node"> /// The node. /// </param> /// <returns> /// Returns GraphEdgeList path from source to sink. /// </returns> private GraphEdgeList GetPath(GraphNode node) { var path = new GraphEdgeList(); GraphNode current = node; while (current.TraverseParent != null) { GraphEdge edge = current.TraverseParent.Neighbours.FindByName( current.TraverseParent.VertexId, current.VertexId); path.Insert(0, edge); // path.Add(edge); current = current.TraverseParent; } return path; }
/// <summary> /// Return a line graph /// If the graph is not initialized throws GraphInitializationException /// If teh graph has 0 edges throws GraphLineGraphInvalidCountOfEdges /// </summary> /// <param name="graph">graph</param> /// <returns>line graf</returns> public static IGraphInterface LineGraph(IGraphInterface graph) { // Variable int idVertex1, idVertex2; string idNewVertex, userNameNewVertex; string userNameVertex1, userNameVertex2; IGraphEdgeListInterface lineGraph; List <IVertexInterface> vertexList; List <IVertexInterface> neighboursList; Dictionary <string, IVertexInterface> vertexMap; List <IVertexInterface> neighboursNewList; if (!graph.GetIsInitialized()) { throw new MyException.GraphException.GraphInitializationException(); } if (graph.GetGraphProperty().GetCountEdges() == 0) { throw new MyException.GraphException.GraphLineGraphInvalidCountOfEdges(graph.GetGraphProperty().GetCountEdges().ToString()); } lineGraph = new GraphEdgeList(graph.GetGraphProperty().GetCountEdges()); lineGraph.SetName("Line graph - " + graph.GetName()); vertexMap = new Dictionary <string, IVertexInterface>(); vertexList = graph.AllVertices(); foreach (IVertexInterface vertex in vertexList) { idVertex1 = vertex.GetIdentifier(); userNameVertex1 = vertex.GetUserName(); neighboursList = graph.Neighbours(vertex); neighboursNewList = new List <IVertexInterface>(); foreach (IVertexInterface neighbour in neighboursList) { idVertex2 = neighbour.GetIdentifier(); userNameVertex2 = neighbour.GetUserName(); if (idVertex1 < idVertex2) { idNewVertex = idVertex1.ToString() + idVertex2.ToString(); userNameNewVertex = userNameVertex1 + userNameVertex2; } else { idNewVertex = idVertex2.ToString() + idVertex1.ToString(); userNameNewVertex = userNameVertex2 + userNameVertex1; } if (!vertexMap.TryGetValue(idNewVertex, out IVertexInterface newVertex)) { newVertex = new Vertex(userNameNewVertex); vertexMap.Add(idNewVertex, newVertex); } neighboursNewList.Add(newVertex); } if (neighboursList.Count == 1) { lineGraph.AddVertex(neighboursNewList.First().GetUserName()); continue; } for (int i = 0; i < neighboursNewList.Count - 1; i++) { for (int j = i + 1; j < neighboursNewList.Count; j++) { lineGraph.AddEdge(neighboursNewList[i].GetUserName(), neighboursNewList[j].GetUserName()); } } } lineGraph.InitializeGraph(); return(lineGraph); }
/// <summary> /// Default constructor. Must be constructed with a value /// </summary> /// <param name="value">Value stored in the node</param> public GraphNode(T value) { this.edges = new GraphEdgeList <T>(); this.value = value; }
/// <summary> /// Partition a graph to connected components /// Change: countComponents, componentsList, isConnected /// Time complexity: O(V + E) /// Space complexity: O(V + E) + new graphs /// </summary> private void Components() { // Variable int componentNumber = 0; Dictionary <IVertexInterface, int> allVerticesDictionary = graph.AllVertices().ToDictionary(x => x, x => 0); /* Invalid graph * if (graph.GetRealCountVertices() == 0) * { * countComponents = 1; * componentsList = new List<IGraphInterface>(componentNumber); * componentsList.Add(graph); * return; * } */ isInitializedComponent = true; // Core algorithm for (int i = 0; i < allVerticesDictionary.Count; i++) { KeyValuePair <IVertexInterface, int> record = allVerticesDictionary.ElementAt(i); if (record.Value != 0) { continue; } ComponentsBFS(record.Key, allVerticesDictionary, ++componentNumber); } countComponents = componentNumber; // Create graphs componentsList = new List <IGraphInterface>(componentNumber); if (countComponents == 1) { componentsList.Add(graph); return; } for (int i = 1; i <= componentNumber; i++) { var vertexComponent = from record in allVerticesDictionary where record.Value == i select record.Key; GraphEdgeList graphComponent = new GraphEdgeList(vertexComponent.Count()); graphComponent.SetName(graph.GetName()); // Add edges foreach (IVertexInterface vertex1 in vertexComponent) { List <IVertexInterface> neighboursVertexList = graph.Neighbours(vertex1); if (neighboursVertexList.Count() == 0) { graphComponent.AddVertex(vertex1.GetUserName()); continue; } foreach (IVertexInterface vertex2 in neighboursVertexList) { graphComponent.AddEdge(vertex1.GetUserName(), vertex2.GetUserName()); } } graphComponent.InitializeGraph(); componentsList.Add(graphComponent); } isConnected = true; if (countComponents != 1) { isConnected = false; } }