How to find the distance between the two most widely separated nodes

为君一笑 提交于 2019-12-05 20:20:13

It looks like you can use either of:

I can't give you much guidance about them though - I'm no expert.

So there's an implementation of Dijkstra which runs O(VlogV + E) giving your approach a complexity of roughly V^2logV + VE. See DADS. But perhaps more intuitive would be to run one of the all pairs shortest path algorithms like Floyd-Warshall or Johnsons. Unfortunately they're all roughly O(V^3) for dense graphs (close to the complete graph where E = V^2).

Is this the Roads in the North problem?

You can use your Dijkstra's implementation as follows:

  1. Pick a random node,(a), run Dijkstra from node a, and find the furthest node from it. Mark that node as node b.
  2. Run Dijkstra again starting at node b, and find the furthest node from it. Mark that node as node c.

I don't have proof for this, but I think b and c will be furthest away nodes. You might need to run one more iteration (I'm still thinking about it).

Multiply the edge weights by -1 and find the shortest path on the new graph. That would be the longest path on the original graph

If you want the longest shortest path that is

sup i,j {inf i,j {n : n=length of a path between i and j}}

you should certainly consider a all nodes shortest path algorithm like Flyod-Warshall as mentioned by others. This would be in the order of O(V^3).

If you want the longest possible path that is

sup i,j {n : n=length of a path between i and j}

you could try to use Midhat's idea. (which really is as complex as the original problem as pointed out in the comments) I would recommend to invert the weights with 1/w though, to retain positive weights, given the original weights were strict positive.

Another algorithm you might want to look up when dealing with negative weights is the algorithm of Bellman and Ford

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!