本文整理汇总了C#中System.Collections.Generic.PriorityQueue类的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue类的具体用法?C# PriorityQueue怎么用?C# PriorityQueue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PriorityQueue类属于System.Collections.Generic命名空间,在下文中一共展示了PriorityQueue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DijkstraAlgorithm
public static void DijkstraAlgorithm(Dictionary<Node, List<Connection>> graph, Node source)
{
PriorityQueue<Node> queue = new PriorityQueue<Node>();
foreach (var item in graph)
{
item.Key.DijkstraDistance = long.MaxValue;
}
queue.Enqueue(source);
source.DijkstraDistance = 0;
while (queue.Count != 0)
{
var currentNode = queue.Dequeue();
foreach (var connection in graph[currentNode])
{
var dist = connection.Distance + currentNode.DijkstraDistance;
if (dist < connection.ToNode.DijkstraDistance)
{
connection.ToNode.DijkstraDistance = dist;
queue.Enqueue(connection.ToNode);
}
}
}
}
示例2: PathFinder
public PathFinder(Grid graph, Cell start, Cell goal)
{
// Priority Queue which contains cells that are candidates for examining, lowest priority to the node with the lowest f value
var frontier = new PriorityQueue<Cell>();
frontier.Enqueue(start, 0);
cameFrom[start] = start;
costSoFar[start] = 0;
while (frontier.Count > 0)
{
var current = frontier.Dequeue();
// Exit the search if goal have discovered
if (current.Equals(goal)) { break; }
// discovers the neighbours
foreach (var next in graph.Neighbors(current))
{
int newCost = costSoFar[current] + graph.Cost(current, next);
if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
{
costSoFar[next] = newCost;
// f = g + h
int priority = newCost + Heuristic(next, goal);
frontier.Enqueue(next, priority);
cameFrom[next] = current;
}
}
}
}
示例3: PriorityQueue
public void PriorityQueue()
{
PriorityQueue<int> pq = new PriorityQueue<int>(LinqExt.TakeMore);
pq.Enqueue(1, 1);
pq.Enqueue(9, 9);
pq.Enqueue(2, 2);
pq.Enqueue(5, 5);
Assert.IsTrue(pq.First() == 9, "Wrong Priority");
Assert.IsTrue(pq.Dequeue() == 9, "Wrong Priority");
Assert.IsTrue(pq.Dequeue() == 5, "Wrong Priority");
pq.ChangePriority(1, 29);
Assert.IsTrue(pq.First() == 1, "Wrong Priority, 1 should be first priority now with priority 29");
pq.Enqueue(3, 2); //add item with same priority
pq.Enqueue(4, 2); //add item with same priority
Assert.IsTrue(pq.Last() == 4, "Wrong Priority, 4 should be last");
pq.Enqueue(30, 30); // mix case
Assert.IsTrue(pq.First() == 30, "Wrong Priority, 30 should be first priority");
pq.Enqueue(31, 30); // mix case
Assert.IsTrue(pq.First() == 30, "Wrong Priority, 30 should still be first priority");
pq.Enqueue(5, 2); //add item with same priority
pq.Enqueue(6, 2); //add item with same priority
Assert.IsTrue(pq.Last() == 6, "Wrong Priority, 6 should be last");
}
示例4: ForegroundNotificationService
public ForegroundNotificationService()
{
_workQueue = new PriorityQueue();
_lastProcessedTimeInMS = Environment.TickCount;
Task.Factory.SafeStartNewFromAsync(ProcessAsync, CancellationToken.None, TaskScheduler.Default);
}
示例5: Remove
public void Remove()
{
PriorityQueue<int> p = new PriorityQueue<int>(delegate(int i, int j) { return i - j; });
Random r = new Random();
for(int i = 0; i < 10000; ++i) {
p.Enqueue(r.Next(1000));
}
int removed = 0;
while(removed < 100) {
int count = p.Count;
p.Remove(r.Next(1000));
if(count > p.Count) {
++removed;
}
}
int item = p.Dequeue();
while(p.Count > 0) {
int next = p.Dequeue();
if(next < item) {
Assert.Fail("bad order detected");
}
}
}
示例6: Main
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();
}
示例7: Solve
static void Solve(Dictionary<int, Node> allNodes, Node startNode)
{
foreach (var node in allNodes.Values)
{
node.Distance = int.MaxValue;
}
startNode.Distance = 0;
PriorityQueue<Node> nodes = new PriorityQueue<Node>();
nodes.Enqueue(startNode);
while (nodes.Count > 0)
{
var node = nodes.Dequeue();
if (node.Distance == int.MaxValue)
{
break;
}
for (int i = 0; i < node.Connections.Count; i++)
{
var newDistance = node.Distance + node.Connections[i].Distance;
if (newDistance < node.Connections[i].ToNode.Distance)
{
node.Connections[i].ToNode.Distance = newDistance;
nodes.Enqueue(node.Connections[i].ToNode);
}
}
}
}
示例8: Dijkstra
private static void Dijkstra(Graph<int> graph, Node<int> source)
{
var queue = new PriorityQueue<Node<int>>();
foreach (var node in graph.NodesCollection.Keys)
{
node.DijkstraDistance = double.PositiveInfinity;
}
source.DijkstraDistance = 0.0d;
queue.Enqueue(source);
while (queue.Count != 0)
{
var currentNode = queue.Dequeue();
if (double.IsPositiveInfinity(currentNode.DijkstraDistance))
{
break;
}
foreach (var neighbor in currentNode.Collection)
{
var potDistance = currentNode.DijkstraDistance + neighbor.Distance;
if (potDistance < neighbor.Target.DijkstraDistance)
{
neighbor.Target.DijkstraDistance = potDistance;
queue.Enqueue(neighbor.Target);
}
}
}
}
示例9: Enqueue_into_empty_queue
public void Enqueue_into_empty_queue()
{
var queue = new List<PriorityQueue<string>.Element>();
var sut = new PriorityQueue<string>(queue);
sut.Enqueue("a", 10);
Assert.That(queue, Is.EqualTo(new[]{new PriorityQueue<string>.Element("a", 10)}));
}
示例10: Main
public static void Main()
{
// 1.
var priorityQueue = new PriorityQueue<int>();
priorityQueue.Enqueue(2);
priorityQueue.Enqueue(7);
priorityQueue.Enqueue(17);
priorityQueue.Enqueue(19);
priorityQueue.Enqueue(26);
priorityQueue.Enqueue(29);
priorityQueue.Dequeue();
priorityQueue.Enqueue(3);
priorityQueue.Enqueue(1);
priorityQueue.Dequeue();
priorityQueue.Enqueue(25);
priorityQueue.Enqueue(36);
priorityQueue.Dequeue();
Console.WriteLine();
// 2.
PrintFirst20Products();
Console.WriteLine();
// 3.
CountWords();
}
示例11: DijkstraAlgorithm
// Dijkstra's shortest paths algorithm, implemented
// with priority queue. Running time: O(M * log M)
// Learn more at: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue
public static void DijkstraAlgorithm(
Dictionary<Node, List<Edge>> graph,
Node sourceNode)
{
var queue = new PriorityQueue<Node>();
foreach (var node in graph)
{
node.Key.Distance = double.PositiveInfinity;
}
sourceNode.Distance = 0.0d;
queue.Enqueue(sourceNode);
while (queue.Count != 0)
{
var currentNode = queue.Dequeue();
if (double.IsPositiveInfinity(currentNode.Distance))
{
// All nodes processed --> algorithm finished
break;
}
foreach (var childEdge in graph[currentNode])
{
var newDistance = currentNode.Distance + childEdge.Distance;
if (newDistance < childEdge.Node.Distance)
{
childEdge.Node.Distance = newDistance;
childEdge.Node.PreviousNode = currentNode;
queue.Enqueue(childEdge.Node);
}
}
}
}
示例12: Main
static void Main(string[] args)
{
var queue = new PriorityQueue<int>();
queue.Enqueue(5);
queue.Enqueue(8);
queue.Enqueue(3);
queue.Enqueue(4);
queue.Enqueue(11);
foreach (var i in queue)
{
Console.WriteLine(i);
}
Console.WriteLine("*******Removed value:{0}",queue.Dequeue());
foreach (var i in queue)
{
Console.WriteLine(i);
}
Console.WriteLine("*******Removed value:{0}", queue.Dequeue());
foreach (var i in queue)
{
Console.WriteLine(i);
}
}
示例13: Find
public static IEnumerable<int?> Find(DirectedWeightedGraph graph, int source)
{
var dist = new int?[graph.NodesCount];
dist[source] = 0;
var closestNodes = new PriorityQueue<int, int>(dist.Select((d, i) => new KeyValuePair<int, int>(i, d.GetValueOrDefault(int.MaxValue))));
var exploredNodes = new HashSet<int>();
while (closestNodes.Count != 0)
{
var node = closestNodes.ExtractHighestPriorityElement();
exploredNodes.Add(node);
foreach (var edge in graph.GetEdges(node).Where(e => !exploredNodes.Contains(e.EndNode)))
{
if (dist[node] != null)
{
var alt = dist[node].Value + edge.Weight;
if (alt < dist[edge.EndNode].GetValueOrDefault(int.MaxValue))
{
dist[edge.EndNode] = alt;
closestNodes.ChangePriority(edge.EndNode, alt);
}
}
}
}
return dist;
}
示例14: EventQueue
/// <summary>
/// Creates a new EventQueue instance and starts the main thread.
/// </summary>
public EventQueue()
{
q = new PriorityQueue<INaoEvent>(4);
thread = new Thread(new ThreadStart(Run));
thread.IsBackground = true;
thread.Start();
}
示例15: Main
public static void Main()
{
//Heap test
//Heap<int> testHeap = new Heap<int>();
//testHeap.Push(3);
//testHeap.Push(4);
//testHeap.Push(5);
//testHeap.Push(1);
//testHeap.Push(6);
//while (testHeap.Count > 0)
//{
// testHeap.Pop();
// testHeap.Print();
//}
//Queue test
PriorityQueue<int> queue = new PriorityQueue<int>();
queue.Enqueue(3);
queue.Enqueue(4);
queue.Enqueue(5);
queue.Enqueue(1);
queue.Enqueue(6);
queue.Print();
while (queue.Count > 0)
{
queue.Dequeue();
queue.Print();
}
}