public void IsPlanar_OneCycleGraphs() { var boyerMyrvold = new BoyerMyrvold <int>(); const int minVertices = 3; const int maxVertices = 1000; for (int i = minVertices; i <= maxVertices; i++) { var graph = GetOneCycle(i); Assert.That(boyerMyrvold.IsPlanar(graph), Is.EqualTo(true)); } }
/// <summary> /// Checks if a given graph is planar. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="graph">Undirected graph.</param> /// <returns></returns> public bool IsPlanar <T>(IGraph <T> graph) { if (graph.IsDirected) { throw new ArgumentException("The graph must not be directed", nameof(graph)); } if (graph.VerticesCount == 0) { return(true); } var boyerMyrvold = new BoyerMyrvold <T>(); return(boyerMyrvold.IsPlanar(TransformGraph(graph))); }
public void IsPlanar_RandomNonPlanarGraphs() { var boyerMyrvold = new BoyerMyrvold <int>(); var random = new Random(); const int graphsCount = 1000; const int minVertices = 5; const int maxVertices = 1000; for (int i = 0; i < graphsCount; i++) { var verticesCount = random.Next(minVertices, maxVertices + 1); var edgesCount = Math.Min(verticesCount * (verticesCount - 1) / 2, 3 * verticesCount); var graph = GetRandomGraph(verticesCount, edgesCount, random); Assert.That(boyerMyrvold.IsPlanar(graph), Is.EqualTo(false)); } }
public void CompareWithBoost(string filename) { var path = TestContext.CurrentContext.TestDirectory + "\\Resources\\" + filename; var boyerMyrvold = new BoyerMyrvold <int>(); using (var reader = File.OpenText(path)) { while (!reader.EndOfStream) { var testData = LoadData(reader); var graph = ConstructGraph(testData); if (boyerMyrvold.IsPlanar(graph, out var embedding)) { Assert.That(testData.IsPlanar, Is.EqualTo(true)); foreach (var vertex in graph.Vertices) { var vertexEmbedding = embedding.GetEdgesAroundVertex(vertex); Assert.That(vertexEmbedding, Is.EquivalentTo(testData.Embedding[vertex])); } var planarFaceVisitor = new GetPlanarFacesVisitor <int>(); PlanarFaceTraversal.Traverse(graph, embedding, planarFaceVisitor); Assert.That(planarFaceVisitor.Faces.Count, Is.EqualTo(testData.Faces.Count)); for (var i = 0; i < planarFaceVisitor.Faces.Count; i++) { var face = planarFaceVisitor.Faces[i]; Assert.That(face, Is.EquivalentTo(testData.Faces[i])); } } else { Assert.That(testData.IsPlanar, Is.EqualTo(false)); } } } }
//https://github.com/OndrejNepozitek/GraphPlanarityTesting public static bool Calculate(Graph g) { var graph = new UndirectedAdjacencyListGraph <int>(); for (int i = 0; i < g.order; i++) { graph.AddVertex(i); } for (int i = 0; i < g.order; i++) { for (int j = i + 1; j < g.order; j++) { if (g.adjacencyMatrix[i, j] == 1) { graph.AddEdge(i, j); } } } var boyerMyrvold = new BoyerMyrvold <int>(); return(boyerMyrvold.IsPlanar(graph)); }
static void Main(string[] args) { List <List <int> > macierz = new List <List <int> >(); var s = new FileInfo(Directory.GetCurrentDirectory()); var s2 = s.Directory.Parent.Parent; String sciezka = s2.ToString() + "\\dane.csv"; using (var reader = new StreamReader(sciezka)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); List <int> zad = new List <int>(); for (int x = 0; x < values.Length; x++) { zad.Add(Convert.ToInt32(values[x])); } macierz.Add(zad); } } bool flaga = true; for (int x = 1; x <= macierz.Count; x++) { for (int y = x + 1; y <= macierz.Count; y++) { int jeden = dajPole(macierz, x, y); int dwa = dajPole(macierz, y, x); if (jeden != dwa) { flaga = false; } } } if (flaga == false) { Console.WriteLine("Podana macierz nie jest grafem"); Console.ReadKey(); return; } IGraph <int> graf = new UndirectedAdjacencyListGraph <int>(); for (int numer = 1; numer <= macierz.Count; numer++) { graf.AddVertex(numer); } for (int numer = 1; numer <= macierz.Count; numer++) { List <int> zad = macierz.ElementAt((numer - 1)); for (int i = numer; i <= zad.Count; i++) { int a = i - 1; if (zad.ElementAt(a) != 0) { graf.AddEdge(numer, (a + 1)); } } } var boyerMyrvold = new BoyerMyrvold <int>(); if (boyerMyrvold.IsPlanar(graf) == true) { Console.WriteLine("Graf jest planarny"); } else { Console.WriteLine("Graf nie jest planarny"); } Console.ReadKey(); }