コード例 #1
0
            public void ReturnsCorrectValueWhenMergingItemsInSameSet() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                Assert.True(fastDisjointSet.Union(0, 1));
                Assert.False(fastDisjointSet.Union(0, 1));
            }
コード例 #2
0
            public void ReturnsCorrectValueForItemsInSameSet() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                fastDisjointSet.Union(0, 1);

                Assert.True(fastDisjointSet.AreInSameSet(0, 1));
            }
コード例 #3
0
            public void InitiallyReturnsTheParentForEachSet() {
                const int n = 10;
                var fastDisjointSet = new FastDisjointSet(n);

                foreach (var i in Enumerable.Range(0, n)) {
                    var actual = fastDisjointSet.Find(i);
                    var expected = i;

                    Assert.Equal(expected, actual);
                }
            }
コード例 #4
0
            public void ReturnsCorrectParentAfterUnion() {
                // Create a Disjoint-Set with 10 elements
                // Union 2 groups of which one is bigger
                const int n = 10;
                var fastDisjointSet = new FastDisjointSet(n);

                fastDisjointSet.Union(0, 1);
                var parent0 = fastDisjointSet.Find(0);
                var parent1 = fastDisjointSet.Find(1);

                Assert.Equal(parent1, parent0);
            }
コード例 #5
0
            public void ReturnsCorrectValueForInitializedSets() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                var expected = Enumerable
                    .Range(0, 5)
                    .Select(e => (new[] {e}));

                var actual = fastDisjointSet.GetSets();

                Assert.Equal(expected, actual);
            }
コード例 #6
0
            public void ReturnsCorrectParentAfterMergingAllItems() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                foreach (var item in Enumerable.Range(1, n - 1)) {
                    fastDisjointSet.Union(0, item);
                }

                var parent = fastDisjointSet.Find(0);
                // There should be only one set with all parents the same
                foreach (var item in Enumerable.Range(1, n - 1)) {
                    var actual = fastDisjointSet.Find(item);
                    Assert.Equal(parent, actual);
                }
            }
コード例 #7
0
            public void ReturnsCorrectValueForFullyMergedSets() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                // Merge all items
                foreach (var item in Enumerable.Range(1, n - 1)) {
                    fastDisjointSet.Union(0, item);
                }

                var expected = new[] {
                    new[] {0, 1, 2, 3, 4},
                };

                var actual = fastDisjointSet.GetSets();

                Assert.Equal(expected, actual);
            }
コード例 #8
0
            public void CorrectlyMerges2Items() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                fastDisjointSet.Union(0, 1);
                Assert.Equal(fastDisjointSet.Find(0), fastDisjointSet.Find(1));

                // Assert other elements are all distinct
                const int othersCount = n - 2;
                var othersRange = Enumerable.Range(2, othersCount);

                var othersUniqueParentCount = othersRange
                    .Select(e => fastDisjointSet.Find(e))
                    .Distinct()
                    .Count();

                Assert.Equal(othersCount, othersUniqueParentCount);
            }
コード例 #9
0
            public void ReturnsCorrectValueForPartiallyMergedSets() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                // Make merges so structure looks like:
                // [[0,1],[2],[3,4]]
                fastDisjointSet.Union(0, 1);
                fastDisjointSet.Union(3, 4);

                var expected = new[] {
                    new[] {0, 1},
                    new[] {2},
                    new[] {3, 4},
                };

                var actual = fastDisjointSet.GetSets();

                Assert.Equal(expected, actual);
            }
コード例 #10
0
            public void ReturnsCorrectParentAfterUnionOfDifferentSetSizes() {
                // Create a Disjoint-Set with 10 elements
                // Make a group of 2 elements, and a group of 3, 
                // after merging, both groups should have the parent of the group of 3
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                fastDisjointSet.Union(0, 1);
                fastDisjointSet.Union(0, 2);
                fastDisjointSet.Union(3, 4);

                // Structure is now like following:
                // [0,1,2], [3,4]]
                var expected = fastDisjointSet.Find(0);

                fastDisjointSet.Union(0, 3);

                var actual = fastDisjointSet.Find(4);
                Assert.Equal(expected, actual);
            }
コード例 #11
0
        public static void UnweightedMinimize <T, S>(DFSA <T, S> dfsa)
        {
            ICollection <DFSAState <T, S> > states = dfsa.States();
            long time = Runtime.CurrentTimeMillis();

            if (debug)
            {
                time = Runtime.CurrentTimeMillis();
                log.Info("\nStarting on " + dfsa.dfsaID);
                log.Info(" -- " + states.Count + " states.");
            }
            int numStates = states.Count;
            // assign ids
            int id = 0;

            DFSAState <T, S>[] state = ErasureUtils.UncheckedCast <DFSAState <T, S>[]>(new DFSAState[numStates]);
            IDictionary <DFSAState <T, S>, int> stateToID = Generics.NewHashMap();

            foreach (DFSAState <T, S> state1 in states)
            {
                state[id]         = state1;
                stateToID[state1] = int.Parse(id);
                id++;
            }
            // initialize grid
            bool[][] distinct = new bool[numStates][];
            IList <DFSAMinimizer.IntPair>[][] dependentList = ErasureUtils.UncheckedCast <IList <DFSAMinimizer.IntPair>[][]>(new IList[numStates][]);
            for (int i = 0; i < numStates; i++)
            {
                for (int j = i + 1; j < numStates; j++)
                {
                    distinct[i][j] = state[i].IsAccepting() != state[j].IsAccepting();
                }
            }
            if (debug)
            {
                log.Info("Initialized: " + (Runtime.CurrentTimeMillis() - time));
                time = Runtime.CurrentTimeMillis();
            }
            // visit all non-distinct
            for (int i_1 = 0; i_1 < numStates; i_1++)
            {
                for (int j = i_1 + 1; j < numStates; j++)
                {
                    if (!distinct[i_1][j])
                    {
                        DFSAState <T, S>      state1_1 = state[i_1];
                        DFSAState <T, S>      state2   = state[j];
                        DFSAMinimizer.IntPair ip       = new DFSAMinimizer.IntPair(i_1, j);
                        // check if some input distinguishes this pair
                        ICollection <T> inputs = Generics.NewHashSet();
                        Sharpen.Collections.AddAll(inputs, state1_1.ContinuingInputs());
                        Sharpen.Collections.AddAll(inputs, state2.ContinuingInputs());
                        bool distinguishable = false;
                        ICollection <DFSAMinimizer.IntPair> pendingIPairs = Generics.NewHashSet();
                        IEnumerator <T> inputI = inputs.GetEnumerator();
                        while (inputI.MoveNext() && !distinguishable)
                        {
                            T input = inputI.Current;
                            DFSATransition <T, S> transition1 = state1_1.Transition(input);
                            DFSATransition <T, S> transition2 = state2.Transition(input);
                            if ((transition1 == null) != (transition2 == null))
                            {
                                distinguishable = true;
                            }
                            if (transition1 != null && transition2 != null)
                            {
                                DFSAState <T, S> target1 = transition1.GetTarget();
                                DFSAState <T, S> target2 = transition2.GetTarget();
                                int num1 = stateToID[target1];
                                int num2 = stateToID[target2];
                                DFSAMinimizer.IntPair targetIPair = new DFSAMinimizer.IntPair(num1, num2);
                                if (num1 != num2)
                                {
                                    if (distinct[num1][num2])
                                    {
                                        distinguishable = true;
                                    }
                                    else
                                    {
                                        pendingIPairs.Add(targetIPair);
                                    }
                                }
                            }
                        }
                        if (distinguishable)
                        {
                            // if the pair is distinguishable, record that
                            IList <DFSAMinimizer.IntPair> markStack = new List <DFSAMinimizer.IntPair>();
                            markStack.Add(ip);
                            while (!markStack.IsEmpty())
                            {
                                DFSAMinimizer.IntPair ipToMark = markStack[markStack.Count - 1];
                                markStack.Remove(markStack.Count - 1);
                                distinct[ipToMark.i][ipToMark.j] = true;
                                IList <DFSAMinimizer.IntPair> addList = dependentList[ipToMark.i][ipToMark.j];
                                if (addList != null)
                                {
                                    Sharpen.Collections.AddAll(markStack, addList);
                                }
                            }
                        }
                        else
                        {
                            // otherwise add it to any pending pairs
                            foreach (DFSAMinimizer.IntPair pendingIPair in pendingIPairs)
                            {
                                IList <DFSAMinimizer.IntPair> dependentList1 = dependentList[pendingIPair.i][pendingIPair.j];
                                if (dependentList1 == null)
                                {
                                    dependentList1 = new List <DFSAMinimizer.IntPair>();
                                    dependentList[pendingIPair.i][pendingIPair.j] = dependentList1;
                                }
                                dependentList1.Add(ip);
                            }
                        }
                    }
                }
            }
            if (debug)
            {
                log.Info("All pairs marked: " + (Runtime.CurrentTimeMillis() - time));
                time = Runtime.CurrentTimeMillis();
            }
            // decide what canonical state each state will map to...
            IDisjointSet <DFSAState <T, S> > stateClasses = new FastDisjointSet <DFSAState <T, S> >(states);

            for (int i_2 = 0; i_2 < numStates; i_2++)
            {
                for (int j = i_2 + 1; j < numStates; j++)
                {
                    if (!distinct[i_2][j])
                    {
                        DFSAState <T, S> state1_1 = state[i_2];
                        DFSAState <T, S> state2   = state[j];
                        stateClasses.Union(state1_1, state2);
                    }
                }
            }
            IDictionary <DFSAState <T, S>, DFSAState <T, S> > stateToRep = Generics.NewHashMap();

            foreach (DFSAState <T, S> state1_2 in states)
            {
                DFSAState <T, S> rep = stateClasses.Find(state1_2);
                stateToRep[state1_2] = rep;
            }
            if (debug)
            {
                log.Info("Canonical states chosen: " + (Runtime.CurrentTimeMillis() - time));
                time = Runtime.CurrentTimeMillis();
            }
            // reduce the DFSA by replacing transition targets with their reps
            foreach (DFSAState <T, S> state1_3 in states)
            {
                if (!state1_3.Equals(stateToRep[state1_3]))
                {
                    continue;
                }
                foreach (DFSATransition <T, S> transition in state1_3.Transitions())
                {
                    //if (!transition.target.equals(stateToRep.get(transition.target)))
                    //  System.out.println(Utils.pad(transition.target.toString(),30)+stateToRep.get(transition.target));
                    transition.target = stateToRep[transition.target];
                }
            }
            dfsa.initialState = stateToRep[dfsa.initialState];
            if (debug)
            {
                log.Info("Done: " + (Runtime.CurrentTimeMillis() - time));
            }
        }
コード例 #12
0
        // done!
        internal static void UnweightedMinimizeOld <T, S>(DFSA <T, S> dfsa)
        {
            ICollection <DFSAState <T, S> > states = dfsa.States();
            IDictionary <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> >, IList <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > > > stateUPairToDependentUPairList = Generics.NewHashMap(states.Count * states.Count / 2 + 1);
            IDictionary <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> >, bool> stateUPairToDistinguished = Generics.NewHashMap(states.Count * states.Count / 2 + 1);

            int[] c          = new int[states.Count * states.Count / 2 + 1];
            int   streak     = 0;
            int   collisions = 0;
            int   entries    = 0;
            long  time       = Runtime.CurrentTimeMillis();

            if (debug)
            {
                time = Runtime.CurrentTimeMillis();
                log.Info("Starting on " + dfsa.dfsaID);
                log.Info(" -- " + states.Count + " states.");
            }
            // initialize grid
            int numDone = 0;

            foreach (DFSAState <T, S> state1 in states)
            {
                foreach (DFSAState <T, S> state2 in states)
                {
                    UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > up = new UnorderedPair <DFSAState <T, S>, DFSAState <T, S> >(state1, state2);
                    if (state1.Equals(state2))
                    {
                        continue;
                    }
                    if (stateUPairToDistinguished.Contains(up))
                    {
                        continue;
                    }
                    int bucket = (up.GetHashCode() & unchecked ((int)(0x7FFFFFFF))) % (states.Count * states.Count / 2 + 1);
                    c[bucket]++;
                    entries++;
                    if (c[bucket] > 1)
                    {
                        collisions++;
                        streak = 0;
                    }
                    else
                    {
                        streak++;
                    }
                    if (state1.IsAccepting() != state2.IsAccepting())
                    {
                        //log.info(Utils.pad((String)state1.stateID, 20)+" "+state2.stateID);
                        stateUPairToDistinguished[up] = true;
                    }
                    else
                    {
                        stateUPairToDistinguished[up] = false;
                    }
                }
                //stateUPairToDependentUPairList.put(up, new ArrayList());
                numDone++;
                if (numDone % 20 == 0)
                {
                    log.Info("\r" + numDone + "  " + ((double)collisions / (double)entries));
                }
            }
            if (debug)
            {
                log.Info("\nInitialized: " + (Runtime.CurrentTimeMillis() - time));
                time = Runtime.CurrentTimeMillis();
            }
            // visit each undistinguished pair
            foreach (UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > up_1 in stateUPairToDistinguished.Keys)
            {
                DFSAState <T, S> state1_1 = up_1.first;
                DFSAState <T, S> state2   = up_1.second;
                if (stateUPairToDistinguished[up_1].Equals(true))
                {
                    continue;
                }
                // check if some input distinguishes this pair
                ICollection <T> inputs = Generics.NewHashSet(state1_1.ContinuingInputs());
                Sharpen.Collections.AddAll(inputs, state2.ContinuingInputs());
                bool distinguishable = false;
                ICollection <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > > pendingUPairs = Generics.NewHashSet();
                IEnumerator <T> inputI = inputs.GetEnumerator();
                while (inputI.MoveNext() && !distinguishable)
                {
                    T input = inputI.Current;
                    DFSATransition <T, S> transition1 = state1_1.Transition(input);
                    DFSATransition <T, S> transition2 = state2.Transition(input);
                    if ((transition1 == null) != (transition2 == null))
                    {
                        distinguishable = true;
                    }
                    if (transition1 != null && transition2 != null)
                    {
                        DFSAState <T, S> target1 = transition1.GetTarget();
                        DFSAState <T, S> target2 = transition2.GetTarget();
                        UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > targetUPair = new UnorderedPair <DFSAState <T, S>, DFSAState <T, S> >(target1, target2);
                        if (!target1.Equals(target2))
                        {
                            if (stateUPairToDistinguished[targetUPair].Equals(true))
                            {
                                distinguishable = true;
                            }
                            else
                            {
                                pendingUPairs.Add(targetUPair);
                            }
                        }
                    }
                }
                // if the pair is distinguishable, record that
                if (distinguishable)
                {
                    IList <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > > markStack = new List <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > >();
                    markStack.Add(up_1);
                    while (!markStack.IsEmpty())
                    {
                        UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > upToMark = markStack[markStack.Count - 1];
                        markStack.Remove(markStack.Count - 1);
                        stateUPairToDistinguished[upToMark] = true;
                        IList <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > > addList = stateUPairToDependentUPairList[upToMark];
                        if (addList != null)
                        {
                            Sharpen.Collections.AddAll(markStack, addList);
                            stateUPairToDependentUPairList[upToMark].Clear();
                        }
                    }
                }
                else
                {
                    // otherwise add it to any pending pairs
                    foreach (UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > pendingUPair in pendingUPairs)
                    {
                        IList <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > > dependentList = stateUPairToDependentUPairList[pendingUPair];
                        if (dependentList == null)
                        {
                            dependentList = new List <UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > >();
                            stateUPairToDependentUPairList[pendingUPair] = dependentList;
                        }
                        dependentList.Add(up_1);
                    }
                }
            }
            if (debug)
            {
                log.Info("All pairs marked: " + (Runtime.CurrentTimeMillis() - time));
                time = Runtime.CurrentTimeMillis();
            }
            // decide what canonical state each state will map to...
            IDisjointSet <DFSAState <T, S> > stateClasses = new FastDisjointSet <DFSAState <T, S> >(states);

            foreach (UnorderedPair <DFSAState <T, S>, DFSAState <T, S> > up_2 in stateUPairToDistinguished.Keys)
            {
                if (stateUPairToDistinguished[up_2].Equals(false))
                {
                    DFSAState <T, S> state1_1 = up_2.first;
                    DFSAState <T, S> state2   = up_2.second;
                    stateClasses.Union(state1_1, state2);
                }
            }
            IDictionary <DFSAState <T, S>, DFSAState <T, S> > stateToRep = Generics.NewHashMap();

            foreach (DFSAState <T, S> state in states)
            {
                DFSAState <T, S> rep = stateClasses.Find(state);
                stateToRep[state] = rep;
            }
            if (debug)
            {
                log.Info("Canonical states chosen: " + (Runtime.CurrentTimeMillis() - time));
                time = Runtime.CurrentTimeMillis();
            }
            // reduce the DFSA by replacing transition targets with their reps
            foreach (DFSAState <T, S> state_1 in states)
            {
                if (!state_1.Equals(stateToRep[state_1]))
                {
                    continue;
                }
                foreach (DFSATransition <T, S> transition in state_1.Transitions())
                {
                    transition.target = stateClasses.Find(transition.target);
                }
            }
            dfsa.initialState = stateClasses.Find(dfsa.initialState);
            if (debug)
            {
                log.Info("Done: " + (Runtime.CurrentTimeMillis() - time));
            }
        }
コード例 #13
0
 public void ThrowsIfItemDoesNotExist(int input) {
     const int n = 10;
     var fastDisjointSet = new FastDisjointSet(n);
     Assert.Throws<ArgumentException>(() => fastDisjointSet.Find(input));
 }
コード例 #14
0
 public void CorrectlyCreatesObject(int input) {
     var fastDisjointSet = new FastDisjointSet(input);
     Assert.Equal(input, fastDisjointSet.N);
 }
コード例 #15
0
            public void ReturnsCorrectValueForItemsNotInSameSet() {
                const int n = 5;
                var fastDisjointSet = new FastDisjointSet(n);

                Assert.False(fastDisjointSet.AreInSameSet(0, 1));
            }