6. Five shortest paths for applications that link C ++ charts (1)

Source: Internet
Author: User
Tags getv

I will introduce the basic storage methods, DFS and BFS, undirected graphs, Minimum Spanning Tree, shortest path, and active network (AOV and AOE) in detail.C ++Graph application. We have already introduced basic storage methods, DFS and BFS, undirected graphs, and minimum spanning tree. Today we will introduce the shortestPath.

Shortest Path

The shortest path may be the most eye-catching algorithm in the graph. You may have tried finding the shortest path on the map. Next we will use computers to fulfill our previous "aspirations ".

An interesting phenomenon in graph algorithms is that the larger the problem is, the simpler the algorithm is. A graph is a complex structure. For a specific problem, the result of solving a specific vertex will be affected by other vertices. It is better to solve the state of a specific sphere than a heap of a collision sphere, the status of other sphere must be considered. Since each vertex needs to be scanned, if we solve all the vertices, then the algorithm is very simple-nothing more than traversing. However, when we reduce the scale of the problem, it is natural that we want the scale of the algorithm to also decrease-if not, isn't it a cool-killing tool? However, because of the complexity of the graph, this reduction is not easy to achieve. Therefore, in order to reduce the size of the algorithm, the algorithm becomes complicated.

In the following introduction, we have clearly confirmed the above conclusion. The human cognitive process is from simple to complex. Although the surface looks like, it is complicated to find the shortest path between each pair of vertices to set the vertex to the shortest path between other vertices. However, as mentioned above, in essence, the former is simpler. The following section does not take into account the historical factors (that is, the algorithm first), or the actual ideas of the algorithm author (who has referenced or who has not referenced ), I will only elaborate on the relationship between algorithms. If any omission occurs, please forgive me.

Preparations

All the way down, the graph class is already very "bloated". To better illustrate the problem, we need to "restart the stove and open another one", and also to separate algorithms from storage methods, to facilitate reuse.

First, you must add several interfaces to the basic graph class.

 
 
  1. template <class name, class dist, class mem>  
  2. class Network  
  3. {  
  4. public:  
  5. int find(const name& v) { int n; if (!data.find(v, n)) return -1; return n; }  
  6. dist& getE(int m, int n) { return data.getE(m, n); }  
  7. const dist& NoEdge() { return data.NoEdge; }  
  8. };  
  9. template <class name, class dist>  
  10. class AdjMatrix  
  11. {  
  12. public:  
  13. dist& getE(int m, int n) { return edge[m][n]; }  
  14. };  
  15. template <class name, class dist>  
  16. class Link  
  17. {  
  18. public:  
  19. dist& getE(int m, int n)  
  20. {  
  21. for (list ::iterator iter = vertices[m].e->begin();  
  22. iter != vertices[m].e->end() && iter->vID < n; iter++);  
  23. if (iter == vertices[m].e->end()) return NoEdge;  
  24. if (iter->vID == n) return iter->cost;  
  25. return NoEdge;  
  26. }  
  27. }; 

Then the "algorithm class" is customized for the shortest path algorithm ". When finding the shortest path of a graph, bind the graph to the algorithm. For example:

 
 
  1. Network <Char,Int, Link <Char,Int> A (100 );
  2. // Insert vertex, edge ...... 
  3. Weight <Char,Int, Link <Char,Int> B (& );
  4.  
  5. # Include "Network. h" 
  6. Template<ClassName,ClassDist,ClassMem>
  7. ClassWeight
  8. {
  9. Public:
  10. Weight (Network * G): G (G), all ( False), N (G-> vNum ())
  11. {
  12. Length =NewDist * [N]; path =New Int* [N];
  13. Shortest =New Bool[N];IntI, j;
  14. For(I = 0; I <N; I ++)
  15. {
  16. Length [I] =NewDist [N]; path [I] =New Int[N];
  17. }
  18. For(I = 0; I <N; I ++)
  19. {
  20. Shortest [I] =False;
  21. For(J = 0; j <N; j ++)
  22. {
  23. Length [I] [j] = G-> getE (I, j );
  24. If(Length [I] [j]! = G-> NoEdge () path [I] [j] = I;
  25. ElsePath [I] [j] =-1;
  26. }
  27. }
  28. }
  29. ~ Weight ()
  30. {
  31. For(IntI = 0; I <N; I ++ ){Delete[] Length [I];Delete[] Path [I];}
  32. Delete[] Length;Delete[] Path;Delete[] Shortest;
  33. }
  34. Private:
  35. VoidPrint (IntI,IntJ)
  36. {
  37. If(Path [I] [j] =-1) cout <"No Path"<Endl;Return;
  38. Cout <"Shortest Path :"; Out (I, j); cout <G-> getV (j) <endl;
  39. Cout <"Path Length :"<Length [I] [j] <endl;
  40. }
  41. VoidOut (IntI,IntJ)
  42. {
  43. If(Path [I] [j]! = I) out (I, path [I] [j]);
  44. Cout <G-> getV (path [I] [j]) <"->";
  45. }
  46. Dist ** length;Int** Path;Bool* Shortest;BoolAll;IntN;
  47. Network * G;
  48. };

It is good to find that the constructor is used. The initialization of the algorithm result array is separated from the algorithm itself. As a result, the basic steps of the algorithm are easy to understand.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.