6

We are given a weighed graph G and its Shortest path distance's matrix delta. So that delta(i,j) denotes the weight of shortest path from i to j (i and j are two vertexes of the graph). delta is initially given containing the value of the shortest paths. Suddenly weight of edge E is decreased from W to W'. How to update delta(i,j) in O(n^2)? (n=number of vertexes of graph) The problem is NOT computing all-pair shortest paths again which has the best O(n^3) complexity. the problem is UPDATING delta, so that we won't need to re-compute all-pair shortest paths.

More clarified : All we have is a graph and its delta matrix. delta matrix contains just value of the shortest path. now we want to update delta matrix according to a change in graph: decreased edge weight. how to update it in O(n^2)?

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198

3 Answers3

7

If edge E from node a to node b has its weight decreased, then we can update the shortest path length from node i to node j in constant time. The new shortest path from i to j is either the same as the old one or it contains the edge from a to b. If it contains the edge from a to b, then its length is delta(i, a) + edge(a,b) + delta(b, j).

From this the O(n^2) algorithm to update the entire matrix is trivial, as is the one dealing with undirected graphs.

Chris Hopman
  • 2,082
  • 12
  • 11
  • Unless I'm seriously misreading the question, we don't know the components of the shortest path. To update the deltas of the paths W' is a component of, we would need to know them. And unless we can do *that* in constant time, we have broken our bounds limit – Robert Dec 17 '10 at 05:46
  • I didn't understand what you said indeed. We just have the delta matrix, containing just values denoting the shortest path length. How could you know edge E has been previously in the shortest path of i to j or not? you just have a number. – parsa rastegari Dec 17 '10 at 05:46
  • Let me clarify the question again: all we have is a graph and its delta matrix. delta matrix contains just value of the shortest path. now we want to update delta matrix according to a change in graph: decreased edge weight. how to update it in O(n^2)? – parsa rastegari Dec 17 '10 at 05:49
  • any suggestions? i re-phrased my question and more clarified it. see above. – parsa rastegari Dec 17 '10 at 05:56
  • Yes, I know. Read what I said again. You don't need to know if the old path contained the edge. The shortest path from a to b that contains the edge from i to j has length `delta(a, i) + edge(i, j) + delta(j, b)` – Chris Hopman Dec 17 '10 at 05:57
  • @Chris - How do you know if it's "the same as the old one or it contains the edge"? It makes sense that the sum would be lower if it contains the edge, but are you saying that's the *only* case in which it will be less? – Robert Dec 17 '10 at 06:07
  • @Robert You obviously misunderstand. I don't care if the old shortest path contained the edge or not. I only care about the length of the old shortest path and the length of the new shortest path containing the changed edge. If the new shortest path doesn't contain the edge then it is clearly the same as the old shortest path. – Chris Hopman Dec 17 '10 at 06:18
  • I just realized that I had switched i,j and a,b in the length calculation, but I don't think that is what is causing the confusion. – Chris Hopman Dec 17 '10 at 06:24
  • 1
    @Robert: It seems that Chris is right. We don't need to know whether the previous shortest path contained edge E or not. we compute the new shortest path and if it has smaller value than previous delta(i,j) we update it with the new one. @Chris: won't there be any problem with using delta(a,i) and delta(j,b)? I myself find nothing wrong with your solution now, but i think the problem intended to be more difficult that this ;) do you all agree that Chris's solution above is right?(re-emphasizing that the above approach doesn't need knowlege about previous path) – parsa rastegari Dec 17 '10 at 06:24
  • @Chris - One of us is missing something, and I suspect it's me. But I now think I understand what you're saying. The components of the shortest path a->z are themselves shortest paths. This means that `|a->i|`+`|i->j|`+`|j->z|` will be shorter than the old `a->z` iff `i->j` was part of the original path. This being the case, update. That sounds right to me, but I clearly am in no shape to verify that. – Robert Dec 17 '10 at 06:28
  • @parsa no there won't be a problem. Neither of those paths would contain the edge whose weight changed. This is because the shortest path from x to y will not contain an edge leaving y or entering x. – Chris Hopman Dec 17 '10 at 06:29
  • @Chris : in what order will you update the delta? what happens if e.g. delta(1,2) is updated and you need its value for computing delta(2,3)? you know what i mean? sounds like being more complex. – parsa rastegari Dec 17 '10 at 06:29
  • @Robert - Yes, the components of a shortest path are shortest paths. This means that, in general, if the shortest path from i to j contains a and later contains b, its length is `(i->a) + (a->b) + (b->j)`. In the case where the shortest path contains the edge from a to b, this length is `(i->a) + edge(a,b) + (b->j)`. Note that the new shortest path may be shorter than the old one even if the old one didn't contain the shortened edge. That is not a problem. – Chris Hopman Dec 17 '10 at 06:38
  • @Parsa You won't actually change any of the values for `delta(x,a)` or `delta(b,x)`. This is because a shortest path ending at `a` will not contain an edge leaving `a`. And similar for such a path beginning at `b`. – Chris Hopman Dec 17 '10 at 06:48
  • @Chris: I had missed your above comment regarding to the fact that "Neither of those paths would contain the edge whose weight changed". Well, i agree. definitely we just need delta(a,i) and delta(j,b) which none of the two can be changed since they can never contain (i,j) themselves. Well, seems that the problem is solved :) , thanks a lot. But I'm still a bit worried whether there would be any problem with this solution or not. i need the answer somewhere which i know the questions intended to be difficult ;), i myself can't figure out any problem with the stated solution above. That's fine! – parsa rastegari Dec 17 '10 at 06:51
  • This is correct. And obvious in retrospect, like all elegant solutions. It's also the key insight behind floyd-warshall, so I'm embarrassed I didn't see how to apply it here. Good job! – Nick Johnson Dec 17 '10 at 10:44
1

http://dl.acm.org/citation.cfm?doid=1039488.1039492 http://dl.acm.org.ezp.lib.unimelb.edu.au/citation.cfm?doid=1039488.1039492

Although they both consider increase and decrease. Increase would make it harder. On the first one, though, page 973, section 3 they explain how to do a decrease-only in n*n.

And no, the dynamic all pair shortest paths can be done in less than nnn. wikipedia is not up to date I guess ;)

excalibur1491
  • 1,201
  • 2
  • 14
  • 29
-2

Read up on Dijkstra's algorithm. It's how you do these shortest-path problems, and runs in less than O(n^2) anyway.

EDIT There are some subtleties here. It sounds like you're provided the shortest path from any i to any j in the graph, and it sounds like you need to update the whole matrix. Iterating over this matrix is n^2, because the matrix is every node by every other, or n*n or n^2. Simply re-running Dijkstra's algorithm for every entry in the delta matrix will not change this performance class, since n^2 is greater than Dijkstra's O(|E|+|V|log|V|) performance. Am I reading this properly, or am I misremembering big-O?

EDIT EDIT It looks like I am misremembering big-O. Iterating over the matrix would be n^2, and Dijkstra's on each would be an additional overhead. I don't see how to do this in the general case without figuring out exactly which paths W' is included in... this seems to imply that each pair should be checked. So you either need to update each pair in constant time, or avoid checking significant parts of the array.

Robert
  • 6,412
  • 3
  • 24
  • 26
  • Dijkstra gives single source shortest paths. – Sonny Saluja Dec 17 '10 at 05:25
  • Yes, Dijkstra's could be used to update one row of the matrix (and one column if the graph is undirected) in less than O(n^2). But to update the entire matrix it would be worse than O(n^2). – Chris Hopman Dec 17 '10 at 05:25
  • Well, the problem is not single source shortest path to use dijkstra and get O(n^2). we want to compute ALL-pair shortest paths(saved in delta(i,j)) and the best way to compute the whole delta is O(n^3){stated in CLRS}. Here we want to update delta(not re-compute it totally AGAIN) according to the change of one edge. – parsa rastegari Dec 17 '10 at 05:33
  • @Robert Doing something that's (roughly) n log n, n^2 times, costs O(n^3 log n), not O(n^2). Otherwise, you could do something that costs O(n!), n times for O(n)! – Nick Johnson Dec 17 '10 at 05:34
  • @sanjit, @chris - absolutely. I edited my question while you were posting, and have since updated it again. @parsa - Agreed. But with no knowledge of the components of the shortest path, there is no way to know (that I can think of) to know which components of the matrix must be updated. If you did, it would be a simple subtraction. – Robert Dec 17 '10 at 05:35
  • @ Robert: O(E+VlogV)*O(V^2) would be worse than O(V^2) which we wanted. is that right ? V=num of vertexes=n – parsa rastegari Dec 17 '10 at 05:36
  • @Nick - yes, see my edit of about 10 minutes ago. I facepalmed about 20 seconds after posting. – Robert Dec 17 '10 at 05:36
  • @Robert :We are given both G(the graph) and Delta matrix. So you have knowledge about the graph. – parsa rastegari Dec 17 '10 at 05:38
  • @parsa - Agreed. Consider that iterating over the matrix is itself O(n^2). So updating the pair would need to be done in constant time. This would be easy if you had a list of "what edges are in this shortest path?" - you could just subtract the difference. But we don't. So we need to find the shortest path again, to know the new weight - or we need to intelligently ignore large portions of the matrix. Maybe I'm missing something, but this seems nigh-impossible to do in n^2, as I noted above. – Robert Dec 17 '10 at 05:42
  • any suggestions? i re-phrased my question and more clarified it. see above. – parsa rastegari Dec 17 '10 at 05:57