knst's blog

By knst, 12 years ago, translation, In English

How to calculate number of topological sort?

Brute force not acceptable: number of vertex N is 10^3; number of edges M: 3 * 10^5. Time limit of calculation isn't critical: 1 hour is acceptable. Can you help me with this problem? I can't find solution.

Trivial example:
N = 6; M = 7
V: 1 2 3 4 5
E:
1 2
1 3
1 4 
1 5
2 4
2 5
3 5

Answer: 
5
(1 2 3 4 5)
(1 3 2 4 5)
(1 2 4 3 5)
(1 2 3 5 4)
(1 3 2 5 4)
  • Vote: I like it
  • +9
  • Vote: I do not like it

| Write comment?
»
12 years ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

It seems that counting the number of topological orderings of the graph is #P-complete. (article).

So I think that without any additional information about graph structure there is no chance to calculate an answer until the end of this era. All known algorithms are exponential, so they won't work fast on the dense graph of such great size (thousand of vertices!).

Anyway, in the article above there is a mention of algorithms that can possibly estimate the answer.

»
6 years ago, hide # |
Rev. 3  
Vote: I like it -8 Vote: I do not like it

Here is the code implementation using mask. certainly it will not help you but it works fine for smaller values.