Exemple #1
0
        /// <summary> Construct the edge short cycles for the
        ///  given initial cycles. </summary>
        public EdgeShortCycles(InitialCycles initialCycles)
        {
            int[][] graph  = initialCycles.Graph;
            int[]   sizeOf = new int[initialCycles.GetNumberOfEdges()];

            this.paths = new List <int[]>(initialCycles.GetNumberOfCycles());

            // cycles are returned ordered by length
            foreach (var cycle in initialCycles.GetCycles())
            {
                int   length = cycle.Length;
                int[] path   = cycle.Path;

                bool found = false;

                // check if any vertex is the shortest through a vertex in the path
                for (int i = 1; i < path.Length; i++)
                {
                    int idx = initialCycles.IndexOfEdge(path[i - 1], path[i]);
                    if (sizeOf[idx] < 1 || length <= sizeOf[idx])
                    {
                        found       = true;
                        sizeOf[idx] = length;
                    }
                }

                if (found)
                {
                    foreach (var p in cycle.GetFamily())
                    {
                        paths.Add(p);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary> Construct the vertex short cycles for the given initial cycles.</summary>
        public VertexShortCycles(InitialCycles initialCycles)
        {
            int[][] graph  = initialCycles.Graph;
            int[]   sizeOf = new int[graph.Length];

            this.paths = new List <int[]>(initialCycles.GetNumberOfCycles());

            // cycles are returned ordered by length
            foreach (var cycle in initialCycles.GetCycles())
            {
                int   length = cycle.Length;
                int[] path   = cycle.Path;

                bool found = false;

                // check if any vertex is the shortest through a vertex in the path
                foreach (var v in path)
                {
                    if (sizeOf[v] < 1 || length <= sizeOf[v])
                    {
                        found     = true;
                        sizeOf[v] = length;
                    }
                }

                if (found)
                {
                    foreach (var p in cycle.GetFamily())
                    {
                        paths.Add(p);
                    }
                }
            }
        }
Exemple #3
0
        public virtual void Cycles_cyclophane_odd_limit_5()
        {
            InitialCycles initial = new InitialCycles(CyclophaneOdd, 5);
            var           cycles  = initial.GetCycles();

            Assert.AreEqual(0, cycles.Count());
        }
Exemple #4
0
        public virtual void Cycles_K4()
        {
            InitialCycles initial = new InitialCycles(K4);

            // todo replace with hasSize (hamcrest)
            Assert.AreEqual(4, initial.GetCycles().Count());
            Assert.AreEqual(4, initial.GetCyclesOfLength(3).Count());
        }
Exemple #5
0
        public virtual void Cycles_cyclophane_odd_limit_7()
        {
            InitialCycles initial = new InitialCycles(CyclophaneOdd, 7);
            var           cycles  = initial.GetCycles();

            Assert.AreEqual(1, cycles.Count());
            int[][] expecteds = new int[][] {
                new int[] { 3, 2, 1, 0, 5, 4, 3 },
            };
            AssertInitialCycles(expecteds, cycles);
        }
Exemple #6
0
        public virtual void Cycles_cyclophane()
        {
            InitialCycles initial = new InitialCycles(CyclophaneOdd);
            var           cycles  = initial.GetCycles();

            Assert.AreEqual(2, cycles.Count());
            int[][] expecteds = new int[][] {
                new int[] { 3, 2, 1, 0, 5, 4, 3 },
                new int[] { 3, 2, 1, 0, 10, 9, 8, 7, 6, 3 },
            };
            AssertInitialCycles(expecteds, cycles);
        }
Exemple #7
0
        public virtual void Cycles_naphthalene()
        {
            InitialCycles initial = new InitialCycles(Naphthalene);
            var           cycles  = initial.GetCycles();

            Assert.AreEqual(2, cycles.Count());
            int[][] expecteds = new int[][] {
                new int[] { 5, 0, 1, 2, 3, 4, 5 },
                new int[] { 5, 4, 7, 8, 9, 6, 5 },
            };
            AssertInitialCycles(expecteds, cycles);
        }
Exemple #8
0
        public virtual void Cycles_bicyclo()
        {
            InitialCycles initial = new InitialCycles(Bicyclo);
            var           cycles  = initial.GetCycles();

            Assert.AreEqual(3, cycles.Count());
            int[][] expecteds = new int[][] {
                new int[] { 5, 0, 1, 2, 3, 4, 5 },
                new int[] { 5, 0, 1, 2, 7, 6, 5 },
                new int[] { 5, 4, 3, 2, 7, 6, 5 },
            };
            AssertInitialCycles(expecteds, cycles);
        }
Exemple #9
0
        public virtual void Cycles_anthracene()
        {
            InitialCycles initial = new InitialCycles(Anthracene);
            var           cycles  = initial.GetCycles();

            Assert.AreEqual(3, cycles.Count());

            int[][] expecteds = new int[][]
            {
                new int[] { 5, 0, 1, 2, 3, 4, 5 },
                new int[] { 9, 6, 5, 4, 7, 8, 9 },
                new int[] { 9, 8, 10, 11, 12, 13, 9 },
            };
            AssertInitialCycles(expecteds, cycles);
        }
Exemple #10
0
        /// <summary>
        /// Generate the minimum cycle basis from a precomputed set of initial
        /// cycles. This constructor allows on to specify that the graph is
        /// connected which allows an optimisation to be used.
        /// </summary>
        /// <param name="initial">set of initial cycles.</param>
        /// <param name="connected">the graph is known to be connected</param>
        /// <exception cref="System.ArgumentNullException">null InitialCycles provided</exception>
        internal MinimumCycleBasis(InitialCycles initial, bool connected)
        {
            CheckNotNull(initial, nameof(initial), "No InitialCycles provided");

            this.graph = initial.Graph;
            this.basis = new GreedyBasis(initial.GetNumberOfCycles(), initial.GetNumberOfEdges());

            // a undirected unweighted connected graph there are |E| - (|V| + 1)
            // cycles in the minimum basis
            int lim = connected ? initial.GetNumberOfEdges() - graph.Length + 1 : int.MaxValue;

            // processing by size add cycles which are independent of smaller cycles
            foreach (var cycle in initial.GetCycles())
            {
                if (basis.Count < lim && basis.IsIndependent(cycle))
                {
                    basis.Add(cycle);
                }
            }
        }
Exemple #11
0
        public virtual void Cycles_family_even()
        {
            InitialCycles initial = new InitialCycles(CyclophaneEven);
            var           cycles  = initial.GetCycles();

            int[] expected = new int[] { 3, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3 };
            var   cycle    = cycles.FirstOrDefault(n => Compares.AreDeepEqual(expected, n.Path));

            Assert.IsNotNull(cycle);
            int[][] family = cycle.GetFamily();
            Assert.AreEqual(2, family.Length);
            int[][] expecteds = new int[][] {
                expected,
                new int[] { 3, 6, 7, 8, 9, 10, 11, 0, 5, 4, 3 },
            };
            foreach (var f in family)
            {
                Assert.IsTrue(expecteds.Any(n => Compares.AreDeepEqual(n, f)));
            }
        }
Exemple #12
0
        public virtual void Cycles_K1()
        {
            InitialCycles initial = new InitialCycles(K1);

            Assert.AreEqual(0, initial.GetCycles().Count());
        }