Have a DAG and we want that for each node v find the number of vertex that existence a path from v to that vertexes
for n <= 1e5
# | User | Rating |
---|---|---|
1 | jiangly | 4039 |
2 | tourist | 3841 |
3 | jqdai0815 | 3682 |
4 | ksun48 | 3590 |
5 | ecnerwala | 3542 |
6 | Benq | 3535 |
7 | orzdevinwang | 3526 |
8 | gamegame | 3477 |
9 | heuristica | 3357 |
10 | Radewoosh | 3355 |
# | User | Contrib. |
---|---|---|
1 | cry | 168 |
2 | -is-this-fft- | 165 |
3 | atcoder_official | 160 |
3 | Um_nik | 160 |
5 | djm03178 | 157 |
6 | Dominater069 | 156 |
7 | adamant | 153 |
8 | luogu_official | 152 |
9 | awoo | 151 |
10 | TheScrasse | 147 |
Have a DAG and we want that for each node v find the number of vertex that existence a path from v to that vertexes
for n <= 1e5
Name |
---|
I think simple traversing can helpful.
That is Good but the order is O(n^2);
Not really because you are using the adjacency-list representation of the graph.
Actually the answer is just to run DFS/BFS on that node and see how many nodes are visited in the process.
Complexity O(E + V)
That not for each vertex
Then you can find the condensation graph into SCC and run DFS to find the answer recursively. Complexity doesn't change
I have modified the code to make it run in O(n)
Can we really compute the number of vertices that can be reached from vertex v with this algorithm?
I think this graph is a counterexample.
(UPD: Sorry, the number with the red color font actually should be 6)
You can use a bit-parallel strategy.
Assuming bit operations for one word is O(1), and let |word| the word-length of your machine, and then the time complexity would be O(NM/|word|). The memory complexity to store the reachability can be O(N*|word|) if you only care about the reachability from |word| vertices at the same time.
In other words, in today's 64-bit environment, it's nearly 64x faster than the naive algorithm to check the reachability from each vertex to all vertices one by one.