当前位置: 首页>>代码示例>>C#>>正文


C# AdjacencyGraph.ShortestPathsDijkstra方法代码示例

本文整理汇总了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);
        }
开发者ID:GovorukhaOksana,项目名称:QuickGraph,代码行数:17,代码来源:WikiSamples.cs

示例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;
        }
开发者ID:reisergames,项目名称:evenav,代码行数:52,代码来源:NavigationService.cs


注:本文中的AdjacencyGraph.ShortestPathsDijkstra方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。