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)) ` |

*chevron_right*

*filter_none*

**Output:**

Total number of walks: 2

**Time Complexity:**

## Recommended Posts:

- Count all possible walks from a source to a destination with exactly k edges
- Print all paths from a given source to a destination
- Print all paths from a given source to a destination using BFS
- Minimum edges to reverse to make path from a source to a destination
- Minimum cost path from source node to destination node via an intermediate node
- Mathematics | Walks, Trails, Paths, Cycles and Circuits in Graph
- Minimum steps to reach a destination
- Contributing to Open Source : Getting Started
- Find if there is a path of more than k length from a source
- Find the minimum cost to reach destination using a train
- Open Source, Freeware and Shareware Softwares
- Multi Source Shortest Path in Unweighted Graph
- Making your first Open Source Pull Request | Github
- Level of Each node in a Tree from source node (using BFS)
- Puzzle | Number of Sheets to be turned so that Prime Number has a Vowel on the other side

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.