Class notes: the shortest path

In a non-net graph , the shortest path refers to the path with the fewest number of edges between two vertices .
In the net diagram , the shortest path refers to the path with the shortest sum of the weights on the edge experienced between two vertices . The shortest path from a
single source point to other vertices : Dijkstra method, the shortest path
between any pair of vertices of O (n2) : Floyed method, the shortest path of O (n3)
single source point
Problem description: given weighted directed graph G = (V, E) and the source point v ∈ V, find the shortest path from v to the remaining vertices in G.
Application example-the problem of computer network transmission: how to find the most economical way to send a message from one computer to all other computers on the Internet.
Dijkstra proposed an algorithm for generating the shortest path in the order of increasing path length- Dijkstra's algorithm.
Understanding of increasing path length
A graph with n vertices, calculate the shortest path from vertex v to other vertices (n-1) in the graph, and find n-1 shortest paths in total.
Increasing according to the path length refers to the calculation principle of the n-1 roads, that is, the first shortest path (v, vi) is found first, the shortest path among all n-1 roads, and then the second shortest path ( v, vj) ...
Dijkstra's algorithm
Basic idea:

1. Set a set S to store the vertices of the shortest path found, the initial state of S contains only the source point v,
2. For vi∈VS, assume that the directed edge from the source point v to vi is the shortest path (the initial value of the shortest path from v to the remaining vertices).
3. Each time a shortest path v,…, vk is obtained, vk is added to the set S, and the paths v,…, vk, vi are compared with the original hypothesis, and the shortest path is taken as the shortest path.
The above process is repeated until all vertices in set V are added to set S.
The characteristic of the shortest path with the shortest path length (that is, the first shortest path) :
on this path, there must be only one edge, and the weight on this edge is the smallest.
The characteristics of the shortest path with the next shortest path length :
it can only have two cases: either directly from the source point to this point (only one edge); or from the source point through the vertex v1 (the first shortest path The vertex to which it is attached), and then reach the vertex (composed of two edges).
The characteristics of the next shortest path with the next shortest path length :
it may have four cases: either directly from the source point to the point (only one edge); or from the source point through the vertex v1, and then to the vertex (by Composed of two edges); or from the source point through vertex v2, and then reach the vertex (two edges); or from the source point through vertices v1, v2, and then reach the vertex (multiple edges).
The characteristics of the remaining shortest path :
it is either directly from the source point to this point (only one edge); or it passes from the source point through the vertex of the shortest path (the vertex in the set S), and then reaches the vertex.
Proof
of correctness The next shortest path is either an arc (v0, vx), or a path that passes through some vertices in S and then reaches vx.
Using the method of reproof: Suppose there is a vertex vy on the next shortest path that is not in S, that is, this path is (v0, ..., vy, ..., vx). Obviously, the length of (v0, ..., vy) is less than the length of (v0, ..., vy, ..., vx), so the next shortest path should be (v0, ..., vy), which is the same as the hypothetical next shortest path ( v0, ..., vy, ..., vx) contradict! Therefore, there can be no vertex vy not in S on the next shortest path, that is, the assumption is not true.
Data structure :
graph storage structure : adjacency matrix storage structure
array dist [n] : each component dist [i] represents the length of the shortest path currently found from the starting point v to the ending point vi. The initial state is: if there is an arc from v to vi, then dist [i] is the weight on the arc; otherwise, dist [i] is set to ∞.
Array path [n] : path [i] is a string representing the shortest path currently found from the starting point v to the ending point vi. The initial state is: if there is an arc from v to vi, then path [i] is vvi; otherwise, path [i] is set to an empty string.
Array s [n] : store the source point and the end point of the shortest path that has been found. Its initial state is only one source point v.
The main steps of Dijkstra's algorithm are as follows :
(1) g is a weighted graph represented by adjacency matrix. S ← {v0}, dist [i] = g.arcs [v0] [vi], path [i] = “v0vi” or “”; initialize the path length from v0 to the remaining vertices as weights;
(2) select vk, such that dist [vk] = min (dist [i] | vi∈VS), vk is the end point of the next shortest path from v0 that is currently obtained. Add vk to S
(3) Modify the length of the shortest path from v0 to any vertex vi on the set VS. If dist [k] + g.arcs [k] [i] <dist [i] then modify dist [i] to dist [k] + g.arcs [k] [i] path [i] = path [k ] + “Vi”
(4) Repeat (2) and (3) n-1 times to find the shortest path from v0 to each other vertex in the graph in the increasing order of the shortest path length.

const int MAX=1000; 
void  Dijkstra(MGraph g, int v){        
	for ( i =0; i<g.vexnum ; i++){   
		dist[i]=g.arcs[v][i];                  
		if ( dist[i]!= MAX)                        
			path [i]=g.vertex[v]+g.vertex[i];
		else                       
			path[i]=“”;        
	}        
	S[0]=g.vertex[v];         
	num=1;  
	While (num<g.vextexNum){     
		k=0;     
		for(i=0;i<G.vertexNum;i++)            
			if((dist[i]<dist[k])   
				k=i     
		cout<<dist[k]<<path[k];     
		s[num++]=G.vertex[k];
		for(i=0;i<G.vertexNum;i++)              
			if(dist[k]+g.arc[k][i]<dist[i] {    
				dist[i]=dist[k]+g.arc[k][i];                        
				path[i]=path[k]+g.vertex[i];                
			} 
	} 
}             


Description of the shortest path between each pair of vertices : given a weighted directed graph G = (V, E), for any vertex vi, vj ∈ V (i ≠ j), find the shortest path from vertex vi to vertex vj .
Solution 1: Each time one vertex is used as the source point, the Dijkstra algorithm is called n times. Obviously, the time complexity is O (n3).
Solution 2: The Floyd algorithm proposed by Freud to find the shortest path between each pair of vertices has a time complexity of O (n3), but the form is simpler.
The basic idea of ​​the Floyd algorithm is as follows :
suppose graph g is expressed by the adjacency matrix method, and find the shortest path between any pair of vertices vi and vj in graph g. (-1) Initialize the shortest path length from vi to vj to (vi, vj), and then perform the following n comparisons and corrections:
(0) Add vertex v0 between vi and vj and compare (vi, v0, vj) The length of the path of (vi, vj), and the shorter path is taken as the shortest path from vi to vj and the middle vertex number is not greater than 0.
(1) Add vertices v1 between vi and vj to get (vi,…, v1) and (v1,…, vj), where: (vi,…, v1) is from vi to v1 and the middle vertex number is not greater than 0 The shortest path of (v1,…, vj) is the shortest path from v1 to vj and the middle vertex number is not greater than 0. These two paths have been found in the previous step. Compare (vi,…, v1,…, vj) with the shortest path obtained in the previous step and the intermediate vertex number of vi to vj is not greater than 0, and take the shorter path as vi to vj and the intermediate vertex number is not The shortest path greater than 1.
(2) Add the vertex v2 between vi and vj to get (vi,…, v2) and (v2,…, vj), where: (vi,…, v2) is from vi to v2 and the middle vertex number is not greater than 1 The shortest path of (v2,…, vj) is the shortest path from v2 to vj and the middle vertex number is not greater than 1. These two paths have been found in the previous step. Compare (vi,…, v2,…, vj) with the shortest path found in the previous step and the intermediate vertex number from vi to vj is not greater than 1, and take the shorter path as vi to vj and the intermediate vertex number is not The shortest path greater than 2.
...
Design
the storage structure of the data structure diagram : the weighted adjacency matrix storage structure
array dist [n] [n] : store the shortest path length obtained during the iteration process. Iteration formula :
dist-1 [i] [j] = arc [i] [j],
dist k [i] [j] = min {distk-1 [i] [j], distk-1 [i] [k ] + distk-1 [k] [j]}
0 ≤ k ≤
n -1 array path [n] [n] : store the shortest path from vi to vj, initially path [i] [j] = “vivj” .
Floyd algorithm-C ++ description

void Floyd(MGraph G) 
{     
	for (i=0; i<G.vertexNum; i++)                
		for (j=0; j<G.vertexNum; j++)        
		{           
			dist[i][j]=G.arc[i][j];           
			if (dist[i][j]!=)                 
				path[i][j]=G.vertex[i]+G.vertex[j];           
			else 
				path[i][j]="";
		}
		for (k=0; k<G.vertexNum; k++)                  
			for (i=0; i<G.vertexNum; i++)                   
				for (j=0; j<G.vertexNum; j++)                
					if (dist[i][k]+dist[k][j]<dist[i][j]) {
						dist[i][j]=dist[i][k]+dist[k][j];                     
						path[i][j]=path[i][k]+path[k][j];               
					} 
}
Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103333937