简体   繁体   English

加权图最短路径设计C ++

[英]Weighted Graph Shortest Path Design C++

I'm having trouble coming up with a plan for a project assigned in a C++ Data Structures course. 我在为C ++数据结构课程中分配的项目制定计划时遇到了麻烦。 I'm not asking for written code, but a plan of attack. 我不是要书面代码,而是要制定攻击计划。 Basically I'm having a hard time thinking this through. 基本上,我很难做到这一点。

How would you go about constructing a weighted graph from this weighted non-directed map with the intention of being able to find the shortest path from a set vertex (Angel's Stadium) to: 您将如何从此加权无向地图构造加权图,以期能够找到从一组顶点(天使体育场)到以下位置的最短路径:

  1. All major league stadiums 所有大联盟球场
  2. All national league stadiums 所有国家联赛体育场
  3. All American league stadiums 所有美国联赛球场

I also have to allow a user to plan their dream vacation by selecting Stadiums they want to visit. 我还必须允许用户通过选择要访问的体育场来计划他们的梦想假期。

The program only needs to print the number of stadiums visited and the total distance traveled in each case. 该程序仅需要打印每种情况下参观的体育场数量和总行驶距离。

My first thought is to have methods for each of the four cases. 我的第一个想法是针对这四种情况分别制定方法。 I'm not even sure how I should go about returning the number of stadiums visited and distance. 我什至不确定我应该如何返回参观过的体育场的数量和距离。 Is using a struct optimal when you need to return two values? 当需要返回两个值时,是否使用结构最优?

Let alone which algorithm to use for shortest path. 更不用说哪种算法用于最短路径。 I have an implementation of Dijkstra's from a previous assignment. 我有以前任务中的Dijkstra的实现。 Some others in my class are using a minimal spanning tree. 我班上的其他一些人正在使用最小生成树。

It would make more sense to have one function for the shortest path that takes in a list of the stadiums to visit but I don't know how I would structure the adjacency matrix. 在一条要访问的体育场列表中最短的路径上具有一个功能会更有意义,但是我不知道如何构造邻接矩阵。

I'm making an input file to read in the adjacency matrix formated like so (edge, weight): 我正在制作一个输入文件,以按如下格式(边缘,重量)读取邻接矩阵:

0 1 340
0 2 110

Is this the optimal structure for this case? 这是针对这种情况的最佳结构吗? I'm not sure how to differentiate between major league, national, and American. 我不确定如何区分大联盟,国家和美国。 Are separate input files necessary? 是否需要单独的输入文件? Or is there a better way of organizing this? 还是有更好的组织方式?

Here's the full assignment if you are interested: [word document] 如果您感兴趣的话,这是完整的作业: [word文档]

You need to implement different instances of TSP (without the final hop) with the stadiums in the tour chosen according to the criteria specified in the question. 您需要根据问题中指定的标准在巡回赛中选择体育场来实施TSP的不同实例(无最终跃点)。

For instance, 例如,

  1. Design a trip to all the major league stadiums with the shortest path starting at a stadium in California. 从加利福尼亚的体育场出发,以最短的路线设计一次前往所有大型联盟体育场的行程。 Print the number of stadiums visited and the total distance travelled. 打印参观的体育馆数量和总路程。

minCost = inf minCost = inf

for (stadium in CaliforniaStadiums ) 用于(加利福尼亚州体育场内的体育场)

minCost = min(minCost, TSP (stadium, MLStadiums)) minCost = min(minCost,TSP(体育场,MLS体育场))

Hope that helps. 希望能有所帮助。

Rudra 鲁德拉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM