Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the number of paths of given length in a undirected unweighted graph

'Length' of a path is the number of edges in the path.

Given a source and a destination vertex, I want to find the number of paths form the source vertex to the destination vertex of given length k.

  • We can visit each vertex as many times as we want, so if a path from a to b goes like this: a -> c -> b -> c -> b it is considered valid. This means there can be cycles and we can go through the destination more than once.

  • Two vertices can be connected by more than one edge. So if vertex a an vertex b are connected by two edges, then the paths , a -> b via edge 1 and a -> b via edge 2 are considered different.

  • Number of vertices N is <= 70, and K, the length of the path, is <= 10^9.

  • As the answer can be very large, it is to be reported modulo some number.

Here is what I have thought so far:

We can use breadth-first-search without marking any vertices as visited, at each iteration, we keep track of the number of edges 'n_e' we required for that path and product 'p' of the number of duplicate edges each edge in our path has.

The search search should terminate if the n_e is greater than k, if we ever reach the destination with n_eequal to k, we terminate the search and add p to out count of number of paths.

I think it we could use a depth-first-search instead of breadth first search, as we do not need the shortest path and the size of Q used in breadth first search might not be enough.

The second algorithm i have am thinking about, is something similar to Floyd Warshall's Algorithm using this approach . Only we dont need a shortest path, so i am not sure this is correct.

The problem I have with my first algorithm is that 'K' can be upto 1000000000 and that means my search will run until it has 10^9 edges and n_e the edge count will be incremented by just 1 at each level, which will be very slow, and I am not sure it will ever terminate for large inputs.

So I need a different approach to solve this problem; any help would be greatly appreciated.

like image 745
2147483647 Avatar asked Jan 11 '13 05:01

2147483647


People also ask

How do you find the number of paths in an undirected graph?

Given G=(V,E) obtain G′ from G by replacing each node by a clique of size N=nc where n=|V| and c≫1. For each simple path of length ℓ in G there are roughly (N!)ℓ paths in G′. So if G has an s−t Hamiltonian path, there will be at least (N!)

How many paths of length 4 are there from A to D?

Because there are exactly eight paths of length four from a to d. By inspecÅon of the graph, we see that a,b,a,b,d; a,b,a,c,d; a,b,d,b,d; a,b,d,c,d; a,c,a,b,d; a,c,a,c,d; a,c,d,b,d; and a, c, d, c, d are the eight paths of length four from a to d.

How many paths of length k are there in Kn?

In general, the number of directed k-vertex paths (k≥2) in Kn is n×(n−1)×⋯×(n−k+1), this is the number of sequences of length k without repeated entries. Thus the number of undirected k-vertex paths is 12n×(n−1)×⋯×(n−k+1).

Does Dijkstra work for unweighted graphs?

As i realised from the comments, Dijkstra's algorithm doesn't work for unweighted graphs.


1 Answers

So, here's a nifty graph theory trick that I remember for this one.

Make an adjacency matrix A. where A[i][j] is 1 if there is an edge between i and j, and 0 otherwise.

Then, the number of paths of length k between i and j is just the [i][j] entry of A^k.

So, to solve the problem, build A and construct A^k using matrix multiplication (the usual trick for doing exponentiation applies here). Then just look up the necessary entry.

EDIT: Well, you need to do the modular arithmetic inside the matrix multiplication to avoid overflow issues, but that's a much smaller detail.

like image 162
Dennis Meng Avatar answered Sep 30 '22 11:09

Dennis Meng