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 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
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.