Floyd Warshall アルゴリズムを変更することにより、ウィキペディアが提供するアプローチを読み、グラフ内の 2 つの指定された点の最短パスを印刷します。私はこれをコーディングしましたが、実際には期待される出力が得られません:
minimumDistanceMatrix[i][j]
グラフ内のすべての要素をそれぞれの重みに初期化し、行列内のすべての要素shortestPathCalculatorMatrix [i][j]
を -1 に初期化します。それで :
// Find shortest path using Floyd–Warshall algorithm for ( unsigned int k = 0 ; k < getTotalNumberOfCities() ; ++ k) for ( unsigned int i = 0 ; i < getTotalNumberOfCities() ; ++ i) for ( unsigned int j = 0 ; j < getTotalNumberOfCities() ; ++ j) if ( minimumDistanceMatrix[i][k] + minimumDistanceMatrix[k][j] < minimumDistanceMatrix[i][j] ) { minimumDistanceMatrix[i][j] = minimumDistanceMatrix[i][k] + minimumDistanceMatrix[k][j]; shortestPathCalculatorMatrix [i][j] = k; }
それで :
void CitiesMap::findShortestPathListBetween(int source , int destination) { if( source == destination || source < 0 || destination < 0) return; if( INFINITY == getShortestPathBetween(source,destination) ) return ; int intermediate = shortestPathCalculatorMatrix[source][destination]; if( -1 == intermediate ) { pathCityList.push_back( destination ); return ; } else { findShortestPathListBetween( source, intermediate ) ; pathCityList.push_back(intermediate); findShortestPathListBetween( intermediate, destination ) ; return ; } }
PS:pathCityList
は、呼び出しが行われる前に空であると想定されるベクトルfindShortestPathListBetween
です。この呼び出しの最後に、このベクトルにはすべての中間ノードが含まれています。
誰かが私が間違っているかもしれない場所を指摘できますか?