public void PriorityQueue_ExtractMax_InsertHighestBubblesToRoot() { #region Arrange Participant participantHighest = new Participant() { Name = "Highest", ParticipantId = 1 }; Participant participantMiddle = new Participant() { Name = "Middle", ParticipantId = 2 }; Participant participantLower = new Participant() { Name = "Lower", ParticipantId = 3 }; Participant participantLowest = new Participant() { Name = "Lowest", ParticipantId = 4 }; int highestRoll = 100; int middleRoll = 50; int lowerRoll = 10; int lowestRoll = 1; PriorityQueue<Participant, int> WinnersMiddleLowestHighest = new PriorityQueue<Participant, int>(new ExtractMax()); WinnersMiddleLowestHighest.Insert(participantMiddle, middleRoll); WinnersMiddleLowestHighest.Insert(participantLower, lowerRoll); WinnersMiddleLowestHighest.Insert(participantHighest, highestRoll); WinnersMiddleLowestHighest.Insert(participantLowest, lowestRoll); #endregion #region Act var highestWinner = WinnersMiddleLowestHighest.ExtractKey(); #endregion #region Assert Assert.AreEqual(participantHighest, highestWinner, "Highest Participant Not Extracted Correctly."); #endregion }
static void Main(string[] args) { var pq = new PriorityQueue<int, int>(); pq.Insert(new KeyValuePair<int, int>(0, 0)); pq.Insert(new KeyValuePair<int, int>(1, 1)); pq.Insert(new KeyValuePair<int, int>(4, 5)); pq.Insert(new KeyValuePair<int, int>(1, 1)); pq.Insert(new KeyValuePair<int, int>(3, 3)); pq.Insert(new KeyValuePair<int, int>(2, 2)); pq.Insert(new KeyValuePair<int, int>(5, 5)); pq.Insert(new KeyValuePair<int, int>(4, 4)); KeyValuePair<int, int> pair; for (var i = 0; i < 10; i++) { if (pq.TryExtractMinimum(out pair)) { Console.WriteLine("key: {0}, value: {1}", pair.Key, pair.Value); } else { Console.WriteLine("Not extracted"); } } Console.ReadLine(); }
static void Main(string[] args) { // Print the top M lines in the input stream. Console.Write("Write Lenth : "); int M = int.Parse(Console.ReadLine()); Console.Write("Write Del max(True) or min(False) : "); bool b = false; bool.TryParse(Console.ReadLine(),out b); PriorityQueue<int> pq = new PriorityQueue<int>(M,b); Console.WriteLine("Insert Queue : "); for(int i=0;i<M;i++) pq.Insert(int.Parse(Console.ReadLine())); Stack<int> stack = new Stack<int>(); while (!pq.isEmpty()) stack.push(pq.Del()); Console.WriteLine("\n\t OutPut Stack"); while(!stack.isEmpty()) Console.WriteLine(stack.pull()); Console.ReadKey(); }
public void PriorityQueueInsertTest() { var values = new[] { 3, 7, 1, 4, 5, 2, 9, 6 }; var maxpq = new PriorityQueue<int>(ExtremeType.Maximum); Assert.IsTrue(maxpq.IsEmpty); for (int i = 0; i < values.Length; i++) { maxpq.Insert(values[i]); } Assert.AreEqual(9, maxpq.DeleteExtreme()); maxpq.Insert(0); maxpq.Insert(8); int last = Int32.MaxValue; while (!maxpq.IsEmpty) { int curr = maxpq.DeleteExtreme(); Assert.Less(curr, last); last = curr; } }
public void PriorityQueueIncreaseKeyTest() { var minpq = new PriorityQueue<float>(new[] { 0.3f, 0.7f, 0.1f, 0.4f, 0.5f, 0.2f, 0.9f, 0.6f }, ExtremeType.Minimum); Assert.AreEqual(0.1f, minpq.PeekExtreme()); minpq.ModifyValue(0.4f, 0.55f); Assert.AreEqual(0.1f, minpq.PeekExtreme()); minpq.ModifyValue(0.1f, 0.22f); Assert.AreEqual(0.2f, minpq.PeekExtreme()); minpq.ModifyValue(0.5f, 500f); minpq.Insert(100f); minpq.Insert(501f); float last = 0.0f; while (!minpq.IsEmpty) { float curr = minpq.DeleteExtreme(); Assert.AreNotEqual(0.4f, curr); Assert.Greater(curr, last); last = curr; } }
private void CGI_Cut() { // -- mark verteices on strokes -- int n = mesh.VertexCount, label = 0; bool[] labeled = new bool[n]; bool[] tag = new bool[n]; foreach (int v in this.sourceVertices) { vnode[v].label = label; labeled[v] = true; tag[v] = true; } label = 1; foreach (int v in this.sinkVertices) { vnode[v].label = label; labeled[v] = true; tag[v] = true; } // -- push to priority queue -- PriorityQueue iQue = new PriorityQueue(); foreach (int v in this.sourceVertices) { foreach (int adj in mesh.AdjVV[v]) { GraphNode node = vnode[adj]; node.label = vnode[v].label; node.dis = Vrt_wij(v, adj); tag[adj] = true; iQue.Insert(node); } } foreach (int v in this.sinkVertices) { foreach (int adj in mesh.AdjVV[v]) { GraphNode node = vnode[adj]; node.label = vnode[v].label; node.dis = Vrt_wij(v, adj); tag[adj] = true; iQue.Insert(node); } } // -- region growing -- while (!iQue.IsEmpty()) { GraphNode node = iQue.DeleteMin() as GraphNode; int v = node.index; labeled[v] = true; foreach (int adj in mesh.AdjVV[v]) { if (labeled[adj]) // -- already labeled -- { continue; } else { double cost = Vrt_wij(v, adj); GraphNode adjNode = vnode[adj]; adjNode.label = node.label; if (tag[adj]) // -- already in the queue -- { if (adjNode.dis > cost) { adjNode.dis = cost; iQue.Update(adjNode); } } else // -- a fresh vertex -- { adjNode.dis = cost; tag[adj] = true; iQue.Insert(adjNode); } } } } // -- convert to facets -- List<int> risidual = new List<int>(); for (int i = 0, j = 0; i < mesh.FaceCount; ++i, j+=3) { int c0 = mesh.FaceIndex[j]; int c1 = mesh.FaceIndex[j+1]; int c2 = mesh.FaceIndex[j+2]; if (vnode[c0].label == vnode[c1].label && vnode[c0].label == vnode[c2].label) { fnode[i].label = vnode[c0].label; } else { fnode[i].label = -1; risidual.Add(i); } } // -- deal with boundary faces -- while (risidual.Count > 0) { List<int> vlist = new List<int>(); vlist.AddRange(risidual); risidual.Clear(); foreach (int f in vlist) { double min = double.MaxValue; int minid = -1; foreach (int adj in mesh.AdjFF[f]) { if (fnode[adj].label < 0) continue; double c = Face_wij1(f, adj); if (c < min) { min = c; minid = adj; } } if (minid != -1) fnode[f].label = fnode[minid].label; else risidual.Add(f); } } // -- patch the results -- int index = this.patches.Count + 1; Patch p = new Patch(index); if (patchid == null) patchid = new byte[mesh.FaceCount]; for (int i = 0; i < mesh.FaceCount; ++i) { if (fnode[i].label == 0) { patchid[i] = (byte)index; p.faces.Add(i); } } this.all_patches.Add(p); }
private void patch_type_decompose() { int n = mesh.FaceCount; for (int i = 0; i < n; ++i) // -- reset the labels -- fnode[i].label = -1; // -- label seed faces -- bool[] visited = new bool[n]; bool[] labeled = new bool[n]; if (patchid == null) patchid = new byte[n]; List<int> sources = new List<int>(); List<int> targets = new List<int>(); foreach (List<int> flist in this.facesOnStrokes) { if (flist.Count < 1) continue; sources.Add(flist[0]); targets.Add(flist[flist.Count - 1]); } byte pid = patchid[sources[0]]; // -- the patch strokes lies on -- foreach (int f in sources) { fnode[f].label = 0; visited[f] = true; labeled[f] = true; } int label = 1; foreach (int f in targets) { fnode[f].label = label; visited[f] = true; labeled[f] = true; } // -- region growing -- PriorityQueue iQue = new PriorityQueue(); foreach (int f in sources) { foreach (int adj in mesh.AdjFF[f]) { if (patchid[adj] != pid) continue; fnode[adj].label = fnode[f].label; fnode[adj].dis = Face_wij(f, adj); iQue.Insert(fnode[adj]); visited[adj] = true; } } foreach (int f in targets) { foreach (int adj in mesh.AdjFF[f]) { if (patchid[adj] != pid) continue; fnode[adj].label = fnode[f].label; fnode[adj].dis = Face_wij(f, adj); iQue.Insert(fnode[adj]); visited[adj] = true; } } while (!iQue.IsEmpty()) { GraphNode node = iQue.DeleteMin() as GraphNode; int f = node.index; labeled[f] = true; foreach (int adj in mesh.AdjFF[f]) { if (patchid[adj] != pid) continue; if (labeled[adj]) { continue; } else if (visited[adj]) { double wij = Face_wij(f, adj); if (fnode[adj].dis > wij) { fnode[adj].label = fnode[f].label; fnode[adj].dis = wij; iQue.Update(fnode[adj]); } } else { fnode[adj].label = fnode[f].label; fnode[adj].dis = Face_wij(f, adj); iQue.Insert(fnode[adj]); visited[adj] = true; } } } // -- patch the results -- int index = this.all_patches.Count+1; Patch p = new Patch(index); p.prev_label = (int)pid; for (int i = 0; i < n; ++i) { if (fnode[i].label == 0) { patchid[i] = (byte)index; p.faces.Add(i); } } this.patches.Add(p); this.all_patches.Add(p); // -- global patches -- }
public ArrayList BranchAndBound(City[] Cities, ArrayList Route, double BSSF) { Node startNode = new Node(Cities); startNode.initStaticMembers(); PriorityQueue pQ = new PriorityQueue(startNode); ArrayList bbRoute = null; Stopwatch timer = new Stopwatch(); timer.Start(); //while ((pQ.Size() > 0) && (pQ.Peek() < BSSF) && (timer.Elapsed.TotalMinutes < 10)) while ((pQ.Size() > 0) && (pQ.Peek() < BSSF) && (timer.Elapsed.TotalSeconds < 30)) { // Keep track of the largest size of the queue if (pQ.Size() > Node.maxNodesCreated) { Node.maxNodesCreated = pQ.Size(); } startNode = pQ.DeleteMinimum(); //startNode.matrix2Table(); // Check for a solution if (startNode.includedEdges == Cities.Length) { ArrayList tempRoute = new ArrayList(); if (!startNode.exited.Contains(-1)) { int index = 0; while (tempRoute.Count < Cities.Length) { tempRoute.Add(Cities[startNode.exited[index]]); index = startNode.exited[index]; } BSSF = startNode.lowerBound; bbRoute = tempRoute; Node.numSolutions++; } } Node incNode = new Node(startNode); Node excNode = new Node(startNode); Node maxInclude = null; Node maxExclude = null; double maxDiff = -1; double diff = 0; int maxRow = 0; int maxCol = 0; for (int row = 0; row < startNode.matrixSize; row++) { for (int col = 0; col < startNode.matrixSize; col++) { if (startNode.rcMatrix[row, col] == 0) { Node includeNode = new Node(incNode, true, row, col); Node excludeNode = new Node(excNode, false, row, col); diff = excludeNode.lowerBound - includeNode.lowerBound; Node.numStatesCreated += 2; if (diff > maxDiff) { maxDiff = diff; maxRow = row; maxCol = col; maxInclude = new Node(includeNode); maxExclude = new Node(excludeNode); } } } } if (maxInclude != null && maxInclude.lowerBound < BSSF) { pQ.Insert(maxInclude); } else { Node.pruneCount++; } if (maxExclude != null && maxExclude.lowerBound < BSSF) { pQ.Insert(maxExclude); } else { Node.pruneCount++; } } timer.Stop(); Node.timeElapsed = timer.ElapsedMilliseconds; if (bbRoute == null) { return Route; } else { // Add the rest of the queue to the pruned count Node.pruneCount += pQ.Size(); return bbRoute; } }
public void PriorityQueue_ExtractMin_EachExtractReturnsLowestValue() { #region Arrange PriorityQueue<Participant, int> Winners = new PriorityQueue<Participant, int>(new ExtractMin()); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "1", ParticipantId = 1 }, 1000)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "2", ParticipantId = 2 }, 500)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "3", ParticipantId = 3 }, 600)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "4", ParticipantId = 4 }, 100)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "5", ParticipantId = 5 }, 150)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "6", ParticipantId = 6 }, 175)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "7", ParticipantId = 7 }, 10)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "8", ParticipantId = 8 }, 750)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "9", ParticipantId = 9 }, 300)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "10", ParticipantId = 10 }, 210)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "11", ParticipantId = 11 }, 1)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "12", ParticipantId = 12 }, 19)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "13", ParticipantId = 13 }, 1002)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "14", ParticipantId = 14 }, 1040)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "15", ParticipantId = 15 }, 900)); Winners.Insert(new KeyValuePair<Participant, int>(new Participant { Name = "16", ParticipantId = 16 }, 90)); #endregion #region Act bool AllAreLower = true; var lowerWinner = Winners.ExtractPair(); for (int i = 1; i < 16; ++i) { var higherWinner = Winners.ExtractPair(); if (higherWinner.Value < lowerWinner.Value && AllAreLower) AllAreLower = false; lowerWinner = higherWinner; } #endregion #region Assert Assert.IsTrue(AllAreLower, "Winners did not extract from lowest to highest value"); #endregion }