Exemplo n.º 1
0
        public CycleClustersReport <TNode> GetReport(IGraph <TNode> graph)
        {
            var cyclesGetter = new GraphCyclesGetter <TNode>();
            var cycles       = cyclesGetter.GetCycles(graph);
            var clusters     = GetClusters(cycles);

            var maxDensity = 0d;

            foreach (var cluster in clusters)
            {
                if (cluster.Cycles.Count <= 3)
                {
                    continue;
                }

                var density = cluster.Cycles.Count / (double)cluster.Nodes.Count;

                if (density > maxDensity)
                {
                    maxDensity = density;
                }
            }

            return(new CycleClustersReport <TNode>()
            {
                Clusters = clusters,
                MaxDensity = maxDensity,
                MaxCyclesInCluster = clusters.Count != 0 ? clusters.Max(x => x.Cycles.Count) : 0,
            });
        }
        public NodesInsideCycleReport <TNode> GetReport(IGraph <TNode> graph)
        {
            var cyclesGetter  = new GraphCyclesGetter <TNode>();
            var cycles        = cyclesGetter.GetCycles(graph);
            var problemsCount = 0;

            foreach (var cycle1 in cycles)
            {
                foreach (var cycle2 in cycles)
                {
                    if (cycle1 == cycle2)
                    {
                        continue;
                    }

                    var commonNodes = cycle1.Intersect(cycle2).ToList();
                    var cycle1Only  = cycle1.Except(commonNodes).ToList();
                    var cycle2Only  = cycle2.Except(commonNodes).ToList();

                    if (commonNodes.Count < 3)
                    {
                        continue;
                    }

                    var positiveCount = 0;
                    var threshold     = 1;

                    if (HasNonCycleNodes(graph, commonNodes) >= threshold)
                    {
                        positiveCount++;
                    }
                    if (HasNonCycleNodes(graph, cycle1Only) >= threshold)
                    {
                        positiveCount++;
                    }
                    if (HasNonCycleNodes(graph, cycle2Only) >= threshold)
                    {
                        positiveCount++;
                    }

                    if (positiveCount >= 2)
                    {
                        problemsCount++;
                    }
                }
            }

            return(new NodesInsideCycleReport <TNode>()
            {
                ProblemsCount = problemsCount,
            });
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        public void GetCycles_SingleCycle()
        {
            var graphCyclesGetter = new GraphCyclesGetter <int>();
            var graph             = new UndirectedAdjacencyListGraph <int>();

            var verticesCount = 4;

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

                if (i > 0)
                {
                    graph.AddEdge(i - 1, i);
                }
            }
            graph.AddEdge(0, verticesCount - 1);

            var cycles = graphCyclesGetter.GetCycles(graph);

            Assert.That(cycles.Count, Is.EqualTo(1));
            Assert.That(cycles[0], Is.EquivalentTo(graph.Vertices));
        }
Exemplo n.º 5
0
        public void GetCycles_MultipleCycles()
        {
            var graphCyclesGetter = new GraphCyclesGetter <int>();
            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 cycles = graphCyclesGetter.GetCycles(graph);

            Assert.That(cycles.Count, Is.EqualTo(6));
        }