Ejemplo n.º 1
0
        private bool Ok()
        {
            var p = PermutationUtils.First(n);

            do
            {
                bool[] could = new bool[1 << n];
                could[0] = true;
                for (int ii = 0; ii < n; ii++)
                {
                    int i = p[ii];

                    int mask = 0;
                    for (int j = 0; j < n; j++)
                    {
                        if (a[i, j] == 1)
                        {
                            mask |= (1 << j);
                        }
                    }
                    bool bad = false;
                    for (int j = 0; j < (1 << n); j++)
                    {
                        if (could[j] && (j & mask) == mask)
                        {
                            bad = true;
                            break;
                        }
                    }
                    if (bad)
                    {
                        return(false);
                    }

                    bool[] could2 = new bool[1 << n];
                    for (int j = 0; j < (1 << n); j++)
                    {
                        if (could[j])
                        {
                            for (int k = 0; k < n; k++)
                            {
                                if (a[i, k] == 1)
                                {
                                    could2[j | (1 << k)] = true;
                                }
                            }
                        }
                    }
                    could = could2;
                }
            } while (PermutationUtils.Next(p));
            return(true);
        }
Ejemplo n.º 2
0
        void DoMonotoneTest(MonotoneType monotoneType)
        {
            int n = 8;
            var p = PermutationUtils.First(n);

            do
            {
                var copyP = (int[])p.Clone();
                var my    = SequenceUtils.GetLongestMonotoneSubsequenceLength(copyP, monotoneType);
                Assert.IsTrue(p.SequenceEqual(copyP));
                var correct = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                Assert.AreEqual(correct, my);

                var mlist = SequenceUtils.GetLongestMonotoneSubsequence(copyP, monotoneType);
                Assert.IsTrue(p.SequenceEqual(copyP));

                Assert.AreEqual(my, mlist.Length);
                for (int i = 1; i < mlist.Length; ++i)
                {
                    Assert.IsTrue(mlist[i] >= 0 && mlist[i] < n);
                    Assert.IsTrue(mlist[i] > mlist[i - 1]);

                    switch (monotoneType)
                    {
                    case MonotoneType.Increasing:
                        Assert.IsTrue(p[mlist[i]] > p[mlist[i - 1]]);
                        break;

                    case MonotoneType.Decreasing:
                        Assert.IsTrue(p[mlist[i]] < p[mlist[i - 1]]);
                        break;

                    case MonotoneType.NonDecreasing:
                        Assert.IsTrue(p[mlist[i]] >= p[mlist[i - 1]]);
                        break;

                    case MonotoneType.NonIncreasing:
                        Assert.IsTrue(p[mlist[i]] <= p[mlist[i - 1]]);
                        break;

                    default:
                        throw new System.Exception("Unknown monotone type");
                    }
                }
            } while (PermutationUtils.Next(p));

            var rnd = new Random(123);

            for (int times = 0; times < 100; ++times)
            {
                n = rnd.Next(50) + 10;
                p = new int[n];
                for (int i = 0; i < n; i++)
                {
                    p[i] = rnd.Next(20);
                }
                var correct = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                var my      = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                Assert.AreEqual(correct, my);

                n = 8;
                p = new int[n];
                for (int i = 0; i < n; i++)
                {
                    p[i] = rnd.Next(5);
                }
                Array.Sort(p);
                do
                {
                    correct = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                    my      = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                    Assert.AreEqual(correct, my);
                } while (PermutationUtils.Next(p));
            }
        }