public Graph.IGraphInterface GenerateGraph() { // Variable int probabilityEdge; foreach (Graph.IVertexInterface firstVertex in graph.AllVertices()) { foreach (Graph.IVertexInterface secondVertex in graph.AllVertices()) { // No duplicates if (usedVerticesHashSet.Contains(secondVertex)) { continue; } if (firstVertex == secondVertex) { continue; } probabilityEdge = random.Next(0, 100000); if (probabilityEdge <= probability) { graph.EdgeAdd(new Graph.Edge(firstVertex, secondVertex)); } } usedVerticesHashSet.Add(firstVertex); } usedVerticesHashSet = null; return(graph); }
/// <summary> /// Write a graph to a file /// If the graph is not initialized throws GraphInitializationException /// </summary> /// <param name="graph">graph</param> /// <returns>Is everything OK</returns> public bool WriteFile(Graph.IGraphInterface graph) { if (!graph.GetIsInitialized()) { throw new MyException.GraphException.GraphInitializationException(); } if (ExistsFile()) { DeleteFile(); } CreateFile(); using (StreamWriter streamWriter = new StreamWriter(GetPath())) { // Header streamWriter.WriteLine(READERWRITERHEADER + Graph.Graph.GraphRepresentationEnum.adjacencyList); streamWriter.WriteLine(READERWRITERBALLAST); streamWriter.WriteLine(READERWRITERNAME + graph.GetName()); streamWriter.WriteLine(READERWRITERCOUNTVERTICES + graph.GetRealCountVertices()); streamWriter.WriteLine(READERWRITERBALLAST); // Graph streamWriter.WriteLine(READERWRITERGRAPH); foreach (Graph.Vertex vertex in graph.AllVertices()) { streamWriter.WriteLine(vertex.GetUserName()); foreach (Graph.Vertex neighbour in graph.Neighbours(vertex)) { streamWriter.WriteLine(LEFTSEPARATORADJACENCYLIST + neighbour.GetUserName() + RIGHTSEPARATORADJACENCYLIST); } } // Colored graph streamWriter.WriteLine(READERWRITERBALLAST); streamWriter.Write(READERWRITERCOLOREDGRAPH); } return(true); }
/// <summary> /// Return true if a graph has been colored in polynomal time, otherwise false /// </summary> /// <param name="graph">graph</param> /// <returns>true if graph has been colored in polynomal time, otherwise false</returns> public static bool TryOptimalColorInPolynomalTime(Graph.IGraphInterface graph) { bool isColored; IGraphColoringAlgorithmInterface algorithm; List <Graph.IVertexInterface> optimalVertexList; Graph.IColoredGraphInterface coloredGraph = graph.GetColoredGraph(); coloredGraph.ResetColors(); // If the graph is chordal => use PEO for coloring if (graph.GetGraphProperty().GetIsChordal()) { optimalVertexList = graph.GetGraphProperty().GetPerfectEliminationOrdering(); coloredGraph.GreedyColoring(optimalVertexList); isColored = coloredGraph.InitializeColoredGraph(); if (!isColored) { throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored(); } return(true); } switch (graph.GetGraphProperty().GetGraphClass()) { case Graph.GraphClass.GraphClass.GraphClassEnum.bipartiteGraph: Tuple <List <Graph.IVertexInterface>, List <Graph.IVertexInterface> > partites = graph.GetGraphProperty().GetPartites(); coloredGraph.GreedyColoring(partites.Item1.Concat(partites.Item2).ToList()); isColored = coloredGraph.InitializeColoredGraph(); if (!isColored) { throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored(); } return(true); case Graph.GraphClass.GraphClass.GraphClassEnum.completeBipartiteGraph: algorithm = new SequenceAlgorithm.SmallestLastSequence.SmallestLastSequence(graph); algorithm.Color(); return(true); case Graph.GraphClass.GraphClass.GraphClassEnum.completeGraph: coloredGraph.GreedyColoring(graph.AllVertices()); isColored = coloredGraph.InitializeColoredGraph(); if (!isColored) { throw new MyException.GraphColoringAlgorithmException.AlgorithmGraphIsNotColored(); } return(true); case Graph.GraphClass.GraphClass.GraphClassEnum.cycleGraph: case Graph.GraphClass.GraphClass.GraphClassEnum.treeGraph: algorithm = new SequenceAlgorithm.ConnectedSequential.ConnectedSequential(graph); algorithm.Color(); return(true); } return(false); }