[1] Why is Freud's algorithm needed?
The shortest path problem from a single source point to all vertices in a weighted graph can be solved using Dijkstra's algorithm .
What if the shortest path between each vertex in the graph and other vertices is required? Similar conceivable methods are:
Each time a vertex is used as the source point, the Dijkstra algorithm is repeated n times.
In this way, we can theoretically find the shortest path between each vertex and other vertices, and the total execution time is O(n3).
All right! In order to achieve this medium requirement, another solution algorithm can be used: Freud's algorithm.
In order to better understand the subtlety of Freud's algorithm, let's first look at a simple case.
The following figure is the simplest 3-vertex connected network graph:
Attach the dist array and prev array: (dist on the left and prev on the right)
[2] Freud's algorithm
Freud's algorithm is a very beautiful algorithm, simple and intuitive.
Unfortunately, because of its triple loop, it is also O(n*n*n) time complexity.
If you are faced with the problem of finding the shortest path from all vertices to all vertices?
It's a good choice.
The algorithm code is as follows:
1 #include " stdafx.h " 2 #include<iostream> 3 #include< string > 4 #define MAX_VERTEX_NUM 100 5 #define INFINITY 65535 6 typedef int Pathmatirx[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 7 typedef int ShortPathTable[MAX_VERTEX_NUM][MAX_VERTEX_NUM ]; 8 using namespace std; 9 typedef struct Graph // Adjacency matrix of directed graph 10 { 11 charvexs[MAX_VERTEX_NUM]; // Array for storing vertices 12 int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // Define a critical matrix 13 int vexnum, arcnum; // Total number of vertices, total number of edges 14 }Graph; 15 16 int LocateVex (Graph G, char ch) // search for 17 { 18 for ( int i = 0 ; i < G.vexnum; i++ ) 19 if (G.vexs[i] == ch) 20 return i; 21 return - 1 ; 22 } 23 24 void CreateGraph(Graph &G) // Create an undirected graph 25 { 26 char c1, c2; // Arc tail and arc head 27 int i, j, weight; // weight is weight 28 cout << " Please Enter the total number of vertices and edges (separated by spaces): " ; 29 cin >> G.vexnum >> G.arcnum; 30 cout << " Please enter the vertex information (separated by spaces): " << endl; 31 for (i = 0 ; i < G.vexnum;i++) 32 { 33 cin >> G.vexs[i]; 34 } 35 for (i = 0 ; i < G.vexnum; i++ ) 36 for (j = 0 ; j < G.vexnum; j++ ) 37 G.arcs[i] [j] = INFINITY; 38 cout << " Please enter the arc tail, arc head and weight: " << endl; 39 for ( int k = 0 ; k < G.arcnum; k++ ) 40 { 41 cin >> c1 >> c2 >> weight; 42 i =LocateVex(G, c1); 43 j = LocateVex(G, c2); 44 G.arcs[i][j] = weight; 45 } 46 } 47 48 void ShortestPath_Floyd(Graph G, int prev[MAX_VERTEX_NUM][MAX_VERTEX_NUM] , int dist[MAX_VERTEX_NUM][MAX_VERTEX_NUM]) 49 { // Floyd algorithm, find the shortest path prev[v][w] and weighted length dist[v][w] from the vertex v to the rest vertex w in the network graph G 50 int v, w, k; 51 for (v = 0 ; v < G.vexnum; v++ ) 52 for (w = 0; w < G.vexnum; w++) // Initialize dist and prev 53 { 54 dist[v][w] = G.arcs[v][w]; // dist[v][w] value is the corresponding point 55 prev[v][w] = w; // initialize prev 56 } 57 for (k = 0 ; k < G.vexnum; k++) // update path 58 for (v = 0 ; v < G .vexnum; v++ ) 59 for (w = 0 ; w < G.vexnum; w++ ) 60 { // If the path through the subscript k vertex is shorter than the path between the original two points 61 if(dist[v][w] > dist[v][k] + dist[k][w]) 62 { 63 dist[v][w] = dist[v][k] + dist[k][w ]; 64 prev[v][w] = prev[v][k]; // The path is set through the vertex with index k 65 } 66 } 67 for (v = 0 ; v < G.vexnum; v++) / / output function 68 { 69 for (w = v + 1 ; w < G.vexnum; w++ ) 70 { 71 cout << G.vexs[v] << " -" << G.vexs[w] << " weight: " << dist[v][w]<<" "; 72 int k = prev[v][w]; 73 cout << "path: " << G.vexs[v]; 74 while (k != w) 75 { 76 cout << "->" << G.vexs[k]; 77 k = prev[k][w]; 78 } 79 cout << "->" << G.vexs[w]<<" "; 80 } 81 cout << endl; 82 } 83 } 84 85 int main() 86 { 87 Graph G; 88 int prev[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 89 int dist[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 90 int v0; 91 CreateGraph(G); 92 ShortestPath_Floyd(G, prev, dist); 93 94 }
Test Results: