简体   繁体   中英

How to find shortes path while counting vertices no edges?

I have a task from Graphs where I need to find the shortest path.

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->

and the cost is

  A->B = 10 ;
  B->C = 15 ;

the cost of going through Vertex B will be: P = | 10 - 15 |

Assumption is that the root and destination vertices have the the cost = 0 .

so in above case the cost of going from A to C via B will be 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. I was thinking about Dijkstra's algorithm and DFS as well but they are incorrect in that case.

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".

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|.

Then use Dijkstra's algorithm on this line graph to find the shortest path between the dummy start edge and the dummy end edge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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