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:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count all possible walks from a source to a destination with exactly k edges
- Shortest Path with even number of Edges from Source to Destination
- Count the number of walks of length N where cost of each walk is equal to a given number
- Print all paths from a given source to a destination
- Minimum edges to reverse to make path from a source to a destination
- Print all paths from a given source to a destination using BFS
- Minimum cost path from source node to destination node via an intermediate node
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Sum of shortest distance on source to destination and back having at least a common vertex
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Count total ways to reach destination from source in an undirected Graph
- Print all shortest paths between given source and destination in an undirected graph
- Mathematics | Walks, Trails, Paths, Cycles and Circuits in Graph
- Find the minimum cost to reach destination using a train
- Minimum steps to reach a destination
- Shortest paths from all vertices to a destination
- Minimum possible modifications in the matrix to reach destination
- Find if there is a path of more than k length from a source
- Contributing to Open Source : Getting Started
- Making your first Open Source Pull Request | Github

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.