简体   繁体   English

C编程语言,最短路径

[英]C programming language, shortest path

I'm am writing a code to find the shortest distance between two points. 我正在编写代码以查找两点之间的最短距离。 My code is working perfect till now. 到目前为止,我的代码运行良好。 I mean it finds the distance and the path that they should pass. 我的意思是它找到了他们应该经过的距离和路径。 I need to print this information, but I should make a print function. 我需要打印此信息,但是我应该执行打印功能。 The way it works is something like that: For example initial point is 4 and final is 13. 它的工作方式是这样的:例如,起始点是4,最终点是13。

I should come up with an algorithm that check their intermediate points. 我应该提出一种算法来检查它们的中间点。 Let's say between 4 & 13 there is point : 7 假设4和13之间有一点:7

4--7--13 Now I need to check every point between them like: 4--7--13现在,我需要检查它们之间的每个点,例如:

4--6--7--9--13 To be more specific it will check if there is a point between 4-6 and 6-7 and 7-9 and 9-13. 4--6--7--9--13更具体地说,它将检查4-6和6-7与7-9和9-13之间是否存在点。 So next in the next iteration it may be formed another list like: 因此,在下一个迭代中的下一个可能会形成另一个列表,例如:

4--2--6--7--5--9--17--13 Now let's say that there will not be any intermediate value between them. 4--2--6--7--5--9--17--13现在假设它们之间没有任何中间值。 And that is what I should print. 这就是我应该打印的内容。 I really would appreciate any help, suggestion that you may give to me 我真的很感谢您的帮助,建议您可以给我

The Warshall-Floyd algorithm (used by the OP), has a version which is able to determine the path in addition to the distance between nodes of a graph: Warshall-Floyd算法(由OP使用)具有一种版本,该版本除了可以确定图的节点之间的距离之外,还可以确定路径:

Floyd-Warshall algorithm with path-reconstruction 具有路径重构的Floyd-Warshall算法

However, it must be noted that this is not the best possible algorithm to solve the shortest-path problem . 但是,必须注意,这不是解决最短路径问题的最佳算法。

This sounds like recursion would be the best way to do this. 这听起来像递归将是执行此操作的最佳方法。 If it already can find the shortest path, im assuming you have a function written to find the shortest path between 2 points. 如果已经可以找到最短路径,则假定您已编写了一个函数来查找2点之间的最短路径。 Maybe you could recursively break down the list, find the shortest path and append that point to a list. 也许您可以递归地分解列表,找到最短路径,然后将该点附加到列表中。

Edit, sorry i misread your question, you need to find the midpoint. 编辑,对不起,我误读了您的问题,您需要找到中点。 Pass a recursive function the whole list of points and find a midpoint. 将整个点列表传递给递归函数,然后找到一个中点。 If one exists, add it to a list. 如果存在,请将其添加到列表中。 If there is no midpoint dont append anything. 如果没有中点,请勿添加任何内容。 Continue calling this function until you come to the base case, which should be 1 or 2 points in the list 继续调用此函数,直到出现基本情况,在列表中应该为1或2点

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

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