/// <summary> /// ワーシャルフロイド法 /// O(N^3) /// </summary> /// <param name="graph">グラフ</param> /// <param name="nodeNum">ノードの数</param> /// <returns>各ノード間の最短距離を辺として持つグラフ</returns> public static WeightGraph WarshallFloyd(WeightGraph graph) { var res = new WeightGraph(graph); foreach (var b in Enumerable.Range(0, graph.NodeNum)) { foreach (var a in Enumerable.Range(0, graph.NodeNum)) { foreach (var c in Enumerable.Range(0, graph.NodeNum)) { res.Add(a, c, Min(res.GetLength(a, c), res.GetLength(a, b) + res.GetLength(b, c))); } } } return(res); }
public WeightGraph(WeightGraph graph) { this.adjacencyMatrix = new long[graph.NodeNum, graph.NodeNum]; foreach (var i in Enumerable.Range(0, graph.NodeNum)) { foreach (var j in Enumerable.Range(0, graph.NodeNum)) { this.adjacencyMatrix[i, j] = graph.GetLength(i, j); } } this.NodeNum = graph.NodeNum; }