private void Testing(SpanningTreeEnum spanningTreeEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[spanningTreeEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(spanningTreeEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); stringBuilder.AppendLine("SpanningTree: "); List <IEdgeInterface> spanningTreeList = graph.GetGraphProperty().GetSpanningTree(); foreach (IEdgeInterface edge in spanningTreeList) { stringBuilder.AppendLine(edge.ToString()); } } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(spanningTreeEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
/// <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); }
public static GraphClassEnum GetGraphClass(IGraphInterface graph) { if (IsCompleteGraph(graph)) { return(GraphClassEnum.completeGraph); } if (IsTreeGraph(graph)) { return(GraphClassEnum.treeGraph); } if (IsCycleGraph(graph)) { return(GraphClassEnum.cycleGraph); } Tuple <bool, bool> bipartiteResult = IsBipartiteGraph(graph); if (bipartiteResult.Item1) { if (bipartiteResult.Item2) { return(GraphClassEnum.completeBipartiteGraph); } return(GraphClassEnum.bipartiteGraph); } return(GraphClassEnum.none); }
private void Testing(SubGraphEnum subGraphEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[subGraphEnum].Item1); countVertices = testsDictionary[subGraphEnum].Item2; reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(subGraphEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); IGraphInterface subGraph = GraphOperation.SubGraph(graph, graph.AllVertices().Take(countVertices).ToList()); stringBuilder.AppendLine("Subgraph created."); stringBuilder.AppendLine(subGraph.ToString()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(subGraphEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
private void Testing(ChordalTestEnum chordalEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[chordalEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(chordalEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); stringBuilder.AppendLine("isChordal " + graph.GetGraphProperty().GetIsChordal()); foreach (IVertexInterface vertex in graph.GetGraphProperty().GetPerfectEliminationOrdering()) { stringBuilder.AppendLine("- " + vertex.GetIdentifier()); } } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(chordalEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } catch (MyException.GraphException.GraphIsNotConnected e) { stringBuilder.AppendLine(e.Message); } }
private void Testing(ClassEnum classEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[classEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(classEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); GraphClass.GraphClassEnum graphClassEnum = GraphClass.GetGraphClass(graph); stringBuilder.AppendLine("Graph class: " + graphClassEnum.ToString()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(classEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
private void Testing(CycleEnum cycleEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[cycleEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(cycleEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); stringBuilder.AppendLine("Is graph cyclic: " + graph.GetGraphProperty().GetIsCyclic()); stringBuilder.AppendLine("Gridth: " + graph.GetGraphProperty().GetGirth()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(cycleEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
private void Testing(ComplementEnum complementEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[complementEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(complementEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); IGraphInterface graphComplement = GraphOperation.ComplementGraph(graph); stringBuilder.AppendLine("Complement graph."); stringBuilder.AppendLine(graphComplement.ToString()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(complementEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
private void Testing(ModificationEnum modificationEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[modificationEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(modificationEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); switch (modificationEnum) { case ModificationEnum.valid: Valid(graph); break; case ModificationEnum.invalid: Invalid(graph); break; default: throw new MyException.TestsException.TestsMissingTestException(modificationEnum.ToString()); } stringBuilder.AppendLine("Graph modified."); stringBuilder.AppendLine(graph.ToString()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(modificationEnum.ToString()); } }
private void Testing(ComponentEnum componentEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[componentEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(componentEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); stringBuilder.AppendLine("Number of components: " + graph.GetGraphProperty().GetCountComponents()); stringBuilder.AppendLine("Is graph connected: " + graph.GetGraphProperty().GetIsConnected()); stringBuilder.AppendLine("Circuit rank: " + graph.GetGraphProperty().GetCircuitRank()); graphComponentList = graph.GetGraphProperty().GetComponents(); foreach (Graph graphComponent in graphComponentList) { stringBuilder.AppendLine("Graph component."); stringBuilder.AppendLine(graphComponent.ToString()); } } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(componentEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
/// <summary> /// Return true if the graph is complete graph /// Time complexity: O(1) /// </summary> /// <param name="graph">graph</param> /// <returns>true if the graph is complete graph, otherwise false</returns> public static bool IsCompleteGraph(IGraphInterface graph) { // |E| = (|V|)C(2) if (graph.GetGraphProperty().GetCountEdges() == MyMath.MyMath.nCr(graph.GetGraphProperty().GetCountVertices(), 2)) { return(true); } return(false); }
/// <summary> /// Return true if the graph is tree /// Time complexity: O(V + E) /// </summary> /// <param name="graph">graph</param> /// <returns>true if the graph is tree, otherwise false</returns> public static bool IsTreeGraph(IGraphInterface graph) { // Graph is connected and |E| = |V| - 1 // Euler's formula if ((graph.GetGraphProperty().GetIsConnected()) && (graph.GetGraphProperty().GetCountEdges() == graph.GetGraphProperty().GetCountVertices() - 1)) { return(true); } return(false); }
private void Testing(DegreeSequenceEnum degreeSequenceEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[degreeSequenceEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(degreeSequenceEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); // Sorted List <int> degreeSequenceList = graph.GetGraphProperty().GetDegreeSequenceInt(true); stringBuilder.AppendLine("Degree sequence"); foreach (int degree in degreeSequenceList) { stringBuilder.Append(degree + " "); } stringBuilder.AppendLine(""); stringBuilder.AppendLine("Minimum vertex degree: " + graph.GetGraphProperty().GetMinimumVertexDegree()); stringBuilder.AppendLine("Maximum vertex degree: " + graph.GetGraphProperty().GetMaximumVertexDegree()); stringBuilder.AppendLine("Average vertex degree: " + graph.GetGraphProperty().GetAverageVertexDegree()); stringBuilder.AppendLine("Is graph regular: " + graph.GetGraphProperty().GetIsRegular()); graph.GetGraphProperty().Reset(); // Unsorted degreeSequenceList = graph.GetGraphProperty().GetDegreeSequenceInt(false); stringBuilder.AppendLine("Degree sequence"); foreach (int degree in degreeSequenceList) { stringBuilder.Append(degree + " "); } stringBuilder.AppendLine(""); stringBuilder.AppendLine("Minimum vertex degree: " + graph.GetGraphProperty().GetMinimumVertexDegree()); stringBuilder.AppendLine("Maximum vertex degree: " + graph.GetGraphProperty().GetMaximumVertexDegree()); stringBuilder.AppendLine("Average vertex degree: " + graph.GetGraphProperty().GetAverageVertexDegree()); stringBuilder.AppendLine("Is graph regular: " + graph.GetGraphProperty().GetIsRegular()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(degreeSequenceEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } }
/// <summary> /// Return true if the graph is cycle /// Time complexity: O(V + E) /// </summary> /// <param name="graph">graph</param> /// <returns>true if the graph is cycle, otherwise false</returns> public static bool IsCycleGraph(IGraphInterface graph) { // Graph is connected and 2-regular if ((graph.GetGraphProperty().GetIsConnected()) && (graph.GetGraphProperty().GetIsRegular()) && (graph.GetGraphProperty().GetMaximumVertexDegree() == 2)) { return(true); } return(false); }
private void Testing(ColoredGraphEnum coloredGraphEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[coloredGraphEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(coloredGraphEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); switch (coloredGraphEnum) { case ColoredGraphEnum.valid: Valid(graph); stringBuilder.AppendLine("Graph modified."); break; case ColoredGraphEnum.invalid: Invalid(graph); stringBuilder.AppendLine("Graph modified."); break; case ColoredGraphEnum.interchange1: case ColoredGraphEnum.interchange2: case ColoredGraphEnum.interchange3: case ColoredGraphEnum.interchange4: case ColoredGraphEnum.interchange5: graph.GetColoredGraph().GreedyColoring(graph.AllVertices(), GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithInterchangeEnum.interchange); stringBuilder.AppendLine("Graph colored."); break; default: throw new MyException.TestsException.TestsMissingTestException(coloredGraphEnum.ToString()); } stringBuilder.AppendLine(graph.GetColoredGraph().ToString()); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(coloredGraphEnum.ToString()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.ToString()); } }
/// <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); }
private void Testing(BridgesCutVerticesEnum bridgesCutVerticesEnum) { try { testPath = ReaderWriter.ReaderWriter.CreateTestFile(testsDictionary[bridgesCutVerticesEnum]); reader = new ReaderWriter.ReaderGraph(testPath, false); graph = reader.ReadFile(); stringBuilder.AppendLine(bridgesCutVerticesEnum.ToString()); stringBuilder.AppendLine("Graph created."); stringBuilder.AppendLine(graph.ToString()); stringBuilder.AppendLine("Number of cut vertices: " + graph.GetGraphProperty().GetCutVertices().Count); stringBuilder.AppendLine("Cut vertices: "); graph.GetGraphProperty().GetCutVertices().ForEach(x => { stringBuilder.AppendLine(x.GetUserName()); }); stringBuilder.AppendLine("Number of bridges: " + graph.GetGraphProperty().GetBridges().Count); stringBuilder.AppendLine("Bridges: "); graph.GetGraphProperty().GetBridges().ForEach(x => { stringBuilder.AppendLine(x.GetVertex1().GetUserName() + " " + x.GetVertex2().GetUserName()); }); graph.GetGraphProperty().Reset(); stringBuilder.AppendLine("Number of cut vertices: " + graph.GetGraphProperty().GetCutVertices().Count); stringBuilder.AppendLine("Number of bridges: " + graph.GetGraphProperty().GetBridges().Count); } catch (KeyNotFoundException) { throw new MyException.TestsException.TestsMissingTestException(bridgesCutVerticesEnum.ToString()); } catch (MyException.ReaderWriterException.ReaderWriterException e) { stringBuilder.AppendLine(e.Message); } catch (MyException.GraphException.GraphIsNotConnected e) { stringBuilder.AppendLine(e.Message); } }
/// <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 VertexExpansion(IVertexInterface vertex) { // Component if (componentsList != null) { // Need change => copies of component if (countComponents > 1) { // Get component which contains the vertex IVertexInterface myVertexComponent = null; IGraphInterface myComponentGraph = null; foreach (IGraphInterface componentGraph in componentsList) { if (componentGraph.ExistsUserName(vertex.GetUserName())) { myComponentGraph = componentGraph; myVertexComponent = myComponentGraph.GetVertexByUserName(vertex.GetUserName()); break; } } myComponentGraph.VertexExpansion(myVertexComponent); } //componentsList = componentsList; //countComponents = countComponents; //isConnected = isConnected; } // Properties isCyclic = null; isEulerian = EulerianGraphEnum.undefined; // IntegralInvariants circuitRank = null; girth = null; cayleysFormula = null; // SequencesAndPolynomialsOthers if (isConnected != null && !(bool)isConnected) { spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>(); } else { spanningTreeBFS = null; } matching = null; cutVertices = null; bridges = null; // DegreeSequence degreeSequence = null; maximumVertexDegree = null; minimumVertexDegree = null; averageVertexDegree = null; medianVertexDegree = null; isDegreeSequenceSorted = false; isRegular = null; // Chordal if (isChordal.HasValue && (bool)isChordal) { isChordal = null; perfectEliminationOrderingList = null; righNeighborhoodDictionary = null; } // GraphClass graphClass = GraphClass.GraphClass.GraphClassEnum.undefined; }
public void VertexDelete(IVertexInterface vertex) { // Component if (componentsList != null) { // More components => copies of components if (countComponents > 1) { // Get component which contains the vertex IVertexInterface myVertexComponent = null; IGraphInterface myComponentGraph = null; foreach (IGraphInterface componentGraph in componentsList) { if (componentGraph.ExistsUserName(vertex.GetUserName())) { myComponentGraph = componentGraph; myVertexComponent = myComponentGraph.GetVertexByUserName(vertex.GetUserName()); break; } } // The vertex is component if (myComponentGraph.CountNeighbours(myVertexComponent) == 0) { componentsList.Remove(myComponentGraph); countComponents--; isConnected = countComponents == 1 ? true : false; } // The vertex is in a component with another vertices else { componentsList = null; countComponents = null; isConnected = null; } } else { if (cutVertices != null && !cutVertices.Contains(vertex)) { //componentsList = componentsList; //countComponents = countComponents; //isConnected = isConnected; } else { componentsList = null; countComponents = null; isConnected = null; } } } // Properties if (isCyclic != null && (bool)isCyclic) { isCyclic = null; } if (isConnected != null && !(bool)isConnected) { isEulerian = EulerianGraphEnum.notEulerian; } else { isEulerian = EulerianGraphEnum.undefined; } // IntegralInvariants circuitRank = null; girth = null; cayleysFormula = null; // SequencesAndPolynomialsOthers if (isConnected != null && !(bool)isConnected) { spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>(); } else { spanningTreeBFS = null; } matching = null; cutVertices = null; bridges = null; // DegreeSequence degreeSequence = null; maximumVertexDegree = null; minimumVertexDegree = null; averageVertexDegree = null; medianVertexDegree = null; isDegreeSequenceSorted = false; isRegular = null; // Chordal isChordal = null; perfectEliminationOrderingList = null; righNeighborhoodDictionary = null; // GraphClass graphClass = GraphClass.GraphClass.GraphClassEnum.undefined; }
public Graph(IGraphInterface graph) { InitializeComponent(); InternalGraph = graph; }
private void Valid(IGraphInterface graph) { // Variable List <IVertexInterface> vertexList = graph.AllVertices(); IColoredGraphInterface coloredGraph = graph.GetColoredGraph(); if (coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.First(), coloredGraph.GreedyColoring(vertexList.First())); if (coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ResetColorVertex(vertexList.First()); coloredGraph.GreedyColoring(vertexList); if (!coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.DeinitializationColoredGraph(); if (!coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.DeinitializationColoredGraph(); coloredGraph.ResetColors(); coloredGraph.ResetColors(); if (coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.First(), 1); coloredGraph.ColorVertex(vertexList.ElementAt(1), 1); if (coloredGraph.CheckValidColor().Count == 0) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.First(), 2); if (!coloredGraph.IsVertexColored(vertexList.First())) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.IsVertexColored(vertexList.ElementAt(2))) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ResetColorVertex(vertexList.First()); if (coloredGraph.IsVertexColored(vertexList.First())) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ResetColorVertex(vertexList.ElementAt(2)); if (coloredGraph.IsVertexColored(vertexList.ElementAt(2))) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.First(), 1); if (coloredGraph.CheckValidColor(vertexList.First())) { throw new MyException.TestsException.SomethingWrongTestException(); } if (!coloredGraph.CheckValidColor(vertexList.ElementAt(2))) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GreedyColoring(vertexList.First()) != 2) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GetUnColoredVertexList().Count != 4) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GetColoredVertexList().Count != 2) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GetCountUsedColors() != 1) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.GreedyColoring(vertexList); if (coloredGraph.GetCountUsedColors() != 2) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.First(), VertexExtended.GetDefaultColor()); if (coloredGraph.CheckValidColor().Count != 0) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.GreedyColoring(vertexList); coloredGraph.InitializeColoredGraph(); if (!coloredGraph.GetIsInitializedColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } graph.VertexAdd(new Vertex()); if (coloredGraph.GetIsInitializedColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } vertexList = graph.AllVertices(); if (vertexList.Last().GetColor() != VertexExtended.GetDefaultColor()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.Last(), 2); coloredGraph.ColorVertex(vertexList.First(), 2); if (coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.GreedyColoring(vertexList); if (!coloredGraph.InitializeColoredGraph()) { throw new MyException.TestsException.SomethingWrongTestException(); } graph.VertexDelete(vertexList.Last()); graph.VertexAdd(new Vertex()); vertexList = graph.AllVertices(); if (coloredGraph.GetColoredVertexList().Count != 6) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GetUnColoredVertexList().Count != 1) { throw new MyException.TestsException.SomethingWrongTestException(); } coloredGraph.ColorVertex(vertexList.Last(), 3); if (coloredGraph.GetCountUsedColors() != 3) { throw new MyException.TestsException.SomethingWrongTestException(); } graph.VertexDelete(vertexList.Last()); if (coloredGraph.GetColoredVertexList().Count != 6) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GetUnColoredVertexList().Count != 0) { throw new MyException.TestsException.SomethingWrongTestException(); } if (coloredGraph.GetCountUsedColors() != 2) { throw new MyException.TestsException.SomethingWrongTestException(); } }
/// <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); }
private void Invalid(IGraphInterface graph) { // Variable List <IVertexInterface> vertexList = graph.AllVertices(); IColoredGraphInterface coloredGraph = graph.GetColoredGraph(); coloredGraph.GreedyColoring(vertexList); coloredGraph.InitializeColoredGraph(); // VertexColor - initialization stringBuilder.AppendLine("VertexColor - initialization"); try { coloredGraph.ColorVertex(vertexList.First(), 2); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // GreedingColoring - initialization stringBuilder.AppendLine("GreedyColoring - initialization"); try { coloredGraph.GreedyColoring(vertexList); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // ResetColorVertex - initialization stringBuilder.AppendLine("Reset - initialization"); try { coloredGraph.ResetColorVertex(vertexList.First()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Reset - initialization stringBuilder.AppendLine("Reset - initialization"); try { coloredGraph.ResetColors(); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Initialization - initialization stringBuilder.AppendLine("Initialization - initialization"); try { coloredGraph.InitializeColoredGraph(); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } coloredGraph.DeinitializationColoredGraph(); // Deinitialization - initialization stringBuilder.AppendLine("Deinitialization - initialization"); try { coloredGraph.DeinitializationColoredGraph(); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // ColorVertex - Doesn't exist stringBuilder.AppendLine("VertexColor - Doesn't exist"); try { coloredGraph.ColorVertex(new Vertex(), 3); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // GreedyColoring - Doesn't exist stringBuilder.AppendLine("GreedyColoring - Doesn't exist"); try { coloredGraph.GreedyColoring(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // CheckValidColor - Doesn't exist stringBuilder.AppendLine("CheckValidColor - Doesn't exist"); try { coloredGraph.CheckValidColor(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // IsVertexColored - Doesn't exist stringBuilder.AppendLine("IsVertexColored - Doesn't exist"); try { coloredGraph.IsVertexColored(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // GetColorVertex - Doesn't exist stringBuilder.AppendLine("GetColorVertex - Doesn't exist"); try { coloredGraph.GetColorVertex(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } }
private void Valid(IGraphInterface graph) { // Variable List <IVertexInterface> vertexList = graph.AllVertices(); graph.VertexContraction(vertexList.First()); vertexList = graph.AllVertices(); graph.VertexSuppression(vertexList.First()); vertexList = graph.AllVertices(); graph.VertexExpansion(vertexList.First()); vertexList = graph.AllVertices(); graph.VertexSuppression(vertexList.Last()); vertexList = graph.AllVertices(); graph.VertexDelete(vertexList.First()); vertexList = graph.AllVertices(); graph.VertexSuppression(vertexList.Last()); vertexList = graph.AllVertices(); IVertexInterface v1 = new Vertex("V1"); graph.VertexAdd(v1); vertexList = graph.AllVertices(); graph.EdgeAdd(new Edge(vertexList.First(), v1)); vertexList = graph.AllVertices(); graph.VertexContraction(v1); vertexList = graph.AllVertices(); graph.EdgeSubdivision(new Edge(vertexList.First(), vertexList.Skip(1).First())); vertexList = graph.AllVertices(); graph.EdgeAdd(new Edge(vertexList.First(), vertexList.Skip(1).First())); vertexList = graph.AllVertices(); graph.EdgeContraction(new Edge(vertexList.First(), vertexList.Skip(1).First())); vertexList = graph.AllVertices(); graph.EdgeDelete(new Edge(vertexList.First(), vertexList.Skip(1).First())); vertexList = graph.AllVertices(); graph.VertexDelete(vertexList.Skip(1).First()); vertexList = graph.AllVertices(); graph.VertexSuppression(vertexList.First()); vertexList = graph.AllVertices(); graph.VertexDelete(vertexList.First()); vertexList = graph.AllVertices(); graph.VertexExpansion(vertexList.First()); vertexList = graph.AllVertices(); graph.EdgeContraction(new Edge(vertexList.First(), vertexList.Skip(1).First())); vertexList = graph.AllVertices(); IVertexInterface v2 = new Vertex("V2"); graph.VertexAdd(v2); vertexList = graph.AllVertices(); graph.VertexDelete(vertexList.Skip(1).First()); graph.VertexAdd(new Vertex("V3")); }
public void EdgeDelete(IEdgeInterface edge) { // Variable IVertexInterface vertex1 = graph.GetVertexByUserName(edge.GetVertex1().GetUserName()); IVertexInterface vertex2 = graph.GetVertexByUserName(edge.GetVertex2().GetUserName()); // Component if (componentsList != null) { if (countComponents > 1) { // Get component which contains the vertex IGraphInterface myComponentGraph = null; foreach (IGraphInterface componentGraph in componentsList) { if (componentGraph.ExistsUserName(vertex1.GetUserName())) { myComponentGraph = componentGraph; break; } } myComponentGraph.EdgeDelete(new Edge(myComponentGraph.GetVertexByUserName(vertex1.GetUserName()), myComponentGraph.GetVertexByUserName(vertex2.GetUserName()))); componentsList = null; countComponents = null; isConnected = null; } else { if (bridges != null) { // The edge is a bridge if (bridges.Any(e => ((e.GetVertex1().Equals(vertex1) && e.GetVertex2().Equals(vertex2)) || (e.GetVertex1().Equals(vertex2) && e.GetVertex2().Equals(vertex1))))) { componentsList = null; countComponents = null; isConnected = null; } // The edge isn't a bridge else { //componentsList = componentsList; //countComponents = countComponents; //isConnected = isConnected; } } } } // Properties if (isCyclic != null && !(bool)isCyclic) { isCyclic = false; } else { isCyclic = null; } if (isEulerian == EulerianGraphEnum.eulerian) { isEulerian = EulerianGraphEnum.semiEulerian; } else { isEulerian = EulerianGraphEnum.undefined; } // IntegralInvariants circuitRank = null; if (girth != null && girth > 2) { girth = null; } //cayleysFormula = cayleysFormula; // SequencesAndPolynomialsOthers if (isConnected != null && !(bool)isConnected) { spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>(); } else { spanningTreeBFS = null; } matching = null; cutVertices = null; bridges = null; // DegreeSequence if (degreeSequence != null) { int countNeighbourVertex1 = graph.CountNeighbours(vertex1); int countNeighbourVertex2 = graph.CountNeighbours(vertex2); if (!isDegreeSequenceSorted) { // Variable int index1, index2; index1 = degreeSequenceInt.FindIndex(i => i == (countNeighbourVertex1 + 1)); degreeSequenceInt[index1] = countNeighbourVertex1; index2 = degreeSequenceInt.FindIndex(i => i == (countNeighbourVertex2 + 1)); degreeSequenceInt[index2] = countNeighbourVertex2; index1 = degreeSequence.FindIndex(p => p.Key == vertex1); index2 = degreeSequence.FindIndex(p => p.Key == vertex2); degreeSequence[index1] = new KeyValuePair <IVertexInterface, int>(vertex1, countNeighbourVertex1); degreeSequence[index2] = new KeyValuePair <IVertexInterface, int>(vertex2, countNeighbourVertex2); } else { degreeSequence = null; degreeSequenceVertex = null; degreeSequenceInt = null; isDegreeSequenceSorted = false; } if (minimumVertexDegree.HasValue) { if (countNeighbourVertex1 < minimumVertexDegree) { minimumVertexDegree = countNeighbourVertex1; } if (countNeighbourVertex2 < minimumVertexDegree) { minimumVertexDegree = countNeighbourVertex2; } } if (maximumVertexDegree.HasValue) { if (((countNeighbourVertex1 + 1) == maximumVertexDegree) || ((countNeighbourVertex2 + 1) == maximumVertexDegree)) { maximumVertexDegree = null; } } averageVertexDegree = null; medianVertexDegree = null; isRegular = null; } // Chordal isChordal = null; perfectEliminationOrderingList = null; righNeighborhoodDictionary = null; // GraphClass graphClass = GraphClass.GraphClass.GraphClassEnum.undefined; }
/// <summary> /// Return true if the graph is (complete) bipartite /// Time complexity: O(V + E) /// </summary> /// <param name="graph">graph</param> /// <returns>(bool, bool) - the first item stands for bipartite and the second item stands for complete /// (true, true) - complete bipartite graph /// (true, false) - bipartite graph /// (false, - ) - the graph is not (complete) bipartite</returns> public static Tuple <bool, bool> IsBipartiteGraph(IGraphInterface graph) { // Variable IVertexInterface vertex; List <IVertexInterface> neighboursVertexList; bool isFirstPartite, isBipartite = true; HashSet <IVertexInterface> firstPartite = new HashSet <IVertexInterface>(); HashSet <IVertexInterface> secondPartite = new HashSet <IVertexInterface>(); Queue <IVertexInterface> vertexQueue = new Queue <IVertexInterface>(); vertex = graph.GetFirstVertex(); vertexQueue.Enqueue(vertex); firstPartite.Add(vertex); while (vertexQueue.Count != 0) { vertex = vertexQueue.Dequeue(); neighboursVertexList = graph.Neighbours(vertex); if (firstPartite.Contains(vertex)) { isFirstPartite = true; } else { isFirstPartite = false; } foreach (IVertexInterface neighbourVertex in neighboursVertexList) { if (!firstPartite.Contains(neighbourVertex) && !secondPartite.Contains(neighbourVertex)) { if (isFirstPartite) { secondPartite.Add(neighbourVertex); } else { firstPartite.Add(neighbourVertex); } vertexQueue.Enqueue(neighbourVertex); } else { if ((firstPartite.Contains(neighbourVertex) && isFirstPartite) || (secondPartite.Contains(neighbourVertex) && !isFirstPartite)) { isBipartite = false; } } } } if (!isBipartite) { return(new Tuple <bool, bool>(false, false)); } // Set bipartites graph.GetGraphProperty().SetPartites(firstPartite.ToList(), secondPartite.ToList()); // Variable int countFirstPartiteVertex = firstPartite.Count; int countSecondPartiteVertex = secondPartite.Count; // Is the graph a complete bipartite graph foreach (Vertex firstPartiteVertex in firstPartite) { if (graph.CountNeighbours(firstPartiteVertex) != countSecondPartiteVertex) { return(new Tuple <bool, bool>(true, false)); } } foreach (Vertex secondPartiteVertex in secondPartite) { if (graph.CountNeighbours(secondPartiteVertex) != countFirstPartiteVertex) { return(new Tuple <bool, bool>(true, false)); } } return(new Tuple <bool, bool>(true, true)); }
public Graph(IGraphInterface graph) { InitializeComponent(); this.graph = graph; }
public void EdgeSubdivision(IEdgeInterface edge) { // Variable IVertexInterface vertex1 = graph.GetVertexByUserName(edge.GetVertex1().GetUserName()); IVertexInterface vertex2 = graph.GetVertexByUserName(edge.GetVertex2().GetUserName()); // Component if (componentsList != null) { if (countComponents > 1) { // Get component which contains the vertex IGraphInterface myComponentGraph = null; foreach (IGraphInterface componentGraph in componentsList) { if (componentGraph.ExistsUserName(vertex1.GetUserName())) { myComponentGraph = componentGraph; break; } } myComponentGraph.EdgeSubdivision(new Edge(myComponentGraph.GetVertexByUserName(vertex1.GetUserName()), myComponentGraph.GetVertexByUserName(vertex2.GetUserName()))); } else { //componentsList = componentsList; //countComponents = countComponents; //isConnected = isConnected; } } // Properties //isCyclic = isCyclic; //isEulerian = isEulerian; // IntegralInvariants //circuitRank = circuitRank; if (girth.HasValue && girth > 2) { girth = null; } cayleysFormula = null; // SequencesAndPolynomialsOthers if (isConnected != null && !(bool)isConnected) { spanningTreeBFS = spanningTreeBFS = new List <IEdgeInterface>(); } else { spanningTreeBFS = null; } matching = null; cutVertices = null; bridges = null; // DegreeSequence degreeSequence = null; maximumVertexDegree = null; minimumVertexDegree = null; averageVertexDegree = null; medianVertexDegree = null; isDegreeSequenceSorted = false; isRegular = null; // Chordal if (isChordal.HasValue && (bool)isChordal) { isChordal = null; perfectEliminationOrderingList = null; righNeighborhoodDictionary = null; } // GraphClass graphClass = GraphClass.GraphClass.GraphClassEnum.undefined; }
private void Invalid(IGraphInterface graph) { // Variable List <IVertexInterface> vertexList = graph.AllVertices(); List <IVertexInterface> vertexNot2DegreeList = new List <IVertexInterface>(); vertexNot2DegreeList = vertexList.Where(v => graph.CountNeighbours(v) != 2).ToList(); IEdgeInterface edge = new Edge(vertexList.First(), graph.Neighbours(vertexList.First()).First()); // Vertex add stringBuilder.AppendLine("Vertex add"); stringBuilder.AppendLine("Vertex exists"); try { graph.VertexAdd(vertexList.First()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Vertex delete stringBuilder.AppendLine("Vertex delete"); stringBuilder.AppendLine("Vertex doesn't exist"); try { graph.VertexDelete(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Vertex contract stringBuilder.AppendLine("Vertex contract"); stringBuilder.AppendLine("Vertex doesn't exist"); try { graph.VertexContraction(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Vertex suppression stringBuilder.AppendLine("Vertex suppression"); stringBuilder.AppendLine("Vertex doesn't exist"); try { graph.VertexSuppression(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } stringBuilder.AppendLine("Invalid vertex degree"); try { graph.VertexSuppression(vertexNot2DegreeList.First()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Vertex expansion stringBuilder.AppendLine("Vertex expansion"); stringBuilder.AppendLine("Vertex doesn't exist"); try { graph.VertexExpansion(new Vertex()); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Edge add stringBuilder.AppendLine("Edge add"); stringBuilder.AppendLine("Edge exists"); try { graph.EdgeAdd(edge); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Edge delete stringBuilder.AppendLine("Edge delete"); stringBuilder.AppendLine("Edge doesn't exist"); try { graph.EdgeDelete(new Edge(new Vertex(), new Vertex())); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Edge contract stringBuilder.AppendLine("Edge contract"); stringBuilder.AppendLine("Edge doesn't exist"); try { graph.EdgeContraction(new Edge(new Vertex(), new Vertex())); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } // Edge subdivision stringBuilder.AppendLine("Edge subdivision"); stringBuilder.AppendLine("Edge doesn't exist"); try { graph.EdgeSubdivision(new Edge(new Vertex(), new Vertex())); } catch (MyException.GraphException.GraphException e) { stringBuilder.AppendLine(e.Message); } }