Floyd algorithm
Floyd algorithm
Dijkstra is used to solve the single-source shortest path problem, while Floyd is used to solve the shortest path problem between points. The design strategy of the Floyd algorithm is dynamic planning, while Dijkstra adopts greedy strategy. Of course, greedy algorithms are a special case of dynamic planning.
Algorithm IDEA
There are only two possible conditions for the shortest path between points:
- Two points are connected by edges, and weight (Vi, vj) is the smallest.
- Another point is the mediation point, which is connected by two points to minimize weight (Vi, VK) + weight (VK, vj.
Min_distance (Vi, vj) = min {weight (Vi, vj), weight (Vi, VK) + weight (VK, vj )}. Based on the logic behind this, coupled with the idea of dynamic planning, it constitutes the Floyd algorithm. Therefore, after the VK obtains all vertices, distance (Vi, vj) can be minimized.
ASIDE: the Code itself is not important, and the algorithm IDEA is the essence. The idea is very difficult to get, but with the idea, you can write code with a little experience. Salute to the creator of ideas!
It is difficult to think, but the code is relatively simple.
# Include <iostream> # include <iomanip> # include <stack> using namespace STD; # define maxweight 100 # UNDEF infinity # define infinity 1000 class graph {PRIVATE: // Number of vertices int numv; // Number of edges int nume; // adjacent matrix int ** matrix; public: Graph (INT numv ); // create the void creategraph (INT nume); // The Destructor ~ Graph (); // Floyd algorithm void Floyd (); // print the adjacent matrix void printadjacentmatrix (); // check the input bool check (INT, Int, INT );};
Class implementation
// Constructor, specify the number of vertices graph: Graph (INT numv) {// checks the number of input vertices while (numv <= 0) {cout <"incorrect number of vertices! Re-enter "; CIN> numv;} This-> numv = numv; // construct the adjacent matrix and initialize matrix = new int * [numv]; int I, J; for (I = 0; I <numv; I ++) matrix [I] = new int [numv]; for (I = 0; I <numv; I ++) for (j = 0; j <numv; j ++) {if (I = J) matrix [I] [I] = 0; elsematrix [I] [J] = infinity ;}} void graph: creategraph (INT nume) {/* checks the number of input edges for a directed graph of numv vertices, up to numv * (numv-1) edges */while (nume <0 | nume> numv * (numv-1) {cout <"edges are faulty! Enter "; CIN> nume;} This-> nume = nume; int tail, Head, weight, I; I = 0; cout <"Enter the start point (arc tail), End Point (ARC header), and weight of each edge" <Endl; while (I <nume) {CIN> tail> head> weight; while (! Check (tail, Head, weight) {cout <"the input edge is incorrect! Enter "<Endl; CIN> tail> head> weight;} matrix [tail] [head] = weight; I ++ ;}} graph ::~ Graph () {int I; for (I = 0; I <numv; I ++) Delete [] matrix [I]; Delete [] matrix ;} /* find the shortest distance between each vertex pair and its path using the fresh algorithm */void graph: Floyd () {// in order not to modify the adjacent matrix, use a two-dimensional array int ** distance = new int * [numv]; int I, j; for (I = 0; I <numv; I ++) distance [I] = new int [numv]; // initialize for (I = 0; I <numv; I ++) for (j = 0; j <numv; j ++) distance [I] [J] = matrix [I] [J]; // Prev array int ** Prev = new int * [numv]; for (I = 0; I <numv; I ++) Prev [I] = new in T [numv]; // initialize prevfor (I = 0; I <numv; I ++) for (j = 0; j <numv; j ++) {If (Matrix [I] [J] = infinity) Prev [I] [J] =-1; elseprev [I] [J] = I;} int D, v; For (V = 0; v <numv; V ++) for (I = 0; I <numv; I ++) for (j = 0; j <numv; j ++) {d = distance [I] [v] + distance [v] [J]; If (d <distance [I] [J]) {distance [I] [J] = D; Prev [I] [J] = V ;}// print the distance and Prev arrays cout <"distance... "<Endl; for (I = 0; I <numv; I ++) {( J = 0; j <numv; j ++) cout <SETW (3) <distance [I] [J]; cout <Endl ;} cout <Endl <"prev... "<Endl; for (I = 0; I <numv; I ++) {for (j = 0; j <numv; j ++) cout <SETW (3) <Prev [I] [J]; cout <Endl ;}cout <Endl; // print the vertex to the shortest path stack <int> S; for (I = 0; I <numv; I ++) {for (j = 0; j <numv; j ++) {If (distance [I] [J] = 0); else if (distance [I] [J] = infinity) cout <"vertex" <I <"to vertex" <j <"no path! "<Endl; else {S. push (j); V = J; do {v = Prev [I] [v]; S. push (V);} while (V! = I ); // print the path cout <"vertex" <I <"to vertex" <j <"the shortest path length is" <distance [I] [J] <", the path sequence is... "; while (! S. empty () {cout <SETW (3) <S. top (); S. pop () ;}cout <Endl ;}} cout <Endl ;}// release space for (I = 0; I <numv; I ++) {Delete [] distance [I]; Delete [] Prev [I];} Delete [] distance; Delete [] Prev;} // print the adjacent matrix void graph :: printadjacentmatrix () {int I, j; cout. SETF (IOs: Left); cout <SETW (7) <""; for (I = 0; I <numv; I ++) cout <SETW (7) <I; cout <Endl; for (I = 0; I <numv; I ++) {cout <SETW (7) <I; for (j = 0; j <numv; j ++) cout <SETW (7) <matrix [I] [J]; cout <Endl ;}} bool graph: Check (INT tail, int head, int weight) {If (tail <0 | tail> = numv | HEAD <0 | HEAD> = numv | weight <= 0 | weight> = maxweight) return false; return true ;}
Main Function
Int main () {cout <"******* Floyd ***** by David *****" <Endl; int numv, nume; cout <"Graph creation... "<Endl; cout <" Number of input vertices "; CIN> numv; graph (numv); cout <" Number of input edges "; CIN> nume; graph. creategraph (nume); cout <Endl <"Floyd... "<Endl; graph. floyd (); System ("pause"); Return 0 ;}
Run
Summary: The Floyd algorithm code seems very long, but it is not difficult. Many of the code is used for preparation and output.
Download complete code: Floyd algorithm
Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/38366923
If this is helpful, try again!
Column directory:
- Data Structure and algorithm directory
- C pointer