/*graph structure, adjacency matrix form*/elemtype Nodes[n];intEdges[n][n];p Rim_or_dijkstra (intIndexBOOLUseprim)/*starting point*/{ intDist[n] = {INF};/*distance from start point to all other sides*/ intDistindex[n] = {-1 }; intVisited[n] = {0 }; intselected = index;/*the selected point*//*the reach edge distance at which the starting point is initialized*/ for(i =0; i < nodes.length; i++ )/*edges[start [end]= weight (not inf on edge), poor lift*/ { if(Visited[i]) Break; Visited[i]=true; if(Edges[selected][i]! =INF) {Dist[i]= Edges[selected][i];/*the distance from index to reach Edge*/ } } for(k =0; K < Nodes.length-1; k++)/*n-1 Cycle Pick -up point*/{visited[selected]=true; intMin =INF; intSEL =-1; for(i =0; i < nodes.length; i++ )/*edges[start [end]= weight (not inf on edge), poor lift*/ { if(Visited[i]) Break;/*belong to the same collection without consideration*/ if(Edges[selected][i]! =INF) { if(Min >Dist[i]) {min= Dist[i]; SEL =i; }/*min= the shortest distance from the collection of points that have been selected to other individual points*//*sel= the corresponding point*/}} Distindex[sel]= selected;/*sel->selected mapping, storing edges*//*One sel corresponds to only one selected,*//*a selected corresponds to multiple sel*//*first time Distindex v2->v1*/selected= sel;/*the selected point to update*//*at this*//*Prim in order to dist[selected]=0*//*Dijkstra do not add code*/ if(Useprim) {dist[selected]=0; } for(i =0; i < nodes.length; i++)/*selected->i, updating other edges*//*since selected is already part of the collection,*//*The distance of the selected of the edge belongs to the accessible edge of the set*/ {/*take a smaller value from the distance of all the set's accessible edges (without the collection itself)*//*here are the same parts of the two methods*/ if( !Visited[i]&& Dist[i] > dist[selected] +Edges[selected][i]) {Dist[i]= dist[selected] +Edges[selected][i]}} }
Contact:
The same greedy strategy is used when the two sides are connected, that is, the set of extensions always find a shortest edge into the set in the non-expanded point, and modify the remaining points to the nearest short circuit of the set with the points of the new collection.
In common: there are visited tag array , dist array, min,distindex array
Note: Most tutorials on the web, the prim algorithm is not visited array, it is simplified with dist[]=0, and here, both methods use the visited array, reducing the difference :
lies in the modification and significance of Dist
Dijkstra just one more dist[selected], used as a cumulative distance
And prim because of dist[selected]=0, so can eliminate dist[selected]
Prim
if (!visited[i] && dist[i] > Edges[selected][i])
{Dist[i] = edges[selected][i];}
Explanation: The inner edge of the set in the prim is considered a short-circuit, negligible, 0 length
Dijkstra
if (!visited[i]
&& Dist[i] > dist[selected] + edges[selected][i])
{Dist[i] = dist[selected] + edges[selected][i]}
Explanation: The inner edge of the collection inside the Dijkstra cannot be ignored, the length exists
The relation and difference between prim algorithm and Dijkstra algorithm