I have a question:Does it require dynamically generating the answer,can't we do it when we need to check the top N history?
If we do it when we need the answer, it'll be quite a different algorithm!
1、we have a two-dimensional array to represent the adjacent between two vertexes;
also each vertex have a adjacent list of vertexes whose id are larger than the vertex itself;
2、For every vertex list,we check its adjacent vertex pair,and find the reasonable pairs through the 2-dim array;
Time complexity is min(O(edge^2),O(n*d^2));(d represent the degree of the vertex),but I think my solution avoid the redundant computation. A little better than the first solution.
I think Yash's solution time complexity is also O(N*M).- xljiasjtu July 15, 2014
Like the below example(I saw somebody's example).