Hello everyone
I'm wondering how to find the number of distinct shortest paths in an undirected unweighted graph !!
any advice !! :D
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3821 |
3 | Benq | 3736 |
4 | Radewoosh | 3631 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3388 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
Hello everyone
I'm wondering how to find the number of distinct shortest paths in an undirected unweighted graph !!
any advice !! :D
Name |
---|
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