Ai
1 Star 1 Fork 0

AndyZhang/C-Sharp-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DijkstraAllPairsShortestPaths.cs 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
Nicholas Rodine 提交于 2018-02-08 02:38 +08:00 . Removed unused usings (#61)
/***
* Computes Dijktra's shortest paths for all pairs of vertices in a graph.
* This is a wrapper class that applies single-source dijkstra shortest paths (DijkstraShortestPaths<TG, TV>)
* to all vertices of a graph and saves the results in a dictionary index by the vertices.
*/
using System;
using System.Collections.Generic;
using DataStructures.Graphs;
namespace Algorithms.Graphs
{
public class DijkstraAllPairsShortestPaths<TGraph, TVertex>
where TGraph : IGraph<TVertex>, IWeightedGraph<TVertex>
where TVertex : IComparable<TVertex>
{
/// <summary>
/// INSTANCE VARIABLES
/// </summary>
Dictionary<TVertex, DijkstraShortestPaths<TGraph, TVertex>> _allPairsDjkstra;
/// <summary>
/// CONSTRUCTOR
/// </summary>
public DijkstraAllPairsShortestPaths(TGraph Graph)
{
if (Graph == null)
throw new ArgumentNullException();
// Initialize the all pairs dictionary
_allPairsDjkstra = new Dictionary<TVertex, DijkstraShortestPaths<TGraph, TVertex>>();
var vertices = Graph.Vertices;
foreach (var vertex in vertices)
{
var dijkstra = new DijkstraShortestPaths<TGraph, TVertex>(Graph, vertex);
_allPairsDjkstra.Add(vertex, dijkstra);
}
}
/// <summary>
/// Determines whether there is a path from source vertex to destination vertex.
/// </summary>
public bool HasPath(TVertex source, TVertex destination)
{
if (!_allPairsDjkstra.ContainsKey(source) || !_allPairsDjkstra.ContainsKey(destination))
throw new Exception("Either one of the vertices or both of them don't belong to Graph.");
return _allPairsDjkstra[source].HasPathTo(destination);
}
/// <summary>
/// Returns the distance between source vertex and destination vertex.
/// </summary>
public long PathDistance(TVertex source, TVertex destination)
{
if (!_allPairsDjkstra.ContainsKey(source) || !_allPairsDjkstra.ContainsKey(destination))
throw new Exception("Either one of the vertices or both of them don't belong to Graph.");
return _allPairsDjkstra[source].DistanceTo(destination);
}
/// <summary>
/// Returns an enumerable collection of nodes that specify the shortest path from source vertex to destination vertex.
/// </summary>
public IEnumerable<TVertex> ShortestPath(TVertex source, TVertex destination)
{
if (!_allPairsDjkstra.ContainsKey(source) || !_allPairsDjkstra.ContainsKey(destination))
throw new Exception("Either one of the vertices or both of them don't belong to Graph.");
return _allPairsDjkstra[source].ShortestPathTo(destination);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/strongandyzhang/C-Sharp-Algorithms.git
git@gitee.com:strongandyzhang/C-Sharp-Algorithms.git
strongandyzhang
C-Sharp-Algorithms
C-Sharp-Algorithms
master

搜索帮助