repeating's blog

By repeating, history, 7 years ago, In English

Hello everyone

I'm wondering how to find the number of distinct shortest paths in an undirected unweighted graph !!

any advice !! :D

  • Vote: I like it
  • +27
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +69 Vote: I do not like it

It shouldn't be hard. Just do a bfs and then you have dist[i] = the distance between source and node i. Sort the nodes by their dists and then compute some dp value for each node, in the order of the sorting, with dp[i] representing the number of distinct shortest paths from source to node i. The recurrence would be: dp[i] = sum of dp[j] so that dist[j] + 1 = dist[i] and you have an edge from j to i. You can see that using the sorting order, you have computed all dps that you had interest in beforehand, so it would work. The final complexity is O(N+M) because sorting isn't just a standard sort. You could use count sort, or even take the nodes in the order they were popped from the queue. Of course, the answer is dp[N] where N is the destination and initializing requires you to set dp[source] = 1