Given n (no. of vertices) and edges in the undirected graph . For each pair of vertices(say 'u', 'v') find(no need to output these) the number of distinct paths connecting these vertices('u','v') and output the minimum of these.(2<=n<=1500) required O(n^2) solution.

My approach:

1) Make the adjacency matrix and call it A.

2) find matrix B=A+A^2+A^3+...+A^n.(Proof

3) B contains the answer and find minimum of its entries.

But I am unable to do step 2 efficiently .

What exactly do you mean by "distinct paths"? Are these paths required to be simple? If no, then for some graphs the answer is infinity and your matrix approach does not work (it may only yield finite answers). If yes, then your matrix approach still does not work as matrix exponentiation does not make all vertexes in the path different.

Maybe you want to find some kind of edge connectivity?

Yes,simple paths only, I think, Original problem is :Problem Is there any simpler method other than finding connectivity or some other simpler method?

That problem is asking about edge-connectivity exactly: what is the minimal number of edges to destroy so the graph becomes disjoint. It's more or less the same as asking for minimal cut. Wikipedia suggests Karger's algorithm and more general Stoer-Wagner algorithm. If tests are good, looks like the former won't pass, and the latter would with ease.

Your definition is looks like an application of Menger's theorem, if you add the word "disjoint" there (and if you don't, it's something different, not sure of "minimal number of paths" is still the same).

For the problem you are referring to I'd suggest reading about max-flow in graphs.

But as you already asked let's see how we can efficiently implement your step 2. The problem is to find sum

A^{1}+A^{2}+A^{3}+ ... +A^{N}forAbeing adjacency matrix. Now let's say thatN= 2k(If it's not we manually calculate the last power and add it to the rest). We can transform original sum intoA^{1}+A^{2}+A^{3}+ ... +A^{2k}= (A^{1}+A^{2}+ ... +A^{k}) + (A^{k + 1}+A^{k + 2}+ ... +A^{2k})and by transforming this into

(

A^{1}+A^{2}+ ... +A^{k}) + (A^{k + 1}+A^{k + 2}+ ... +A^{2k}) = (1 +A^{k})(A^{1}+A^{2}+ ... +A^{k})we halve the number of powers we need to calculate. We now just need to recursively calculate (

A^{1}+A^{2}+ ... +A^{k}) the same way we did the original sum. Complexity isO(N^{3}×log^{2}N). That's the fastest way I know if you are interested in paths from specific nodes. Not connected to your problem in any way but I think it's a pretty neat trick.yeah,but but that complexity wouldn't be good enough,that's why I asked it here.Thanks for that.