Reputation: 5349
I was looking through "Fundamentals of Computer Algorithms" book for multi stage graph problem.
It says:
Algorithm Graph(G,k,n,p)
{
cost[n]=0;
for j=n-1 to 1 step -1 do
{
Let r be a vertex such that<j,r> is an edge of G and c[j,r]+cost[r] is minimum
cost[j]=c[j,r]+cost[r]
}
}
The author says that the complexity is O(|V| + |E|). Where the |V| is the number of vertices and |E| is the number of edges.
I know the for loop runs for total number of vertices and the inside line has to select a near edge.
I couldn't understand the logic behind
Upvotes: 1
Views: 7907
Reputation: 13289
To further your understanding, look at Dijsktra's algorithm on an abritrary digraph, each edge is only considered once as well. The running time is O(|E| + |V lg V|).
Because a multistage graph is partitioned into sets, you can find the shortest path by set because you know that the vertexes in set X to the target node must be visited before set X-1. You also know that vertexes in the same set don't have edges between each other. In short, you know the order in which to process them and don't have to consider all possible vertexes each time as in Dijsktra
Upvotes: 0