# All vertex pairs connected with exactly k edges in a graph

Given a directed graph represented as an adjacency matrix and an integer ‘k’, the task is to find all the vertex pairs that are connected with exactly ‘k’ edges.

Also, find the number of ways in which the two vertices can be linked in exactly k edges.

**Examples :**

Input :k = 3 and graph : 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0Output :1 -> 4 in 1 way(s) 1 -> 5 in 1 way(s) 2 -> 1 in 1 way(s) 2 -> 3 in 1 way(s) 3 -> 2 in 1 way(s) 3 -> 4 in 1 way(s) 3 -> 5 in 1 way(s) 4 -> 3 in 1 way(s) 5 -> 1 in 1 way(s) 5 -> 3 in 1 way(s)Input :k = 2 and graph : 0 0 0 1 0 1 0 1 0Output :2 -> 2 in 1 way(s) 3 -> 1 in 1 way(s) 3 -> 3 in 1 way(s)

**Approach :**

- We will multiply the adjacency matrix with itself ‘k’ number of times.
- In the resultant matrix,
`res[i][j]`

will be the number of ways in which vertex ‘j’ can be reached from vertex ‘i’ covering exactly ‘k’ edges.

Below is the implementation of the above approach :

`// Java implementation of the approach ` `public` `class` `KPaths { ` ` ` ` ` `// Function to multiply two square matrices ` ` ` `static` `int` `[][] multiplyMatrices(` `int` `[][] arr1, ` `int` `[][] arr2) ` ` ` `{ ` ` ` `int` `order = arr1.length; ` ` ` `int` `[][] ans = ` `new` `int` `[order][order]; ` ` ` `for` `(` `int` `i = ` `0` `; i < order; i++) { ` ` ` `for` `(` `int` `j = ` `0` `; j < order; j++) { ` ` ` `for` `(` `int` `k = ` `0` `; k < order; k++) { ` ` ` `ans[i][j] += arr1[i][k] * arr2[k][j]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Function to find all the pairs that ` ` ` `// can be connected with exactly 'k' edges ` ` ` `static` `void` `solve(` `int` `[][] arr, ` `int` `k) ` ` ` `{ ` ` ` `int` `[][] res = ` `new` `int` `[arr.length][arr[` `0` `].length]; ` ` ` ` ` `// copying arr to res, ` ` ` `// which is the result for k=1 ` ` ` `for` `(` `int` `i = ` `0` `; i < res.length; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < res.length; j++) ` ` ` `res[i][j] = arr[i][j]; ` ` ` ` ` `// multiplying arr with itself ` ` ` `// the required number of times ` ` ` `for` `(` `int` `i = ` `2` `; i <= k; i++) ` ` ` `res = multiplyMatrices(res, arr); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < res.length; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < res.length; j++) ` ` ` ` ` `// if there is a path between 'i' ` ` ` `// and 'j' in exactly 'k' edges ` ` ` `if` `(res[i][j] > ` `0` `) ` ` ` `System.out.println(i + ` `" -> "` `+ j + ` ` ` `" in "` `+ res[i][j] + ` `" way(s)"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[][] arr = ` `new` `int` `[` `5` `][` `5` `]; ` ` ` `arr[` `0` `][` `1` `] = ` `1` `; ` ` ` `arr[` `1` `][` `2` `] = ` `1` `; ` ` ` `arr[` `2` `][` `3` `] = ` `1` `; ` ` ` `arr[` `2` `][` `4` `] = ` `1` `; ` ` ` `arr[` `3` `][` `0` `] = ` `1` `; ` ` ` `arr[` `4` `][` `2` `] = ` `1` `; ` ` ` `int` `k = ` `3` `; ` ` ` `solve(arr, k); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

0 -> 3 in 1 way(s) 0 -> 4 in 1 way(s) 1 -> 0 in 1 way(s) 1 -> 2 in 1 way(s) 2 -> 1 in 1 way(s) 2 -> 3 in 1 way(s) 2 -> 4 in 1 way(s) 3 -> 2 in 1 way(s) 4 -> 0 in 1 way(s) 4 -> 2 in 1 way(s)

## Recommended Posts:

- Maximum number of edges among all connected components of an undirected graph
- Find the Degree of a Particular vertex in a Graph
- Find a Mother Vertex in a Graph
- Connected Components in an undirected graph
- k'th heaviest adjacent node in a graph where each vertex has weight
- Topological Sort of a graph using departure time of vertex
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Cycles of length n in an undirected and connected graph
- Clone an undirected graph with multiple connected components
- Sum of the minimum elements in all connected components of an undirected graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Finding minimum vertex cover size of a graph using binary search
- Count number of edges in an undirected graph
- Minimum number of edges between two vertices of a Graph
- Minimum number of edges between two vertices of a graph using DFS

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.