Exemple #1
0
        /// <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);
        }
Exemple #2
0
 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;
 }