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 | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 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