0

I've got a problem with a 2-section question:

G=(V,E) is undirected unweighted graph. t,s are nodes in the graph. e=(a,b) is an edge in the graph.

1) Suggest an efficient algorithm that checks if e is a part of all shortest paths from s to t.

2) Suggest an efficient algorithm that checks if e is a part of one of the shortest paths from s to t.

I've seen in the forum suggestion to solving section 1, by using Dijkstra algorithm twice, once with the given edge and once without. Then you need to compare the results. However, I didn't managed to figure more efficient way to solve section 2. I guess it is possible, but I don't know how.

Any suggestions?

2 Answers2

2

Actually for an unweighted, undirected graph, you don't need to use Dijkstra, a simple BFS shall serve the purpose.

Following method checks whether e is a part of atleast one shortest path from s to t:

Compute the shortest path from s to e and the shortest path from e to t

If the sum of the lengths of these two paths is equal to the shortest path from s to t, then e is a part of atleast one shortest path from s to t.

s -----> e -------> t

If you want to know whether e is a part of exactly one shortest path from s to t, then in addition, the following link maybe be helpful. It concerns a directed graph, but our undirected graph can be thought of as a directed graph with edge from u to v and v to u.

How to find the number of different shortest paths between two vertices, in directed graph and with linear-time?

Community
  • 1
  • 1
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

Problem 2 should be should be solvable using one (modified) pass of Dijkstra's algorithm.

As before, you keep the frontier of the search space in a priority queue, but you also add one more bit of information, which is a flag that denotes whether the path that lead to this part of the frontier went through the edge in question.

  • Initially, just the vertex s is in the queue, and the flag is false.
  • The flag is propagated (kept as true) if it was true when popped off the head of queue.
  • The flag is set to true for the first time, whenever the edge e is traversed (i.e. if you needed to traverse e to get to a v then when pushing v onto the queue, you set the flag to true).

e was used if when your target, t is popped from the stack, its flag is set to true.

amnn
  • 3,657
  • 17
  • 23