简体   繁体   中英

algorithm for singe source shortest path problem

we are given a directed graph G = (V, E) with positive and negative edge weights, but no cycles. Let s ∈ V be a given source vertex. How to find an algorithm that finds distance of all vertices from s, supposably runs faster than Bellman Ford's O(VE) time complexity.

If the graph has no cycles, then you can just process the vertices in topological order.

For each vertex v , if it is reachable from s at distance d , then for every edge (v,u) with weight w , mark u as reachable with weight d+w . If u is already reachable at a lower weight, then leave it alone.

Because you process the graph in topological order, you know that when you process a vertex v , you will already have processed all its predecessors, so you will know the length of its shortest path from s . The first reachable vertex will, of course, be s .

It's pretty easy to combine this with Kahn's algorithm for topological sorting, on the subgraph of vertices reachable from s .

  1. First do a BFS search to find all the vertices reachable from s , and simultaneously count each vertex's incoming edges within this subset.
  2. s will be the only vertex with count '0'. It also has a known 0 distance from s . Put it in a queue.
  3. While there are vertexes in the queue:
    1. Remove a vertex v from the queue
    2. Adjust the distances to its neighbors
    3. Reduce the incoming edge counts of it's neighbors. If any neighbor's count gets to 0, then put it in the queue.

When you're done, all reachable vertexes will be process and all their distances will be known.

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