Beispiel #1
0
        public static Node <T> FindOverlapWithHash(EPI.DataStructures.LinkedList.LinkedList <T> aList, EPI.DataStructures.LinkedList.LinkedList <T> bList)
        {
            HashSet <Node <T> > aNodes = new HashSet <Node <T> >();
            Node <T>            a      = aList.Head;
            Node <T>            b      = bList.Head;

            if (a == null || b == null)
            {
                return(null);
            }

            while (a != null)
            {
                aNodes.Add(a);
                a = a.Next;
            }
            while (b != null)
            {
                if (aNodes.Contains(b))
                {
                    return(b);
                }
                b = b.Next;
            }
            return(null);
        }
Beispiel #2
0
        public void NoCycleLengthOne()
        {
            EPI.DataStructures.LinkedList.LinkedList <Object> list = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            Node <Object> a = new Node <object>();

            list.Head = a;
            a.Next    = null;

            Assert.Null(C07Q03 <Object> .FindCycleWithDict(list));
            Assert.Null(C07Q03 <Object> .FindCycle(list));
            Assert.Null(C07Q03 <Object> .FindCycleOptimized(list));
        }
Beispiel #3
0
        public void CycleStartsAtHeadLengthOne()
        {
            EPI.DataStructures.LinkedList.LinkedList <Object> list = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            Node <Object> a = new Node <object>();

            list.Head = a;
            a.Next    = a;

            Assert.Equal(a, C07Q03 <Object> .FindCycleWithDict(list));
            Assert.Equal(a, C07Q03 <Object> .FindCycle(list));
            Assert.Equal(a, C07Q03 <Object> .FindCycleOptimized(list));
        }
Beispiel #4
0
        public void HasCycle01()
        {
            EPI.DataStructures.LinkedList.LinkedList <Object> list = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            Node <Object> a = new Node <object>();
            Node <Object> b = new Node <object>();
            Node <Object> c = new Node <object>();

            list.Head = a;
            a.Next    = b;
            b.Next    = c;
            c.Next    = b;

            Assert.Equal(b, C07Q03 <Object> .FindCycleWithDict(list));
            Assert.Equal(b, C07Q03 <Object> .FindCycle(list));
            Assert.Equal(b, C07Q03 <Object> .FindCycleOptimized(list));
        }
Beispiel #5
0
        public static Node <T> FindCycle(EPI.DataStructures.LinkedList.LinkedList <T> list)
        {
            if (list.Head == null || list.Head.Next == null)
            {
                return(null);
            }

            Node <T> slow = list.Head;
            Node <T> fast = list.Head.Next;

            while (fast != null && fast != slow)
            {
                fast = fast.Next?.Next;
                slow = slow.Next;
            }
            if (fast == null)
            {
                return(null);
            }

            // fast is currently in a cycle
            int      cycleLength = 1;
            Node <T> seen        = fast;

            fast = fast.Next;
            while (seen != fast)
            {
                cycleLength++;
                fast = fast.Next;
            }

            Node <T> cycleStart = list.Head;

            while (cycleStart != fast)
            {
                for (int i = 0; i < cycleLength; i++)
                {
                    fast = fast.Next;
                    if (fast == cycleStart)
                    {
                        return(cycleStart);
                    }
                }
                cycleStart = cycleStart.Next;
            }
            return(cycleStart);
        }
Beispiel #6
0
        public static Node <T> FindCycleWithDict(EPI.DataStructures.LinkedList.LinkedList <T> list)
        {
            Node <T>            current = list.Head;
            HashSet <Node <T> > visited = new HashSet <Node <T> >();

            while (current != null)
            {
                if (visited.Contains(current))
                {
                    return(current);
                }

                visited.Add(current);
                current = current.Next;
            }

            return(null);
        }
Beispiel #7
0
        public void SameHead()
        {
            EPI.DataStructures.LinkedList.LinkedList <Object> listA = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            EPI.DataStructures.LinkedList.LinkedList <Object> listB = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            Node <Object> aa = new Node <object>();
            Node <Object> ab = new Node <object>();
            Node <Object> ac = new Node <object>();

            listA.Head = aa;
            listB.Head = aa;
            aa.Next    = ab;
            ab.Next    = ac;
            ac.Next    = null;

            Assert.Equal(aa, C07Q04 <Object> .FindOverlapWithHash(listA, listB));
            Assert.Equal(aa, C07Q04 <Object> .FindOverlapBruteForce(listA, listB));
            Assert.Equal(aa, C07Q04 <Object> .FindOverlap(listA, listB));
        }
Beispiel #8
0
        public static Node <T> FindOverlap(EPI.DataStructures.LinkedList.LinkedList <T> aList, EPI.DataStructures.LinkedList.LinkedList <T> bList)
        {
            Node <T> a = aList.Head;
            Node <T> b = bList.Head;
            Node <T> longer, shorter;
            int      aLen = 0;
            int      bLen = 0;

            while (a != null)
            {
                aLen++;
                a = a.Next;
            }
            while (b != null)
            {
                bLen++;
                b = b.Next;
            }

            if (aLen > bLen)
            {
                longer  = aList.Head;
                shorter = bList.Head;
            }
            else
            {
                longer  = bList.Head;
                shorter = aList.Head;
            }

            for (int i = 0; i < Math.Abs(aLen - bLen); i++)
            {
                longer = longer.Next;
            }

            while (longer != null && longer != shorter)
            {
                longer  = longer.Next;
                shorter = shorter.Next;
            }
            return(longer);
        }
Beispiel #9
0
        public static Node <T> FindOverlapBruteForce(EPI.DataStructures.LinkedList.LinkedList <T> aList, EPI.DataStructures.LinkedList.LinkedList <T> bList)
        {
            Node <T> a = aList.Head;
            Node <T> b;

            while (a != null)
            {
                b = bList.Head;
                while (b != null)
                {
                    if (a == b)
                    {
                        return(b);
                    }
                    b = b.Next;
                }
                a = a.Next;
            }

            return(null);
        }
Beispiel #10
0
        public void CycleAtTailLengthOne()
        {
            EPI.DataStructures.LinkedList.LinkedList <Object> list = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            Node <Object> a = new Node <object>();
            Node <Object> b = new Node <object>();
            Node <Object> c = new Node <object>();
            Node <Object> d = new Node <object>();
            Node <Object> e = new Node <object>();
            Node <Object> f = new Node <object>();

            list.Head = a;
            a.Next    = b;
            b.Next    = c;
            c.Next    = d;
            d.Next    = e;
            e.Next    = f;
            f.Next    = f;

            Assert.Equal(f, C07Q03 <Object> .FindCycleWithDict(list));
            Assert.Equal(f, C07Q03 <Object> .FindCycle(list));
            Assert.Equal(f, C07Q03 <Object> .FindCycleOptimized(list));
        }
Beispiel #11
0
        public void NoOverlap()
        {
            EPI.DataStructures.LinkedList.LinkedList <Object> listA = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            EPI.DataStructures.LinkedList.LinkedList <Object> listB = new EPI.DataStructures.LinkedList.LinkedList <Object>();
            Node <Object> aa = new Node <object>();
            Node <Object> ab = new Node <object>();
            Node <Object> ba = new Node <object>();
            Node <Object> bb = new Node <object>();
            Node <Object> bc = new Node <object>();
            Node <Object> bd = new Node <object>();

            listA.Head = aa;
            aa.Next    = ab;
            ab.Next    = null;
            listB.Head = ba;
            ba.Next    = bb;
            bb.Next    = bc;
            bc.Next    = bd;
            bd.Next    = null;

            Assert.Null(C07Q04 <Object> .FindOverlapWithHash(listA, listB));
            Assert.Null(C07Q04 <Object> .FindOverlapBruteForce(listA, listB));
            Assert.Null(C07Q04 <Object> .FindOverlap(listA, listB));
        }