Exemplo n.º 1
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();
        }
Exemplo n.º 2
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.º 3
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.º 4
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.º 5
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 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);
 }
Exemplo n.º 7
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.º 8
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 QueueSizeIncreases()
        {
            var queue = new QueueLinkedList <int>();
            var size  = 100;

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

            Assert.AreEqual(size, queue.Size());
        }
Exemplo n.º 10
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);
            }
        }
        /// <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);
        }
        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 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());
        }
        /// <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.º 15
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);
        }
        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);
            }
        }
Exemplo n.º 17
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();
        }
Exemplo n.º 18
0
        public static QueueLinkedList DeckOfCard(string[] suits, string[] rank)
        {
            try
            {
                QueueLinkedList queueLinkedList = new QueueLinkedList();

                Random random = new Random();

                for (int j = 0; j < 9; j++)
                {
                    bool flag;
                    do
                    {
                        bool inputFlag = false;
                        flag = false;
                        int  suitsRandom = random.Next(0, 4);
                        char initial     = suits[suitsRandom].ToCharArray()[0];

                        int    rankRandom = random.Next(0, 13);
                        string cardsForm  = rank[rankRandom] + initial;

                        if (end == 0)
                        {
                            checkCards[end] = cardsForm;
                            end++;
                            flag = true;
                        }
                        else
                        {
                            for (int k = 0; k < end; k++)
                            {
                                if (checkCards[k] == cardsForm)
                                {
                                    flag      = false;
                                    inputFlag = true;
                                    break;
                                }
                            }
                            if (!inputFlag)
                            {
                                checkCards[end] = cardsForm;
                                end++;
                                flag = true;
                            }
                        }
                    } while (!flag);
                }
                SortDeckOfCards();

                for (int i = init; i < end; i++)
                {
                    queueLinkedList.Enqueue(checkCards[i]);
                }

                init = end;

                return(queueLinkedList);
            }
            catch (Exception e)
            {
                Console.WriteLine("Message: {0}", e.Message);
                return(null);
            }
        }
Exemplo n.º 19
0
 public void Enqueue()
 {
     _queue.Enqueue(3);
     _queue.Enqueue(4);
     Assert.IsTrue(_queue.Peek().Value == 3);
 }