Блог пользователя repeating

Автор repeating, история, 7 лет назад, По-английски

Hello everyone

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

any advice !! :D

  • Проголосовать: нравится
  • +27
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +69 Проголосовать: не нравится

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