1. Let's talk about the idea of the prim algorithm:
As we all know, the prim algorithm is a minimum spanning tree algorithm that uses the greedy principle (which is not proved here). It sets two vertex sets, one of which is the vertex set a of the required spanning tree, another set is vertex B, which is not added to the Spanning Tree. Its implementation process is as follows:
Step 1: All vertices are in Set B, and set a is empty.
Step 2: Start with a vertex. Add this initial vertex to set a and subtract this vertex from Set B (the code implementation is very simple, that is, set an array of tags, if it is set to false, it indicates that the point is in B, and if it is set to true, it indicates that the point is in a.) Find the shortest point in the path adjacent to it. For example, add this point to set, subtract This vertex from Set B (the code implementation is the same as above ).
Step 1: Set a already has multiple vertices. In this case, if two sets A and B are located, you only need to find the shortest edge of the point from the set to the point in the B set, it can be any combination of vertices in set a and Set B. This shortest edge has two vertices, And the vertices in Set B are added to set, (It is a bit tricky to implement the Code. You do not need to enumerate all the situations, that is, update operations ).
Step 2: Repeat the above process. Until all vertices end in set.
2. Let's talk about the Dijkstra algorithm process:
The process of this algorithm is a little more step than that of the prim algorithm, but the idea is clever and greedy. It aims to find the shortest distance from a source point to the target point, in general, the Dijkstra algorithm is to find the shortest path from a source point to the target point. The solution is to find the shortest distance and shortest distance from the source point to the entire graph, third Short Distance and so on (these distance are the shortest distance from the source point to a certain point )..... Each distance corresponds to a vertex, that is, the shortest distance to this vertex, that is, the shortest distance from the origin to all vertices, and a two-dimensional array exists, finally, the destination point can be directly obtained through the Table query to obtain the shortest distance.
Step 1: How to obtain the shortest distance from the source point (assuming S1? The distance between the vertex adjacent to this source vertex and The Source Vertex is all placed in an array Dist []. If the distance is not reachable, DIST [] is the maximum value. Here we will explain why it is a one-dimensional array, the reason is that the default value is from the source point to the value of this one-dimensional array, only the target point can be used as the subscript, at this time, the shortest "one" path from the source point to other points will be available, as long as the smallest Dist [] is selected (the other endpoint of the shortest path is assumed to be S2 ).
Step 1: Search for the next short distance from the source point (assuming S1) to another point, the distance or the value in DIST, or the distance from the shortest distance selected in step 1 + from the finding point (assuming S2) to other points (in fact, this is also an update operation, the updated value is the value in DIST []. If the distance from the shortest distance + from this point (assuming S2) to other points is smaller than the value in DIST, you can update the DIST [] array, and then select the "second" Short Path (next short path) with the smallest value from the DIST [] array ).
Step 2: Search for the "3" short path. At this time, the endpoint (S3) of the second short path updates the value in the Dist [] array of the adjacent vertex.
Step 1: Repeat the above process n-1 times (N refers to the number of nodes) to obtain the result. In fact, the shortest path from the source point to all points is obtained, in the DIST [] table, you only need to look up the table to find the shortest path from the source point.
Reference code: http://hi.baidu.com/zp9450/blog/item/37ae08fa4e0a752b4f4aea18.html