Data structure: the shortest distance between points-Floyd algorithm

Source: Internet
Author: User

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:

  1. Two points are connected by edges, and weight (Vi, vj) is the smallest.
  2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.