[英]How to find shortes path while counting vertices no edges?
I have a task from Graphs where I need to find the shortest path. 我有一个来自Graph的任务,我需要找到最短的路径。
It won't be normal alorithm because you don't have to count paths, you need to count vertices. 这不是正常的算法,因为您不必计算路径,而需要计算顶点。
The rule is like the cost of going through the particular Vertex is: 规则就像遍历特定顶点的成本是:
| P = costOfPathFromThePreviousVertex - costOfPathToTheNextVertex |
So for example when you have Graph: A-> B-> C-> 因此,例如,当您拥有图:A-> B-> C->
and the cost is 成本是
A->B = 10 ;
B->C = 15 ;
the cost of going through Vertex B will be: P = | 通过顶点B的成本为:P = | 10 - 15 |
10-15 |
Assumption is that the root and destination vertices have the the cost = 0 . 假设根顶点和目标顶点的成本为0。
so in above case the cost of going from A to C via B will be 5. 因此在上述情况下,从A通过B到C的成本为5。
Its easy to say but I have no idea which of algorithms I need to implement to get the result when I have x vertices. 说起来容易,但是我不知道当我有x个顶点时需要实现哪种算法才能得到结果。 I was thinking about Dijkstra's algorithm and DFS as well but they are incorrect in that case.
我也在考虑Dijkstra的算法和DFS,但在那种情况下它们是不正确的。
Any help will be very appreciate. 任何帮助将不胜感激。
I would add a dummy start and destination vertex connected to the actual start and destination. 我将添加一个连接到实际起点和终点的虚拟起点和终点顶点。
Then turn your graph into a line graph which has a vertex for each edge, eg v1="A->B" and v2="B->C". 然后将您的图变成一个线图 ,该线图的每个边都有一个顶点,例如v1 =“ A-> B”和v2 =“ B-> C”。
Assign weights to the edges in the new graph according to your formula. 根据您的公式将权重分配给新图形中的边缘。
For example, the edge connecting v1 to v2 will have weight |10-15|. 例如,将v1连接到v2的边的权重| 10-15 |。
Then use Dijkstra's algorithm on this line graph to find the shortest path between the dummy start edge and the dummy end edge. 然后在该线图上使用Dijkstra算法,找到虚拟起始边缘和虚拟终止边缘之间的最短路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.