简体   繁体   English

在计算无边顶点时如何找到短路路径?

[英]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.

相关问题 如何找到手绘多边形的边和顶点 - How to find Edges and Vertices of a hand drawn polygon 树图 - 找到多少对顶点,它们之间路径上的边总和为 C - tree graph - find how many pairs of vertices, for which the sum of edges on a path between them is C 如何找到树的任意两个顶点之间的边或顶点数量? - How to find the number of edges or vertices between any two vertices of a tree? 查找顶点边(多边形)的最佳算法 - Best Algorithm to find the edges (polygon) of vertices 如何修改 Disjktra 算法以在最短路径中至少有 X 个顶点或 K 个边 - How to modify Disjktra algorithm to have at least X vertices or K edges in shortest path 用x个红色顶点找到2个顶点之间的路径 - Find path between 2 vertices with x red vertices 如何在DAG中找到两个顶点之间的最大权重路径? - How to find the maximum-weight path between two vertices in a DAG? 如何获得具有已知边的抽象多边形的顶点 - How to get the vertices of an abstract polygon with known edges 如何为mst的所有顶点对找到最大路径边缘 - How to find maximum edge of path for all pairs of vertices of mst 在网格中使用顶点(2D和3D)查找边缘的算法 - algorithm to find edges using vertices (2D and 3D) in a mesh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM