public void Dequeue() { _queue.Enqueue(3); _queue.Enqueue(4); Assert.IsTrue(_queue.Dequeue() == 3); Assert.IsTrue(_queue.Dequeue() == 4); var ex = Assert.Throws <InvalidOperationException>(() => _queue.Dequeue()); Assert.IsTrue(ex.Message == ExceptionMessage); }
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 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); }
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 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); } } }
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); }
/// <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); } }
/// <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 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 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 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()); }
public void EmptyQueueDequeueUnderflow() { var queue = new QueueLinkedList <int>(); try { queue.Dequeue(); } catch (InvalidOperationException ex) { Assert.AreEqual("The Queue is empty.", ex.Message); } }
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); } }
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()); }
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); }
/// <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); }