Exemplo n.º 1
0
        private static Graph ReadFromFile(string fileName)
        {
            //The format of line is:
            //   Id: id_neighbour_1, id_neighbour_2, ... , id_nieghbour_n
            var g = new Graph();
            var allLines = File.ReadAllLines(fileName);
            var splittedAllLines = allLines.Select(x => x.Split(':'));
            if (splittedAllLines.Any(x => x.Count() != 2))
                throw new InvalidFileFormatException();

            foreach (var id in splittedAllLines.Select(x => x[0]))
            {
                g.AddVertex(ParseVertexId(id));
            }
            foreach (var line in splittedAllLines)
            {
                var fromId = ParseVertexId(line[0]);
                var neighbours = line[1].Split(',');
                foreach (var neighbour in neighbours)
                {
                    int toId = ParseVertexId(neighbour);
                    if(!g.ContainsVertex(toId))
                        throw new InvalidFileFormatException();
                    g.AddDirectedEdge(fromId, toId);
                }
            }
            return g;
        }
Exemplo n.º 2
0
 public BipartiteTestingAlgorithm(Graph graph)
 {
     this.verticies = graph.AllVerticies.ToList();
     vertexColors = new Dictionary<int, Color?>();
     foreach (var vertex in graph.AllVerticies)
     {
         vertexColors[vertex.Id] = null;
     }
 }
Exemplo n.º 3
0
 private Edge? GetEdgeToExtend(Graph g, Edge e)
 {
     foreach (var e2 in NonplanarSubgraph.GetAllEdges())
     {
         if (!g.ContainsEdge(e2.U, e2.V) && g.ContainsEdge(e.V, e2.U) && g.ContainsEdge(e.V, e2.V))
         {
             return e2;
         }
     }
     return null;
 }
Exemplo n.º 4
0
 private void ExtendNonplanarSubgraph(Graph g, Edge e)
 {
     var toExtend = GetEdgeToExtend(g, e);
     if (toExtend != null)
     {
        NonplanarSubgraph.RemoveEdge(toExtend.Value);
        NonplanarSubgraph.AddVertex(e.V);
        NonplanarSubgraph.AddUndirectedEdge(e.V, toExtend.Value.V);
        NonplanarSubgraph.AddUndirectedEdge(e.V, toExtend.Value.U);
     }
 }
Exemplo n.º 5
0
 public static Graph CreateBipartiteGraph(int n, int m)
 {
     var graph = new Graph();
     graph.AddVerticiesRange(n + m);
     for (int i = 0; i < n; i++)
     {
         for (int j = n; j < n+m; j++)
         {
             graph.AddUndirectedEdge(i, j);
         }
     }
     return graph;
 }
Exemplo n.º 6
0
        public static Graph CreateCompleteGraph(int size)
        {
            var graph = new Graph();
            graph.AddVerticiesRange(size);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    graph.AddUndirectedEdge(i, j);
                }
            }
            return graph;
        }
Exemplo n.º 7
0
 private bool IsPlanar(Graph g)
 {
     Counter++;
     if (g.IsCompleteBipartite(3, 3) || g.IsComplete(5))
     {
         NonplanarSubgraph = g;
         return false;
     }
     if (g.Size == 5)
         return true;
     foreach (var edge in g.GetAllEdges())
     {
         var h = g.Shrink(edge.U, edge.V);
         var isPlanar = IsPlanar(h);
         if (!isPlanar)
         {
             ExtendNonplanarSubgraph(g, edge);
             return false;
         }
     }
     return true;
 }
Exemplo n.º 8
0
 public PlanarityTestingAlgorithm(Graph inputGraph)
 {
     this.inputGraph = inputGraph;
 }
Exemplo n.º 9
0
 public Graph Shrink(int n, int m)
 {
     var h = new Graph();
     foreach (var v in AllVerticies.Where(x => x.Id != m))
     {
         h.AddVertex(v.Id);
     }
     foreach (var vertex in AllVerticies.Where(x => x.Id != m))
     {
         foreach (var neighbour in vertex.AllNeighbours.Where(x => x.Id != m))
         {
             h.AddDirectedEdge(vertex.Id, neighbour.Id);
         }
     }
     var mVertex = verticies[m];
     foreach (var neighbourOfM in mVertex.AllNeighbours.Where(x => x.Id != n))
     {
         h.AddUndirectedEdge(n, neighbourOfM.Id);
     }
     return h;
 }