本文整理汇总了C#中AdjacencyGraph.ShortestPathsDijkstra方法的典型用法代码示例。如果您正苦于以下问题:C# AdjacencyGraph.ShortestPathsDijkstra方法的具体用法?C# AdjacencyGraph.ShortestPathsDijkstra怎么用?C# AdjacencyGraph.ShortestPathsDijkstra使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AdjacencyGraph
的用法示例。
在下文中一共展示了AdjacencyGraph.ShortestPathsDijkstra方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShortestPath
public void ShortestPath()
{
var cities = new AdjacencyGraph<int, Edge<int>>(); // a graph of cities
cities.AddVerticesAndEdge(new Edge<int>(0, 1));
Func<Edge<int>, double> cityDistances = e => e.Target + e.Source ; // a delegate that gives the distance between cities
int sourceCity = 0; // starting city
int targetCity = 1; // ending city
// vis can create all the shortest path in the graph
// and returns a delegate vertex -> path
var tryGetPath = cities.ShortestPathsDijkstra(cityDistances, sourceCity);
IEnumerable<Edge<int>> path;
if (tryGetPath(targetCity, out path))
foreach (var e in path)
Console.WriteLine(e);
}
示例2: NearestRoute
public IEnumerable<MapSolarSystemEdge> NearestRoute(long fromSolarSystemID, long toSolarSystemID, bool safeOnly = false)
{
eveGraph = new AdjacencyGraph<SolarSystem, Edge<SolarSystem>>(); // Eve universe
getPathCache = new Dictionary<long, TryFunc<SolarSystem, IEnumerable<Edge<SolarSystem>>>>(); // function cache
var dbResult = (from s in context.SolarSystems
from e in s.ToSolarSystems
where s.security >= (safeOnly ? 0.5 : -20)
select new { From = s, To = e }).AsEnumerable();
var edges = dbResult.Select(a => new Edge<SolarSystem>(a.From, a.To)).OrderBy(e => e.Source.solarSystemID).ToList();
var vertices = context.SolarSystems;
eveGraph.AddVertexRange(vertices); // Fill Graph
eveGraph.AddEdgeRange(edges); // Fill Graph
var allPathes = new List<IEnumerable<Edge<SolarSystem>>>();
var result = new List<MapSolarSystemEdge>();
var from = context.SolarSystems.Where(m => m.solarSystemID == fromSolarSystemID).SingleOrDefault();
var linkedSystems = from system in context.SolarSystems
where system.solarSystemID == toSolarSystemID
&& system.security >= (safeOnly ? 0.5 : -20)
select system;
TryFunc<SolarSystem, IEnumerable<Edge<SolarSystem>>> tryGetPath;
if (getPathCache.ContainsKey(from.solarSystemID))
{
tryGetPath = getPathCache[from.solarSystemID];
}
else
{
tryGetPath = eveGraph.ShortestPathsDijkstra(v => 1, from);
getPathCache[from.solarSystemID] = tryGetPath;
}
foreach (var s in linkedSystems)
{
IEnumerable<Edge<SolarSystem>> path;
if (tryGetPath(s, out path))
{
allPathes.Add(path);
}
}
var shortestPath = allPathes.OrderBy(p => p.Count()).FirstOrDefault();
if (shortestPath == null)
return null;
foreach (var item in shortestPath)
{
result.Add(new MapSolarSystemEdge() { From = item.Source.solarSystemID, To = item.Target.solarSystemID });
}
return result;
}