First, Dijkstra algorithm (greedy to find the shortest distance algorithm)
In this algorithm, I follow my own understanding to name, it will be easier to understand.
#defineMAXSIZE 100#defineunvisited 0#defineVisited 1#defineINFINITY 66666typedefstructTool {intVisited[maxsize]; /*whether an array is accessed, Visited[i] indicates that vertex i has been accessed, that is, the shortest distance to vertex i has been calculated*/ intKnown_shortest_distance[maxsize]; /*the shortest distance array known. Known_shortest_distance[i] represents the shortest distance from V0 to VI*/ intPrevious_vertex[maxsize]; /*Previous_vertex[i]=j, Representative: On the shortest path from V0 to VI, the previous vertex of vertex i is the vertex J*/} tool;typedefstructGragh {intnum_of_vertexes; intadjacent_matrix[maxsize][maxsize];} Gragh;voidInit_tool (Gragh g, Tool &t) {inti =0; for(i =0; i < g.num_of_vertexes; i++) {T.visited[i]=unvisited; T.known_shortest_distance[i]= g.adjacent_matrix[0][i]; T.previous_vertex[i]=0; }}voidDijstra_shortest_distance (Gragh G, Tool &t) {intI, J, X;//Loop Variable intmin_distance, X_index; /*Min_distance stores the minimum distance from V0 to VX in the currently known shortest distance array; X_index storage of VX subscript*/Init_tool (g, t); for(i =0; i < g.num_of_vertexes; i++) {min_distance=INFINITY; for(x =0; x < g.num_of_vertexes; X + +) { if(!t.visited[x] && t.known_shortest_distance[x] <min_distance) {min_distance=T.known_shortest_distance[x]; X_index=x; } } /*The minimum value in the shortest distance array through the above loop is min_distance; The subscript of the vertex is x_index; In vertices that are not currently accessed and have the shortest starting distance from V0, the subscript of this point is X_index.*/T.visited[x_index]=visited; for(j =0; J < G.num_of_vertexes; J + +) { if(!t.visited[j] && (min_distance + g.adjacent_matrix[x_index][j] <T.known_shortest_distance[j])) {T.known_shortest_distance[j]= Min_distance +G.adjacent_matrix[x_index][j]; T.PREVIOUS_VERTEX[J]=X_index; } } }}
First, the Floyd algorithm (regardless of 3,721, the entire figure of any two points in the shortest distance to find out again)
typedefstructFloyd_tool {intKnown_shortest_distance[maxsize][maxsize]; /*the shortest distance array known. KNOWN_SHORTEST_DISTANCE[I][J] represents the shortest distance from VI to VJ*/ intPath_matrix[maxsize][maxsize]; /*Path_matrix[i][j]=k, Representative: On the shortest path from VI to VJ, go through the vertex k; Use k instead of I until Path_matrix[k][j]=j*/} Floydtool;voidInit_floyd_tool (Floydtool &ft, Gragh g) { for(inti =0; i < g.num_of_vertexes; i++) { for(intj =0; J < G.num_of_vertexes; J + +) {Ft.known_shortest_distance[i][j]=G.adjacent_matrix[i][j]; FT.PATH_MATRIX[I][J]=J; } }}voidFloyd_shortest_distance (Gragh G,floydtool &ft) {Init_floyd_tool (ft,g); for(inti =0; i < g.num_of_vertexes; i++){ for(intj =0; J < g.num_of_vertexes;j++){ for(intK =0; k < g.num_of_vertexes;k++){ if(Ft.known_shortest_distance[j][k] >Ft.known_shortest_distance[j][i]+Ft.known_shortest_distance[i][k]) {Ft.known_shortest_distance[j][k]=Ft.known_shortest_distance[j][i]+Ft.known_shortest_distance[i][k]; FT.PATH_MATRIX[J][K]=Ft.path_matrix[j][i]; } } } }}
Improved version:
voidFloyd_shortest_distance (Gragh G,floydtool &ft) {Init_floyd_tool (ft,g); for(inti =0; i < g.num_of_vertexes; i++){ for(intj =0; J < g.num_of_vertexes;j++){ if (ft.known_shortest_distance[j][i] = = INFINITY) continue; for(int k = j + 1; k < g.num_of_vertexes;k++){ if (ft.known_shortest_distance[k][i] = = INFINITY) continue; if(Ft.known_shortest_distance[j][k] >Ft.known_shortest_distance[j][i]+Ft.known_shortest_distance[i][k]) {Ft.known_shortest_distance[j][k]=Ft.known_shortest_distance[j][i]+Ft.known_shortest_distance[i][k]; FT.PATH_MATRIX[J][K]=Ft.path_matrix[j][i]; } } } }}
Data structure--graph--Shortest path d&f algorithm