Given a weighted graph g= (v,e), where the right of each edge is a real number, in addition to a vertex in V, called the source, now calculates the shortest path length from the source to each of the other vertices. The path length here refers to the sum of the right side of the road. This problem is called single source shortest path problem. There are 3 algorithms for calculating single-source shortest path, Dijkstra, Bellman-ford, SPFA algorithm
1. Dijkstra algorithm
Based on greedy thought, the algorithm calculates the shortest path of a node to all other nodes. The algorithm requires that there is no negative right side, if we want to calculate the single-source shortest path of the graph with negative weights, we use 2 different methods.
To divide all the vertices in the graph into 2 sets, VA and VB, if the source point s to a vertex U's shortest path has been determined, then you join VA, the first time VA contains only the source point S, the rest of the points belong to VB, after the end of the algorithm, all from the source point s reachable point belongs to the VA, the unreachable point Can be found in the shortest path length of the same as the shortest path, the method is to record the end of the previous point, so long as the backward check back to determine the entire shortest path.
Specific steps:
1) First initialization, the source point s to the graph points of the direct distance as the initial value recorded as s to each point of the shortest distance, if not directly arrived, recorded as inf,s to s distance of 0
2) in all VB points to find a s to its shortest path length point u, you remove from VB, added to the VA.
3) from the determined U point update S to VB not a little bit of v distance, if the s->u+u->v is less than s->v then update this distance
4) Repeat steps 2, 3, until there is no point in VB or VB points can not reach the source point s
In fact, Dijkstra and prim algorithm very similar, just because of the problem, the implementation process of the calculation content is different, the former calculates the path length, the latter comparison of the length of the edge.
Algorithm implementation:
PackageCom.langsin.duisort;ImportJava.util.HashSet;ImportJava.util.Scanner;ImportJava.util.Set;/*test Case: 7 50 1 1000 2 300 4 102 1 602 3 603 1 104 3 500//Source Point*/ Public classTest {StaticScanner sc=NewScanner (system.in); Static intEdge,node;//Number of edges, vertices Static int[] map=New int[20] [20]; Static Boolean[] visited=New Boolean[20];//true belongs to VA, false belongs to VB Static int[] dest=New int[20];//record the shortest path from the location to the source point Static int[] pre=New int[20];//record the previous node in the location Public Static voidMain (string[] args) {Edge=Sc.nextint (); Node=Sc.nextint (); for(inti=0;i<=node;i++) for(intj=0;j<=node;j++) Map[i][j]= 10000;//Initialize to Infinity intF, T, W;//Edge f->t weight is w for(inti=0;i<edge;i++) {f=Sc.nextint (); T=Sc.nextint (); W=Sc.nextint (); Map[f][t]=W; } //input source point s ints=Sc.nextint (); Dijkstra (node,s); } Public Static voidDijkstra (intNintu) { for(inti=0;i<n;i++) { if(i!=u) {Dest[i]=Map[u][i]; Visited[i]=false; Pre[i]=u; }} Dest[u]=0; Visited[u]=true; intk=-1,mini=1000; for(inti=0;i<n-1;i++) {//Cycle n-1 TimesMini=1000;k=-1; for(intj=0;j<n;j++){ if(visited[j]==false&&dest[j]<mini) {//in VB, take a point with the smallest distance to the source point KMini=Dest[j]; K=J; } } if(K==-1)return;//There are no extensible pointsvisited[k]=true; for(intj=0;j<n;j++){ //for each point J adjacent to K in VB, update the shortest path of S to J if(visited[j]==false&&map[k][j]!=10000&&dest[j]>dest[k]+Map[k][j]) {Dest[j]=dest[k]+Map[k][j]; PRE[J]=K; } } } }}
Consider: How to find the shortest path!
Single Source Shortest Path