Example #1
0
        public void IsBipartite_MoreComponents()
        {
            {
                var verticesCount = 5;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));

                graph.AddEdge(0, 1);
                graph.AddEdge(1, 2);
                graph.AddEdge(2, 0);
                graph.AddEdge(3, 4);

                Assert.IsFalse(bipartiteCheck.IsBipartite(graph, out var parts));
            }

            {
                var verticesCount = 5;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));

                graph.AddEdge(0, 1);
                graph.AddEdge(1, 2);
                graph.AddEdge(3, 4);

                Assert.IsTrue(bipartiteCheck.IsBipartite(graph, out var parts));
                CheckParts(parts.Item1, parts.Item2, graph);
            }
        }
        protected virtual List <NamedGraph> GetGraphSet(string name, int count)
        {
            var graphs = new List <NamedGraph>();

            for (int i = 0; i < count; i++)
            {
                var filename = $"Resources/RandomGraphs/{name}/{i}.txt";
                var lines    = File.ReadAllLines(filename);

                var graph = new UndirectedAdjacencyListGraph <int>();

                // Add vertices
                var verticesCount = int.Parse(lines[0]);
                for (var vertex = 0; vertex < verticesCount; vertex++)
                {
                    graph.AddVertex(vertex);
                }

                // Add edges
                for (var j = 1; j < lines.Length; j++)
                {
                    var line = lines[j];
                    var edge = line.Split(' ').Select(int.Parse).ToList();
                    graph.AddEdge(edge[0], edge[1]);
                }

                graphs.Add(new NamedGraph(graph, $"{name} {i}"));
            }

            return(graphs);
        }
Example #3
0
        public void IsBipartite_OddCycles_ReturnsFalse()
        {
            {
                var verticesCount = 3;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddEdge(x, (x + 1).Mod(verticesCount)));

                Assert.IsFalse(bipartiteCheck.IsBipartite(graph, out var parts));
            }

            {
                var verticesCount = 11;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddEdge(x, (x + 1).Mod(verticesCount)));

                Assert.IsFalse(bipartiteCheck.IsBipartite(graph, out var parts));
            }

            {
                var verticesCount = 25;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddEdge(x, (x + 1).Mod(verticesCount)));

                Assert.IsFalse(bipartiteCheck.IsBipartite(graph, out var parts));
            }
        }
Example #4
0
        /// <inheritdoc />
        public virtual IGraph <int> GetGraph()
        {
            IsLocked = true;
            var vertices        = Enumerable.Range(0, Rooms.Count);
            var graph           = new UndirectedAdjacencyListGraph <int>();
            var corridorCounter = Rooms.Count;

            foreach (var vertex in vertices)
            {
                graph.AddVertex(vertex);
            }

            foreach (var passage in Passages)
            {
                if (IsWithCorridors)
                {
                    graph.AddVertex(corridorCounter);
                    graph.AddEdge(Rooms[passage.Item1], corridorCounter);
                    graph.AddEdge(corridorCounter, Rooms[passage.Item2]);
                    corridorCounter++;
                }
                else
                {
                    graph.AddEdge(Rooms[passage.Item1], Rooms[passage.Item2]);
                }
            }

            return(graph);
        }
Example #5
0
        /// <remarks>
        /// Figure 7 bottom graph from the original paper.
        /// </remarks>
        /// <returns></returns>
        public static IGraph <int> GetExample3()
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 17);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(1, 7);
            graph.AddEdge(1, 10);
            graph.AddEdge(2, 3);
            graph.AddEdge(2, 5);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(4, 6);
            graph.AddEdge(6, 11);
            graph.AddEdge(7, 8);
            graph.AddEdge(8, 9);
            graph.AddEdge(9, 16);
            graph.AddEdge(10, 11);
            graph.AddEdge(10, 13);
            graph.AddEdge(11, 12);
            graph.AddEdge(12, 13);
            graph.AddEdge(12, 14);
            graph.AddEdge(14, 15);
            graph.AddEdge(15, 16);

            return(graph);
        }
        public void GetReport_OneCluster()
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(0, 4);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);

            var report = analyzer.GetReport(graph);

            Assert.That(report.Clusters.Count, Is.EqualTo(1));
            Assert.That(report.Clusters[0].Nodes.Count, Is.EqualTo(5));
            Assert.That(report.Clusters[0].Cycles.Count, Is.EqualTo(6));
            Assert.That(report.MaxDensity, Is.EqualTo(6 / 5d));
        }
Example #7
0
        public void IsConnected_NotConnected_NoEdges()
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 20);

            Assert.IsFalse(graphUtils.IsConnected(graph));
        }
        /// <summary>
        /// Compute a minimum vertex cover of a given bipartite graph.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="edges"></param>
        /// <returns></returns>
        public Tuple <List <int>, List <int> > BipartiteVertexCover(int left, int right, List <Tuple <int, int> > edges)
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            for (var i = 0; i < left + right; i++)
            {
                graph.AddVertex(i);
            }
            foreach (var e in edges)
            {
                graph.AddEdge(e.Item1, e.Item2);
            }

            var matching = hopcroftKarp.GetMaximumMatching(graph);

            var matchV = new int?[left + right];
            var matchU = new int?[left + right];

            foreach (var e in matching)
            {
                matchV[e.From] = e.To;
                matchU[e.To]   = e.From;
            }

            var visitU = new bool[left + right];
            var visitV = new bool[left + right];

            for (var i = left; i < left + right; i++)
            {
                if (!matchU[i].HasValue)
                {
                    Alternate(i, graph, visitU, visitV, matchV);
                }
            }

            var leftNodes = new List <int>();

            for (var i = 0; i < right; i++)
            {
                if (visitV[i])
                {
                    leftNodes.Add(i);
                }
            }

            var rightNodes = new List <int>();

            for (var i = left; i < left + right; i++)
            {
                if (!visitU[i])
                {
                    rightNodes.Add(i);
                }
            }

            return(new Tuple <List <int>, List <int> >(leftNodes, rightNodes));
        }
Example #9
0
        public void IsBipartite_CompleteBipartite_ReturnsTrue()
        {
            {
                var leftSize      = 3;
                var rightSize     = 4;
                var verticesCount = leftSize + rightSize;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));
                Enumerable.Range(0, leftSize)
                .ToList()
                .ForEach(x =>
                         Enumerable.Range(leftSize, rightSize)
                         .ToList()
                         .ForEach(y => graph.AddEdge(x, y))
                         );

                Assert.IsTrue(bipartiteCheck.IsBipartite(graph, out var parts));
                CheckParts(parts.Item1, parts.Item2, graph);
            }

            {
                var leftSize      = 10;
                var rightSize     = 1;
                var verticesCount = leftSize + rightSize;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));
                Enumerable.Range(0, leftSize)
                .ToList()
                .ForEach(x =>
                         Enumerable.Range(leftSize, rightSize)
                         .ToList()
                         .ForEach(y => graph.AddEdge(x, y))
                         );

                Assert.IsTrue(bipartiteCheck.IsBipartite(graph, out var parts));
                CheckParts(parts.Item1, parts.Item2, graph);
            }

            {
                var leftSize      = 10;
                var rightSize     = 21;
                var verticesCount = leftSize + rightSize;
                var graph         = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));
                Enumerable.Range(0, leftSize)
                .ToList()
                .ForEach(x =>
                         Enumerable.Range(leftSize, rightSize)
                         .ToList()
                         .ForEach(y => graph.AddEdge(x, y))
                         );

                Assert.IsTrue(bipartiteCheck.IsBipartite(graph, out var parts));
                CheckParts(parts.Item1, parts.Item2, graph);
            }
        }
Example #10
0
            private List <NamedGraph> GetGraphSetFromSingleFile(string name, int count)
            {
                var cycleClustersAnalyzer = new CycleClustersAnalyzer <int>();
                var graphs = new List <NamedGraph>();
                var lines  = File.ReadAllLines($"Resources/RandomGraphs/{name}.txt");

                if (File.Exists($"Resources/RandomGraphs/{name}_{maxClusterSize}.txt"))
                {
                    lines = File.ReadAllLines($"Resources/RandomGraphs/{name}_{maxClusterSize}.txt");
                }

                var i           = 0;
                var lineCounter = 0;

                while (graphs.Count < count)
                {
                    if (lineCounter >= lines.Length)
                    {
                        break;
                    }

                    var graph = new UndirectedAdjacencyListGraph <int>();

                    // Add vertices
                    var verticesCount = int.Parse(lines[lineCounter++]);
                    for (var vertex = 0; vertex < verticesCount; vertex++)
                    {
                        graph.AddVertex(vertex);
                    }

                    // Add edges
                    while (true)
                    {
                        var line = lines[lineCounter++];

                        if (string.IsNullOrEmpty(line))
                        {
                            break;
                        }

                        var edge = line.Split(' ').Select(int.Parse).ToList();
                        graph.AddEdge(edge[0], edge[1]);
                    }

                    var clustersReport = cycleClustersAnalyzer.GetReport(graph);

                    if (clustersReport.MaxCyclesInCluster <= this.maxClusterSize)
                    {
                        graphs.Add(new NamedGraph(graph, $"{name} {i}"));
                    }

                    i++;
                }

                return(graphs);
            }
Example #11
0
        public void IsBipartite_NoEdges_ReturnsTrue()
        {
            var verticesCount = 5;
            var graph         = new UndirectedAdjacencyListGraph <int>();

            Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));

            Assert.IsTrue(bipartiteCheck.IsBipartite(graph, out var parts));
            CheckParts(parts.Item1, parts.Item2, graph);
        }
Example #12
0
        /// <remarks>
        /// Figure 8 graph from the original paper.
        /// </remarks>
        /// <returns></returns>
        public static IGraph <int> GetExample4()
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 41);

            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(1, 2);
            graph.AddEdge(1, 9);
            graph.AddEdge(2, 5);
            graph.AddEdge(3, 6);
            graph.AddEdge(4, 7);
            graph.AddEdge(4, 8);
            graph.AddEdge(5, 6);
            graph.AddEdge(5, 10);
            graph.AddEdge(6, 11);
            graph.AddEdge(7, 12);
            graph.AddEdge(8, 13);
            graph.AddEdge(9, 10);
            graph.AddEdge(11, 12);
            graph.AddEdge(11, 18);
            graph.AddEdge(12, 19);
            graph.AddEdge(12, 20);
            graph.AddEdge(13, 14);
            graph.AddEdge(13, 20);
            graph.AddEdge(14, 15);
            graph.AddEdge(15, 26);
            graph.AddEdge(16, 22);
            graph.AddEdge(17, 22);
            graph.AddEdge(18, 19);
            graph.AddEdge(18, 23);
            graph.AddEdge(21, 24);
            graph.AddEdge(22, 23);
            graph.AddEdge(22, 27);
            graph.AddEdge(22, 28);
            graph.AddEdge(22, 29);
            graph.AddEdge(23, 24);
            graph.AddEdge(24, 30);
            graph.AddEdge(25, 26);
            graph.AddEdge(26, 32);
            graph.AddEdge(31, 32);
            graph.AddEdge(32, 33);
            graph.AddEdge(32, 35);
            graph.AddEdge(34, 35);
            graph.AddEdge(35, 36);
            graph.AddEdge(35, 38);
            graph.AddEdge(37, 38);
            graph.AddEdge(38, 39);
            graph.AddEdge(39, 40);

            return(graph);
        }
Example #13
0
        public static List <NamedGraph <int> > GetRandomGraphs(int count, int vertices, int nonTreeEdges)
        {
            var name     = $"e_{nonTreeEdges}_{nonTreeEdges}_v_{vertices}_{vertices + 9}";
            var filename = $"Resources/RandomGraphs/{name}.txt";

            if (!File.Exists(filename))
            {
                throw new ArgumentException("Incorrect combination of vertices and edges!");
            }

            var lines  = File.ReadAllLines(filename);
            var graphs = new List <NamedGraph <int> >();

            var i           = 0;
            var lineCounter = 0;

            while (graphs.Count < count)
            {
                if (lineCounter >= lines.Length)
                {
                    break;
                }

                var graph = new UndirectedAdjacencyListGraph <int>();

                // Add vertices
                var verticesCount = int.Parse(lines[lineCounter++]);
                for (var vertex = 0; vertex < verticesCount; vertex++)
                {
                    graph.AddVertex(vertex);
                }

                // Add edges
                while (true)
                {
                    var line = lines[lineCounter++];

                    if (string.IsNullOrEmpty(line))
                    {
                        break;
                    }

                    var edge = line.Split(' ').Select(int.Parse).ToList();
                    graph.AddEdge(edge[0], edge[1]);
                }

                graphs.Add(new NamedGraph(graph, $"{name} {i}"));

                i++;
            }

            return(graphs);
        }
Example #14
0
        public void IsConnected_Connected_C3()
        {
            // C_3
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 3);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);

            Assert.IsTrue(graphUtils.IsConnected(graph));
        }
Example #15
0
            protected override List <NamedGraph> GetGraphSet(string name, int count)
            {
                return(GetGraphSetFromSingleFile(name, count));

                var cycleClustersAnalyzer = new CycleClustersAnalyzer <int>();
                var graphs = new List <NamedGraph>();

                var i = 0;

                while (graphs.Count < count)
                {
                    var filename = $"Resources/RandomGraphs/{name}/{i}.txt";

                    if (!File.Exists(filename))
                    {
                        break;
                    }

                    var lines = File.ReadAllLines(filename);

                    var graph = new UndirectedAdjacencyListGraph <int>();

                    // Add vertices
                    var verticesCount = int.Parse(lines[0]);
                    for (var vertex = 0; vertex < verticesCount; vertex++)
                    {
                        graph.AddVertex(vertex);
                    }

                    // Add edges
                    for (var j = 1; j < lines.Length; j++)
                    {
                        var line = lines[j];
                        var edge = line.Split(' ').Select(int.Parse).ToList();
                        graph.AddEdge(edge[0], edge[1]);
                    }

                    var clustersReport = cycleClustersAnalyzer.GetReport(graph);

                    if (clustersReport.MaxCyclesInCluster <= this.maxClusterSize)
                    {
                        graphs.Add(new NamedGraph(graph, $"{name} {i}"));
                    }

                    i++;
                }

                return(graphs);
            }
Example #16
0
        public void OneToMany_ReturnsOne()
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            Enumerable.Range(0, 4).ToList().ForEach(x => graph.AddVertex(x));

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);

            var matching = hopcroftKarp.GetMaximumMatching(graph);

            Assert.AreEqual(1, matching.Count);
            CheckMatching(graph, matching);
        }
Example #17
0
        public void IsBipartite_NotBipartite_ReturnsFalse()
        {
            var verticesCount = 5;
            var graph         = new UndirectedAdjacencyListGraph <int>();

            Enumerable.Range(0, verticesCount).ToList().ForEach(x => graph.AddVertex(x));

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(2, 4);
            graph.AddEdge(3, 4);

            Assert.IsFalse(bipartiteCheck.IsBipartite(graph, out var parts));
        }
Example #18
0
        private IGraph <int> ConstructGraph(TestData data)
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            for (int i = 0; i < data.VerticesCount; i++)
            {
                graph.AddVertex(i);
            }

            foreach (var edge in data.Edges)
            {
                graph.AddEdge(edge.Source, edge.Target);
            }

            return(graph);
        }
Example #19
0
        public void IsConnected_NotConnected_TwoC3s()
        {
            // Two not connected C_3s
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 6);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);

            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(5, 3);

            Assert.IsFalse(graphUtils.IsConnected(graph));
        }
Example #20
0
        /// <summary>
        /// Gets the graph of rooms with all rooms (including corridor rooms)
        /// </summary>
        /// <returns></returns>
        public IGraph <TRoom> GetGraph()
        {
            var graph = new UndirectedAdjacencyListGraph <TRoom>();

            foreach (var room in roomDescriptions.Keys)
            {
                graph.AddVertex(room);
            }

            foreach (var passage in passages)
            {
                graph.AddEdge(passage.Room1, passage.Room2);
            }

            CheckIfValid(graph);

            return(graph);
        }
Example #21
0
        /// <summary>
        /// Creates a cycle graph on a given number of vertices.
        /// </summary>
        /// <param name="verticesCount"></param>
        /// <returns></returns>
        private IGraph <int> GetOneCycle(int verticesCount)
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            for (int i = 0; i < verticesCount; i++)
            {
                graph.AddVertex(i);
            }

            for (int i = 0; i < verticesCount - 1; i++)
            {
                graph.AddEdge(i, i + 1);
            }

            graph.AddEdge(verticesCount - 1, 0);

            return(graph);
        }
Example #22
0
        public void EightVertices_ReturnsFour()
        {
            // https://www.geeksforgeeks.org/wp-content/uploads/HopcroftKarp1.png
            var graph = new UndirectedAdjacencyListGraph <int>();

            Enumerable.Range(0, 8).ToList().ForEach(x => graph.AddVertex(x));

            graph.AddEdge(0, 5);
            graph.AddEdge(0, 6);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 5);
            graph.AddEdge(3, 5);
            graph.AddEdge(3, 7);

            var matching = hopcroftKarp.GetMaximumMatching(graph);

            Assert.AreEqual(4, matching.Count);
            CheckMatching(graph, matching);
        }
Example #23
0
        /// <inheritdoc />
        public virtual IGraph <int> GetGraphWithoutCorrridors()
        {
            // TODO: keep dry
            IsLocked = true;
            var vertices = Enumerable.Range(0, Rooms.Count);
            var graph    = new UndirectedAdjacencyListGraph <int>();

            foreach (var vertex in vertices)
            {
                graph.AddVertex(vertex);
            }

            foreach (var passage in Passages)
            {
                graph.AddEdge(Rooms[passage.Item1], Rooms[passage.Item2]);
            }

            return(graph);
        }
Example #24
0
        public static IGraph <int> GetCompleteGraph(int verticesCount)
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            for (int i = 0; i < verticesCount; i++)
            {
                graph.AddVertex(i);
            }

            for (int i = 0; i < verticesCount - 1; i++)
            {
                for (int j = i + 1; j < verticesCount; j++)
                {
                    graph.AddEdge(i, j);
                }
            }

            return(graph);
        }
Example #25
0
        public void GetCycles_TwoCyclesWithSharedEdge()
        {
            var graphCyclesGetter = new GraphCyclesGetter <int>();
            var graph             = new UndirectedAdjacencyListGraph <int>();

            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 0);

            var expectedCycles = new List <List <int> >()
            {
                new List <int>()
                {
                    0, 1, 2
                },
                new List <int>()
                {
                    0, 2, 3
                },
                new List <int>()
                {
                    0, 1, 2, 3
                },
            };

            var cycles = graphCyclesGetter.GetCycles(graph);

            Assert.That(cycles.Count, Is.EqualTo(expectedCycles.Count));

            foreach (var cycle in cycles)
            {
                var index = expectedCycles.FindIndex(x => x.SequenceEqualWithoutOrder(cycle));
                Assert.That(index, Is.GreaterThanOrEqualTo(0));
                expectedCycles.RemoveAt(index);
            }
        }
Example #26
0
        public void IsConnected_Connected_TwoC3s()
        {
            // Two C_3s connected by an edge
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 6);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);

            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(5, 3);

            graph.AddEdge(0, 3);

            Assert.IsTrue(graphUtils.IsConnected(graph));
        }
Example #27
0
        public void CompleteGraph_ReturnsFive()
        {
            var graph     = new UndirectedAdjacencyListGraph <int>();
            var leftSize  = 5;
            var rightSize = 6;

            Enumerable.Range(0, leftSize + rightSize).ToList().ForEach(x => graph.AddVertex(x));
            Enumerable.Range(0, leftSize)
            .ToList()
            .ForEach(x =>
                     Enumerable.Range(leftSize, rightSize)
                     .ToList()
                     .ForEach(y => graph.AddEdge(x, y))
                     );

            var matching = hopcroftKarp.GetMaximumMatching(graph);

            Assert.AreEqual(5, matching.Count);
            CheckMatching(graph, matching);
        }
Example #28
0
        /// <remarks>
        /// Figure 7 top graph from the original paper.
        /// </remarks>
        /// <returns></returns>
        public static IGraph <int> GetExample2()
        {
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 9);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 3);
            graph.AddEdge(1, 2);
            graph.AddEdge(1, 4);
            graph.AddEdge(1, 5);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 6);
            graph.AddEdge(4, 5);
            graph.AddEdge(6, 7);
            graph.AddEdge(6, 8);
            graph.AddEdge(7, 8);

            return(graph);
        }
Example #29
0
        public void IsPlanar_NotPlanar_K5()
        {
            // K_5
            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVerticesRange(0, 5);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(0, 4);
            graph.AddEdge(1, 2);
            graph.AddEdge(1, 3);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 3);
            graph.AddEdge(2, 4);
            graph.AddEdge(3, 4);

            Assert.IsFalse(graphUtils.IsPlanar(graph));
        }
        public void GetChains_BasicCounts()
        {
            {
                // Two C_3s connected by a common vertex
                var graph = new UndirectedAdjacencyListGraph <int>();

                graph.AddVertex(0);
                graph.AddVertex(1);
                graph.AddVertex(2);
                graph.AddVertex(3);
                graph.AddVertex(4);

                graph.AddEdge(0, 1);
                graph.AddEdge(1, 2);
                graph.AddEdge(2, 0);
                graph.AddEdge(1, 3);
                graph.AddEdge(3, 4);
                graph.AddEdge(4, 1);

                var chains = chainDecomposition.GetChains(graph);

                Assert.IsTrue(graph.Vertices.SequenceEqualWithoutOrder(chains.SelectMany(x => x).Distinct()));
            }

            {
                // Two intersecting paths
                var graph = new UndirectedAdjacencyListGraph <int>();
                Enumerable.Range(0, 7).ToList().ForEach(x => graph.AddVertex(x));

                graph.AddEdge(0, 1);
                graph.AddEdge(1, 2);
                graph.AddEdge(2, 3);
                graph.AddEdge(4, 1);
                graph.AddEdge(1, 5);
                graph.AddEdge(5, 6);

                var chains = chainDecomposition.GetChains(graph);

                Assert.IsTrue(graph.Vertices.SequenceEqualWithoutOrder(chains.SelectMany(x => x).Distinct()));
            }
        }