Exemplo n.º 1
0
        public static T BreadthFirstSearch <T>(this Graph <T> graph, Func <T, bool> matchFunc)
            where T : class
        {
            if (graph.IsEmpty)
            {
                return(null);
            }

            graph.Nodes.ForEach(x => x._isVisited = false);

            IQueue <GraphNode <T> > queue = new QueueLinkedList <GraphNode <T> >();

            queue.Enqueue(graph.Nodes[0]);

            while (!queue.IsEmpty)
            {
                var node = queue.Dequeue();
                if (node._isVisited)
                {
                    continue;
                }

                if (matchFunc(node.Data))
                {
                    return(node.Data);
                }

                node.Edges.ForEach(x => queue.Enqueue(x.To));
                node._isVisited = true;
            }

            return(null);
        }
Exemplo n.º 2
0
        private bool HasAugmentedPath(FlowNetwork G)
        {
            var V = G.V();

            edgeTo = new FlowEdge[V];
            marked = new bool[V];
            var queue = new QueueLinkedList <int>();

            queue.Enqueue(s);

            while (!queue.IsEmpty)
            {
                var x = queue.Dequeue();
                marked[x] = true;
                foreach (var e in G.adj(x))
                {
                    var w = e.other(x);

                    if (!marked[w] && e.residualCapacityTo(w) > 0)
                    {
                        queue.Enqueue(w);
                        edgeTo[w] = e;

                        if (w == t)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        public void Implement_Queue_using_LinkedList(int[] arr)
        {
            var QueueLL = new QueueLinkedList <int>();

            foreach (var item in arr)
            {
                QueueLL.EnQueue(item);
            }

            foreach (var item in arr)
            {
                var result = QueueLL.DeQueue();
                Assert.Equal(item, result);
            }

            QueueLL.EnQueue(10);
            QueueLL.EnQueue(20);
            QueueLL.DeQueue();
            QueueLL.DeQueue();
            QueueLL.EnQueue(30);
            QueueLL.EnQueue(40);
            QueueLL.EnQueue(50);
            QueueLL.DeQueue();
            output.WriteLine("Queue Front : " + QueueLL.Front.Data);
            output.WriteLine("Queue Rear : " + QueueLL.Rear.Data);
            Assert.Equal(40, QueueLL.Front.Data);
            Assert.Equal(50, QueueLL.Rear.Data);
        }
Exemplo n.º 4
0
        public BreadthFirstSearch(Graph G, int s)
        {
            var V = G.V();

            this.s = s;

            var queue = new QueueLinkedList <int>();

            queue.Enqueue(s);

            marked = new bool[V];
            edgeTo = new int[V];

            marked[s] = true;

            while (!queue.IsEmpty)
            {
                var v = queue.Dequeue();

                foreach (var w in G.adj(v))
                {
                    if (marked[w])
                    {
                        continue;
                    }
                    edgeTo[w] = v;
                    marked[w] = true;
                    queue.Enqueue(w);
                }
            }
        }
Exemplo n.º 5
0
        public void LevelOrderTraversal()
        {
            if (_rootNode == null)
            {
                Console.WriteLine("Tree is empty!");
                return;
            }

            var queue = new QueueLinkedList <NodeTree <TData> >();

            queue.Enqueue(_rootNode);

            while (queue.Size() != 0)
            {
                var dequeuedElement = queue.Dequeue();

                Console.WriteLine(dequeuedElement.Data);

                if (dequeuedElement.LeftChild != null)
                {
                    queue.Enqueue(dequeuedElement.LeftChild);
                }

                if (dequeuedElement.RightChild != null)
                {
                    queue.Enqueue(dequeuedElement.RightChild);
                }
            }

            Console.WriteLine();
        }
        public void Get_GetFromEmptyQueueShouldThrowExeption()
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int>();

            // Act

            // Assert
            Assert.Throws <DataStructureIsEmptyOnReadExeption>(() => sut.Get());
        }
Exemplo n.º 7
0
        /// <summary>
        /// This Method is used to test the DecksOfCardsUsingQueue Class.
        /// </summary>
        public static void DeckOfCardsUsingQueue()
        {
            try
            {
                Console.WriteLine();
                Console.WriteLine("-----------------Deck of Cards Using Queue Program-----------------");
                Console.WriteLine();

                string[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
                string[] rank  = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };

                QueueLinkedList player1 = new QueueLinkedList();
                QueueLinkedList player2 = new QueueLinkedList();
                QueueLinkedList player3 = new QueueLinkedList();
                QueueLinkedList player4 = new QueueLinkedList();

                player1 = Utility.DeckOfCard(suits, rank);
                player2 = Utility.DeckOfCard(suits, rank);
                player3 = Utility.DeckOfCard(suits, rank);
                player4 = Utility.DeckOfCard(suits, rank);


                while (player1.Size() != 0)
                {
                    player1.Dequeue();
                }

                Console.WriteLine();

                while (player2.Size() != 0)
                {
                    player2.Dequeue();
                }

                Console.WriteLine();

                while (player3.Size() != 0)
                {
                    player3.Dequeue();
                }

                Console.WriteLine();

                while (player4.Size() != 0)
                {
                    player4.Dequeue();
                }

                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Message: {0}", e.Message);
            }
        }
Exemplo n.º 8
0
        public IEnumerable <string> KeysWithPrefix(string prefix)
        {
            Node x = Get(root, prefix, 0);
            QueueLinkedList <String> queue = new QueueLinkedList <string>();

            if (x != null)
            {
                Collect(x, prefix, queue);
            }
            return(queue);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures.
        /// Finds all vertices that can be reached by the starting vertex.
        /// </summary>
        /// <param name="rootVertex"></param>
        /// <returns></returns>
        public Dictionary <T, BfsVertexInfo <T> > BreadthFirstSearch(T rootVertex, Action <T> preVisit = null)
        {
            // Traversed graph information
            var visitedVerticesInfo = new Dictionary <T, BfsVertexInfo <T> >();

            if (!_aList.ContainsKey(rootVertex))
            {
                return(visitedVerticesInfo);
            }

            // Initialize it
            foreach (var i in _aList)
            {
                visitedVerticesInfo[i.Key] = new BfsVertexInfo <T>();
            }

            // Set the distance for the root vertex
            visitedVerticesInfo[rootVertex] = new BfsVertexInfo <T>()
            {
                Distance = 0
            };

            // Create a queue and add root vertex
            QueueLinkedList <T> queue = new QueueLinkedList <T>();

            queue.Enqueue(rootVertex);

            // As long as the queue is not empty:
            while (!queue.IsEmpty())
            {
                // Repeatedly dequeue a vertex u from the queue.
                var vertex = queue.Dequeue();
                Console.Write(vertex + " ");

                // Trace the path
                preVisit?.Invoke(vertex);

                // For each neighbor v of u that has not been visited:
                foreach (var neighbor in _aList[vertex])
                {
                    if (visitedVerticesInfo[neighbor].Distance == null)
                    {
                        // Set distance to 1 greater than u's distance
                        visitedVerticesInfo[neighbor].Distance = visitedVerticesInfo[vertex].Distance + 1;
                        // Set predecessor to u
                        visitedVerticesInfo[neighbor].Predecessor = vertex;
                        // Enqueue v
                        queue.Enqueue(neighbor);
                    }
                }
            }

            return(visitedVerticesInfo);
        }
        public void QueueSizeIncreases()
        {
            var queue = new QueueLinkedList <int>();
            var size  = 100;

            for (int i = 0; i < size; i++)
            {
                queue.Enqueue(i);
            }

            Assert.AreEqual(size, queue.Size());
        }
 public void TestQueue()
 {
     var queue = new QueueLinkedList<int>();
     queue.Enqueue(10);
     Assert.Equal(1, queue.Count);
     queue.Enqueue(20);
     Assert.Equal(2, queue.Count);
     Assert.False(queue.IsEmpty);
     Assert.Equal(10, queue.Dequeue());
     Assert.Equal(20, queue.Dequeue());
     Assert.True(queue.IsEmpty);
 }
        public void IsEmpty_EmptyQueueShouldReturnTrue()
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int>();
            bool expected             = true;

            // Act
            bool actual = sut.IsEmpty();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void Indexer_SetFromWrongIndexShouldThrowExeption()
        {
            // Arrange
            QueueLinkedList <double> sut = new QueueLinkedList <double>();

            sut.Add(1);
            sut.Add(2);

            // Act

            // Assert
            Assert.Throws <IndexOutOfRangeException>(() => sut[5] = 10);
        }
Exemplo n.º 14
0
        public void Test()
        {
            var queue = new QueueLinkedList <int>();

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);

            Assert.AreEqual("123", queue.GetValues());

            Assert.AreEqual(1, queue.Dequeue());
            Assert.AreEqual("23", queue.GetValues());
        }
Exemplo n.º 15
0
        public static void TraverseBreadthFirst <T>(this SearchTree <T> tree, Action <T> action)
            where T : class
        {
            IQueue <TreeNode <T> > queue = new QueueLinkedList <TreeNode <T> >(); // TODO test circular

            queue.Enqueue(tree.Root);
            while (!queue.IsEmpty)
            {
                var node = queue.Dequeue();
                action(node.Data);
                node.Children.ForEach(queue.Enqueue);
            }
        }
        public void EmptyQueueDequeueUnderflow()
        {
            var queue = new QueueLinkedList <int>();

            try
            {
                queue.Dequeue();
            }
            catch (InvalidOperationException ex)
            {
                Assert.AreEqual("The Queue is empty.", ex.Message);
            }
        }
        public void Filter_FilterShouldWork()
        {
            // Arrange
            QueueLinkedList <double> sut = new QueueLinkedList <double> {
                double.MaxValue, 2.7, 4.25, 5.28, 1.3, 2.8
            };

            double[] expected = { 5.28, 4.25, double.MaxValue };

            // Act
            IEnumerable <double> actual = sut.Filter(item => item > 4.1);

            // Assert
            Assert.Equal(expected, actual);
        }
        public void Clear_ClearQueueWithSomeNumbersShoulWork()
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int>();
            int expected = 0;

            // Act
            sut.Add(int.MaxValue);
            sut.Add(int.MinValue);
            sut.Add(42);
            sut.Clear();
            int actual = sut.Size();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void Add_AddManyNumbersToQueueShouldWork(int count)
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int>();
            int expected = count;

            // Act
            for (int i = 0; i < count; i++)
            {
                sut.Add(i);
            }
            int actual = sut.Size();

            // Assert
            Assert.Equal(expected, actual);
        }
        public void MultipleEnqueueDequeueReturnValues()
        {
            var queue = new QueueLinkedList <int>();
            var size  = 50;

            for (int i = 0; i < size; i++)
            {
                queue.Enqueue(i);
            }

            for (int i = 0; i < size; i++)
            {
                var dequeued = queue.Dequeue();
                Assert.AreEqual(i, dequeued);
            }
        }
        public void Indexer_IndexerGetAndSetFromEndShouldWork()
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int>();
            int expected = 42;

            // Act
            for (int i = 0; i < 10; i++)
            {
                sut.Add(i);
            }
            sut[9] = 42;
            int actual = sut[9];

            // Assert
            Assert.Equal(expected, actual);
        }
        public void StackSizeDecreases()
        {
            var queue = new QueueLinkedList <int>();
            var size  = 100;

            for (int i = 0; i < size; i++)
            {
                queue.Enqueue(i);
            }

            for (int i = 0; i < size; i++)
            {
                queue.Dequeue();
            }

            Assert.AreEqual(0, queue.Size());
        }
        public void Get_GetNumbersFromQueueShouldWork()
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int>();

            // Act
            for (int i = 0; i < 100; i++)
            {
                sut.Add(i);
            }

            // Assert
            for (int expected = 0; expected < 100; expected++)
            {
                Assert.Equal(expected, sut.Get());
            }
        }
        /// <summary>
        /// It Displat the Customer Transaction
        /// </summary>
        public static void DisplayTransaction()
        {
            try
            {
                List <CustomerPurchased> customerPurchaseds = ReadCustomerPurchasedLists();
                customerPurchaseds = customerPurchaseds.FindAll(x => x.UserName.Equals(UserName));

                QueueLinkedList queueLinkedList = new QueueLinkedList();

                bool inputFlag;
                int  choice;

                DisplayPurchasedShares(customerPurchaseds);

                do
                {
                    Console.WriteLine();
                    Console.Write("Which Share Transaction you want to view: ");
                    inputFlag = int.TryParse(Console.ReadLine(), out choice);
                    ErrorMessage(inputFlag);
                    if (!inputFlag)
                    {
                        DisplayPurchasedShares(customerPurchaseds);
                    }
                    if (choice <= 0 || choice > customerPurchaseds.Count)
                    {
                        Console.WriteLine("Invalid Choice !!!");
                        DisplayPurchasedShares(customerPurchaseds);
                        Console.WriteLine();
                        inputFlag = false;
                    }
                } while (!inputFlag);

                foreach (CustomerPurchased customerPurchased in customerPurchaseds)
                {
                    queueLinkedList.Enqueue(customerPurchased.DateAndTime);
                }

                Console.WriteLine("The Transaction Date is: {0}", queueLinkedList.Search(choice));
            }
            catch (Exception e)
            {
                Console.WriteLine("Message: {0}", e.Message);
            }
        }
Exemplo n.º 25
0
        public static T BreadthFirstSearch <T>(this SearchTree <T> tree, Func <T, bool> matchFunc)
            where T : class
        {
            IQueue <TreeNode <T> > queue = new QueueLinkedList <TreeNode <T> >();

            queue.Enqueue(tree.Root);

            while (!queue.IsEmpty)
            {
                var node = queue.Dequeue();
                if (matchFunc(node.Data))
                {
                    return(node.Data);
                }
                node.Children.ForEach(queue.Enqueue);
            }

            return(null);
        }
Exemplo n.º 26
0
    public static void Main(string[] args)
    {
        QueueLinkedList <int> queue = new QueueLinkedList <int>();

        Console.WriteLine($"Size(): {queue.Size()}");
        Console.WriteLine($"IsEmpty(): {queue.IsEmpty()}");
        Console.WriteLine();

        queue.Enqueue(10);
        queue.Enqueue(20);
        queue.Enqueue(30);
        queue.Enqueue(40);

        Console.WriteLine($"Size(): {queue.Size()}");
        Console.WriteLine($"IsEmpty(): {queue.IsEmpty()}");
        Console.WriteLine();

        Console.WriteLine($"Dequeue(): {queue.Dequeue().ToString()}");
        Console.WriteLine($"Dequeue(): {queue.Dequeue().ToString()}");
        Console.WriteLine();

        Console.WriteLine($"Size(): {queue.Size()}");

        // Expected Output:
        // ------------------
        // Size(): 0
        // IsEmpty(): True
        //
        // Enqueue(): 10
        // Enqueue(): 20
        // Enqueue(): 30
        // Enqueue(): 40
        //
        // Size(): 4
        // IsEmpty(): False
        //
        // Dequeue(): 10
        // Dequeue(): 20
        //
        // Size(): 2
    }
        public void Enumerable_EnumShouldWork()
        {
            // Arrange
            QueueLinkedList <int> sut = new QueueLinkedList <int> {
                5, 4, 3, 2, 1
            };

            int[] arr = new int[5];

            // Act
            int i = 0;

            foreach (var item in sut)
            {
                arr[i] = item;
                i++;
            }

            // Assert

            Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, arr);
        }
        /// <summary>
        /// TODO: Add a description what is going on here
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static BfsVertexInfo <int?>[] DoBfs(int[][] graph, int source)
        {
            BfsVertexInfo <int?>[] bfsInfo = new BfsVertexInfo <int?> [graph.Length];

            // Initialize bfsInfo array
            for (int i = 0; i < graph.Length; i++)
            {
                bfsInfo[i] = new BfsVertexInfo <int?>
                {
                    Distance    = null,
                    Predecessor = null
                };
            }

            bfsInfo[source].Distance = 0;

            var queue = new QueueLinkedList <int>();

            queue.Enqueue(source);

            while (!queue.IsEmpty())
            {
                var u = queue.Dequeue();
                for (int i = 0; i < graph[u].Length; i++)
                {
                    var v = graph[u][i];
                    if (bfsInfo[v].Distance == null)
                    {
                        bfsInfo[v].Distance    = bfsInfo[u].Distance + 1;
                        bfsInfo[v].Predecessor = u;
                        queue.Enqueue(v);
                    }
                }
            }

            return(bfsInfo);
        }
Exemplo n.º 29
0
        static void QueueLinkedList()
        {
            QueueLinkedList queue = new QueueLinkedList();

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            queue.Enqueue(6);
            queue.Enqueue(7);

            foreach (int val in queue.GetEnumerator())
            {
                Console.WriteLine(val);
            }


            Console.WriteLine("Peek");
            Console.WriteLine(queue.Peek());
            Console.WriteLine(queue.Peek());

            Console.WriteLine("Pop");
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());

            Console.WriteLine("Remaining");
            foreach (int val in queue.GetEnumerator())
            {
                Console.WriteLine(val);
            }

            Console.Read();
        }
        public void QueueUnderflow()
        {
            var queue = new QueueLinkedList <int>();
            var size  = 1000;

            for (int i = 0; i < size; i++)
            {
                queue.Enqueue(i);
            }

            for (int i = 0; i < size; i++)
            {
                queue.Dequeue();
            }

            try
            {
                queue.Dequeue();
            }
            catch (InvalidOperationException ex)
            {
                Assert.AreEqual("The Queue is empty.", ex.Message);
            }
        }