Given a graph and two vertices src and dest, count the total number of paths from src to dest where the length of the path is k (there should be exactly k edges between them). Note that the graph is represented as an adjacency matrix.

For example, consider the following graph:

The number of paths from vertex 0 to vertex 3 with length 2 is 2 ({0->1->3} and {0->2->3}).

We have already discussed a O(V^{3} K) approach in count all possible walks from a source to a destination with exactly k edges. In this post, a O(V^{3} Log K) approach is discussed.

**Approach:** The idea is to compute a resultant matrix where . The total number of paths from source to destination of length k will then be simply . We use this technique to compute the exponentiation of the adjacency matrix of the given graph.

The recursion tree of power function used here for exponent = 7 looks like below:

Below is the implementation of the above approach:

`# Python3 code to count the number of paths ` `# from the source to the destination of length ` `# k in the given graph ` ` ` `# Function to multiply two matrices ` `def` `multiply(mat1, mat2): ` ` ` ` ` `result ` `=` `[[` `0` `for` `col ` `in` `range` `(` `len` `(mat2[` `0` `]))] \ ` ` ` `for` `row ` `in` `range` `(` `len` `(mat1))] ` ` ` ` ` `for` `row ` `in` `range` `(` `len` `(mat1)): ` ` ` ` ` `for` `col ` `in` `range` `(` `len` `(mat2[` `0` `])): ` ` ` ` ` `for` `k ` `in` `range` `(` `len` `(mat2)): ` ` ` ` ` `result[row][col] ` `+` `=` `mat1[row][k] ` `*` `mat2[k][col] ` ` ` ` ` `return` `result ` ` ` `# Function to calculate (matrix)^exp ` `def` `power(matrix, exp): ` ` ` ` ` `if` `exp ` `=` `=` `1` `: ` ` ` `return` `matrix ` ` ` ` ` `else` `: ` ` ` `tempMatrix ` `=` `power(matrix, exp` `/` `/` `2` `) ` ` ` `res ` `=` `multiply(tempMatrix, tempMatrix) ` ` ` ` ` `if` `exp ` `%` `2` `=` `=` `0` `: ` ` ` `return` `res ` ` ` ` ` `else` `: ` ` ` `return` `multiply(matrix, res) ` ` ` `# Function to calculate total number of paths from ` `# source to destination where length of path is k ` `def` `countWalks(graph, src, dest, k): ` ` ` ` ` `resultantMatrix ` `=` `power(graph, k) ` ` ` `return` `resultantMatrix[src][dest] ` ` ` `# Driver code ` `graph ` `=` `[[` `0` `, ` `1` `, ` `1` `, ` `1` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `1` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `1` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `0` `]] ` `src, dest, k ` `=` `0` `, ` `3` `, ` `2` `print` `(` `"Total number of walks:"` `, countWalks(graph, src, dest, k)) ` |

**Output:**

Total number of walks: 2

**Time Complexity:**

## Recommended Posts:

- Count all possible walks from a source to a destination with exactly k edges
- Number of paths from source to destination in a directed acyclic graph
- Print all paths from a given source to a destination using BFS
- Print all paths from a given source to a destination
- Check if a destination is reachable from source with two movements allowed
- Minimum edges to reverse to make path from a source to a destination
- 8085 program to move blocks of bits from source location to a destination location
- Minimum cost path from source node to destination node via an intermediate node
- Number of decisions to reach destination
- Count number of ways to reach destination in a Maze
- Count of possible hexagonal walks
- Mathematics | Walks, Trails, Paths, Cycles and Circuits in Graph
- Minimum steps to reach a destination
- Sudo Placement[1.3] | Final Destination
- Minimum Initial Points to Reach Destination

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.