本文整理汇总了C#中PriorityQueue.PullLowest方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.PullLowest方法的具体用法?C# PriorityQueue.PullLowest怎么用?C# PriorityQueue.PullLowest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.PullLowest方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static IEnumerable<INode> Run(INode start, INode finish, IEnumerable<INode> nodes)
{
if (!nodes.Contains(start) || !nodes.Contains(finish))
throw new System.ArgumentException("Both `start` and `finish` must be in `nodes`.");
var distances = nodes.ToDictionary(node => node, _ => int.MaxValue);
var notVisitedQueue = new PriorityQueue(nodes, distances);
notVisitedQueue.DecreasePriority(start, 0);
var previous = new Dictionary<INode, INode>(notVisitedQueue.Count);
bool ifPathFound = false;
while (!notVisitedQueue.IsEmpty())
{
var nearest = notVisitedQueue.PullLowest();
if (distances[nearest] == int.MaxValue)
break;
if (nearest.Equals(finish))
{
ifPathFound = true;
break;
}
var links = nearest.GetLinks();
foreach (var link in links)
{
var alt = distances[nearest] + link.Weight;
if (alt < distances[link.Neighbour])
{
notVisitedQueue.DecreasePriority(link.Neighbour, alt);
previous[link.Neighbour] = nearest;
}
}
}
List<INode> result = new List<INode>();
if (ifPathFound)
{
INode current = finish;
while (current != start)
{
result.Add(current);
current = previous[current];
}
result.Add(current);
result.Reverse();
}
return result.AsEnumerable();
}