Floyd Algorithm Learning Notes

Source: Internet
Author: User

Algorithm Ideas

Path matrix

The shortest path matrix between every two points is obtained by the weight matrix of a graph. from the graph of the weighted adjacency matrix A=[a (i,j)] nxn start, recursive N-Update, that is, by the Matrix D (0) =a, according to a formula, The Matrix D (1), and the same formula by D (1) to construct D (2), and so on. Finally, the same formula is used to construct the matrix D (n) by D (n-1). Matrix d (n) I row j column element is the shortest path length of vertex to J vertex of I, called D (N) as the distance matrix of the graph, and can also introduce a successor matrix path to record the shortest path between two points. state transition Equationthe equation of state transfer is as follows: Map[i,j]:=min{map[i,k]+map[k,j],map[i,j]};Map[i,j] represents the shortest distance from I to J, K is the breakpoint of the exhaustive i,j, Map[n,n] The initial value should be 0. of course, if this road does not pass, but also must be special treatment, such as no map[i,k] this road. Core Algorithms1, starting from any one-sided path. The distance between all two points is the right of the edge, and if no edge is connected between the two points, then the right is infinity. 2, for each pair of vertices u and V, see if there is a vertex w to make the path from u to W to v shorter than known. If it is updated. The graph is represented by the adjacency matrix G, and if there is a way from VI to VJ, then g[i,j]=d,d represents the length of the road; otherwise g[i,j]= Infinity. Defines a matrix D used to record the information of the insertion point, d[i,j] represents the point from VI to VJ, and initializes the d[i,j]=j. Insert each vertex into the graph, comparing the distance from the insertion point to the original distance, g[i,j] = min (G[i,j], g[i,k]+g[k,j]), or d[i,j]=k if the value of g[i,j] becomes smaller. Contains information about the shortest path between two points in G, while in D contains information for the shortest pass path.

The complexity of time and complexity of space

Time complexity: Because the core algorithm is a three for loop with relaxation method, the time complexity is O (n^3)

Spatial complexity: The entire algorithm space consumption is a n*n matrix, so its spatial complexity is O (n^2)

C + + code

Floyd.cpp:Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "FStream"
#define MaxLen 20
#define MAXIMUM 100
using namespace Std;

typedef struct GRAPH
{
int vertex;
int edge;
int Matrix[maxlen][maxlen];
};
int _tmain (int argc, _tchar* argv[])
{
Ofstream Outwrite;
Outwrite.open ("H.txt", ios::app|ios::out);
outwrite<< "Welcome to the graph world!\n";
outwrite<< "The initial matrix is:\n";
int vertexnumber;
int edgenumber;
int beginning,ending,weight;
int Mindistance[maxlen][maxlen];
int Interval[maxlen][maxlen];
Graph Floydgraph;
cout<< "Welcome to the graph world!" <<endl;
cout<< "Input the number of the vertex:";
cin>>vertexnumber;
cout<< "Input the number of the edge:";
cin>>edgenumber;
for (int i = 0; i < Vertexnumber; i++)
{
for (int j = 0; J < Vertexnumber; J + +)
{
Floydgraph.matrix[i][j]=maximum;
}
}
for (int i = 0; I <edgenumber; i++)
{
cout<< "Please input the beginning index:";
cin>>beginning;
cout<< "Please input the ending index:";
cin>>ending;
cout<< "Please input the distance of the" the ";
cin>>weight;
Floydgraph.matrix[beginning][ending]=weight;
}
for (int i = 0; I <vertexnumber; i++)
{
for (int j = 0; J < Vertexnumber; J + +)
{
MINDISTANCE[I][J]=FLOYDGRAPH.MATRIX[I][J];
outwrite<<floydgraph.matrix[i][j]<< "\ t";
Interval[i][j]=-1;
}
outwrite<< "\ n";
}
for (int k = 0; k <vertexnumber; k++)
{
for (int i = 0; i < Vertexnumber; i++)
{
for (int j = 0; J < Vertexnumber; J + +)
{
if (Mindistance[i][j]>mindistance[i][k]+mindistance[k][j])
{
MINDISTANCE[I][J]=MINDISTANCE[I][K]+MINDISTANCE[K][J];
Interval[i][j]=k;
}
}
}
}
outwrite<< "\ n" << "after the Floyd transition, The matrix is:" << "\ n";
for (int i = 0; i < Vertexnumber; i++)
{
for (int j = 0; J < Vertexnumber; J + +)
{
cout<< "The mindistance between" <<i<< "and" <<j << "is:";
cout<<mindistance[i][j]<<endl;
cout<< "The points pass through the point:" <<interval[i][j];
cout<<endl;
outwrite<<mindistance[i][j]<< "\ t";
}
outwrite<< "\ n";
}
outwrite<< "\ n";
outwrite<< "The points between the beginning point and the ending point is:" << "\ n";
for (int i = 0; i < Vertexnumber; i++)
{
for (int j = 0; J < Vertexnumber; J + +)
{
outwrite<<interval[i][j]<< "\ t";
}
outwrite<< "\ n";
}
Outwrite.close ();
GetChar ();
GetChar ();
GetChar ();
return 0;
}

Floyd Algorithm Learning Notes

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.