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.
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 0 Output : 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 0 Output : 2 -> 2 in 1 way(s) 3 -> 1 in 1 way(s) 3 -> 3 in 1 way(s)
- 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 :
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)
The time complexity of the above code can be reduced for large values of k by using matrix exponentitation. The complexity can be changed from O(n^3 * k) to O(n^3 * log k)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Maximum number of edges among all connected components of an undirected graph
- Convert undirected connected graph to strongly connected directed graph
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Find a Mother Vertex in a Graph
- Find the Degree of a Particular vertex in a Graph
- Find a Mother vertex in a Graph using Bit Masking
- Find dependencies of each Vertex in a Directed Graph
- k'th heaviest adjacent node in a graph where each vertex has weight
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Topological Sort of a graph using departure time of vertex
- Add and Remove vertex in Adjacency List representation of Graph
- Connected Components in an undirected graph
- Check if a directed graph is connected or not
- Finding minimum vertex cover size of a graph using binary search
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Cycles of length n in an undirected and connected graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Largest subarray sum of all connected components in undirected graph
- Clone an undirected graph with multiple connected components
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.