In a Weighted Directed Graph G (with positive weights), with n vertex and m edges, we want to calculate the shortest path from vertex 1 to other vertexes. We use 1-dimensional array D[1..n], and D[1]=0 and others filled with +infinity for updating the array we can just use Realx (u, v).

If D[v] > D[u] + W(u, v) then D(v):=D(u)+W(u,v)

in which W(u, v) is the weight of u-->v edge, how many time must we call the relax function to ensure that for each vertex u, D[u] equals to length of shortest path from vertex 1 to u?

Solution: I think this is Bellman-Ford and m*n times we must call.

Could anyone help me? In Dijkstra and Bellman-Ford and others we have Relax functions. How do we detect which one of them?

From CLRS Book:

The algorithms in this chapter differ in how many times they relax each edge and the order in which they relax edges. Dijkstra’s algorithm and the shortest-paths algorithm for directed acyclic graphs relax each edge exactly once. The Bellman-Ford algorithm relaxes each edge |V|-1 times.

More Huimen Maisori's questions See All
Similar questions and discussions