14

I'm currently reading introduction to algorithms and came by Johnson’s algorithm that depends on making sure that all paths are positive.

the algo depends on finding a new weight function (w') that is positive for all edges and keeps the correctness of the shortest paths relations.

It does so by calculating h(s), h(d) values to be added to the w original value.

My question is, why not just find the smallest w in the graph and add it to all edges ? this will satisfy both conditions and will require less calculation.

Raphael
  • 72,336
  • 29
  • 179
  • 389
Mr.Me
  • 243
  • 1
  • 2
  • 7

2 Answers2

25

Adding a weight to every edge adds more weight to long paths than short paths. (Long in the sense of having many edges.)

For example, suppose the lowest-cost edge has weight $-2$ and there are two paths from $a$ to $b$: a single edge of weight $3$ and a path with two edges, each of weight $1$. The two-edge path has the lowest weight. However, if you add $2$ to every edge, the one-edge path has weight $5$ but the two-edge path now has weight $6$, so you get the wrong answer.

David Richerby
  • 81,689
  • 26
  • 141
  • 235
0

Increasing every edge weight by the same amount does not necessarily increase every path by the same amount of distance. Rather, the increase to the paths are often disproportional which depends on how many edges the path has.