Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the shortest path of C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to calculate the shortest path of C++". In daily operation, I believe many people have doubts about how to calculate the shortest path of C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to calculate the shortest path of C++". Next, please follow the editor to study!

The shortest path

The shortest path is probably the most eye-catching algorithm for beginners-finding the shortest path on the map may have been tried by everyone. Now we use computers to fulfill our "wishes".

There is an interesting phenomenon in the algorithm of the graph, that is, the larger the scale of the problem, the simpler the algorithm. The graph is a complex structure. For a particular problem, the result of solving a particular vertex will be affected by other vertices-just like a bunch of spheres that collide with each other. If you want to solve the state of a particular sphere, you must consider the state of other spheres. Since every vertex has to be scanned, if all vertices are solved, the algorithm is very simple-nothing more than traversing. However, when we reduce the size of the problem, it is only natural that we want the size of the algorithm to be reduced as well-if not, instead of killing chickens. However, it is precisely because of the complexity of the graph that this reduction is not easy to achieve. Therefore, in order to reduce the scale of the algorithm, the algorithm is complex.

In the following introduction, the above conclusion is clearly confirmed. People's cognitive process is from simple to complex, although on the surface, finding the shortest path between each pair of vertices is more complex than the shortest path between specific vertices to other vertices, but, as mentioned above, the former is essentially simpler. The following introduction does not take into account historical factors (that is, which algorithm was proposed first), nor does it take into account the true ideas of the proposer of the algorithm (who referred to or did not refer to who), but only from the relationship between the algorithms themselves. If there are any omissions, please forgive me.

Preparatory work

Along the way, the graph has become very "bloated". In order to explain the problem more clearly, it is necessary to "reopen the stove and open a new business". At the same time, it is also to separate the algorithm from the storage method for reuse.

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

Template class Network {public: int find (const name& v) {int n; if (! data.find (v, n) return-1; return n;} dist& getE (int m, int n) {return data.getE (m, n);} const dist& NoEdge () {return data.NoEdge;}; template class AdjMatrix {public: dist& getE (int m, int n) {return edge [m] [n];}} Template class Link {public: dist& getE (int m, int n) {for (list::iterator iter = vertices [m] .e-> begin (); iter! = vertices [m] .e-> end () & & iter- > vID

< n; iter++); if (iter == vertices[m].e->

End () return NoEdge; if (iter- > vID = = n) return iter- > cost; return NoEdge;}}

Then there is the "algorithm class" that is tailor-made for the shortest path algorithm. When finding the shortest path to a graph, bind the graph to the algorithm, for example:

Network a (100); / / insertion point, edge. Weight b (& a); # include "Network.h" template class Weight {public: Weight (Network* G): G (G), all (false), N (G-> vNum ()) {length = new dist* [N]; path = new int* [N]; shortest = new bool [N]; int I, j; for (I = 0; I

< N; i++) { length[i] = new dist[N]; path[i] = new int[N]; } for (i = 0; i < N; i++) { shortest[i] = false; for (j = 0; j < N; j++) { length[i][j] = G->

GetE (I, j); if (length [I] [j]! = G-> NoEdge ()) path [I] [j] = i; else path [I] [j] =-1;} ~ Weight () {for (int I = 0; I < N; iTunes +) {delete [] path [I]; delete [] path [I];} delete [] length; delete [] path; delete [] shortest } private: void print (int I, int j) {if (path [I] [j] = =-1) cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report