What is the shortest path?
Single Source Shortest path (the so-called single-source shortest path is just a vertex, the shortest path is the minimum of the weights of the other vertices and the path between the vertices)
What is the shortest path problem?
Given a belt weight graph, the weights of each edge in the graph are nonnegative and represent the distance between the two vertices. Specifies that a vertex in the graph is the source point, and the shortest path and length of the source point to other vertices are identified, which is the single source shortest path problem.
What is the Dijkstra algorithm?
The common method for solving single-source shortest path problem is Dijkstra (Dijkstra) algorithm. The algorithm uses a greedy strategy: each time it finds the closest vertex in the remaining vertex to the source point.
Algorithmic thinking
With the weighted graph g=<v,e>, so that S is the set that has the shortest path vertex determined, the v-s can be used to represent the set of remaining indeterminate shortest path vertices. Assuming V0 is the source point, the initial s={v0}. Use the array distance to represent the path length of the source point V0 to the remaining vertices, using the array pre[i] to represent the previous vertex of vertex i on the shortest path sequence. Initially, Pre[i] is the subscript of the source point. The next step is to repeat two steps:
- Find the smallest one from the current Distance[i], record its subscript v=i, the shortest path of the source point V0 to the vertex vv is determined, add VV to S.
- Updates the shortest path length of the source point to the remaining vertex. The Update method is: Vertex vv of the above step is the middle point, if Distance[v]+weight (v,i) <distance[i], then the value is modified: Pre[i]=v;distance[i]=distance[v]+weight (v,i);
Repeat the two steps until the shortest path to all vertices has been found.
It needs to be pointed out that the Dijkstra algorithm solves not only the direction graph, but also the non-direction graph. A complete example of a weighted graph is given below:
Here are some examples:
Weighted graph with direction
The solution process of the Dijkstra algorithm (which specifies that the INF is the meaning of Infinity Infinity). )
A simple implementation of the Dijkstra algorithm for the forward network based on adjacency matrix storage:
Const intInfinity = +;//defines an infinity constant, denoted by 1000//define graph structure, using adjacency matrix storage formTemplate <intMax_size>classgraph{Private:/*adjacency Matrix, which holds the weighted value for a forward network (weighted graph)*/Adjacent[max_size][max_size]; Public: voidDijkstra (int);//Dijkstra algorithm to find the shortest path};//Dijkstra Algorithm implementation (weighted graph based on adjacency matrix storage)voidGraph::D Ijkstra (intvertex) { //Note: The subscript indicates the node. intCount =0;//used to record the number of nodes visited, followed by control loops BOOLFind[max_size];//to mark nodes that have found the shortest path intPre[max_size];//Shortest path to the predecessor node that holds the current node intDistance[max_size];//the shortest path for storing the current node//Initialize for(intI=0; i<max_size;i++) Pre[i]= vertex;//The beginning of all the nodes is the beginning of the vertex for(intI=0; i<max_size;i++) Distance[i]= Adjacent[vertex][i];//the weighted value stored in the adjacency matrix is the distance for(intI=0; i<max_size;i++) Find[i]=false;//no shortest path found for initialization of all nodesFind[vertex] =true; intv = vertex;//the variable used to iterate vertices intD//used to denote distance while(Count <max_size) {D=Infinity; for(intI=0; i<max_size;i++)//find an unreachable node from the shortest path to the initial node { if(!find[i] && distance[i]<d) {d=Diatance[i]; V=i; }} Find[v]=true; //update the predecessor and shortest distance of the remaining nodes for(intI=0; i<max_size;i++) { if(!Find[i]) { /*The node of the shortest path found above as the starting point, * connected to other inaccessible nodes, * when the path from the initial node to this node is short, * The last node is used as the precursor node, updated A moment .*/D= Distance[v] +Adjacent[v][i]; if(D <Distance[i]) {Pre[i]=v; Distance[i]=D; } }} count++; } }
Reference: http://blog.csdn.net/zhangxiangdavaid/article/details/38360337
The shortest path Dijkstra algorithm for a forward network (weighted graph)