- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Number of Paths of Length K in a Directed Graph
You are given a directed and unweighted graph G and an integer K. You have to find the number of paths in the graph of length K. Here the graph is in the form of an adjacency matrix. From vertex i to j, if there exists an edge, it is denoted by G[i][j]=1 else denoted by G[i][j]=0.
Input
A directed and unweighted graph represented by an adjacency matrix
Integer K that denotes the length of path to be found
Output
Total number of paths of length K
Case I K=3
Output
Total paths= 2
Explanation
In the above graph, there are 2 paths with a length of 3. They are-
0->1->2->3
0->2->3->1
Case II K=2
Output
Total paths = 6
Explanation
In the above graph, there are 2 paths with a length of 3. They are-
0->1->2
0->2->3
1->2->3
2->3->4
3->4->1
4->1->2
Example
import java.util.*; public class Main { public static void main(String[] args) { int[][] G = { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}, {0, 1, 0, 0} }; int K = 3; List<List<Integer>> paths = fpath(G, K); System.out.println("Paths of length are"); int count=0; for (List<Integer> path : paths) { count++; System.out.println(path); } System.out.println("Total number of paths "+count); } public static List<List<Integer>> fpath(int[][] G, int K) { List<List<Integer>> paths = new ArrayList<>(); List<Integer> path = new ArrayList<>(); boolean[] visited = new boolean[G.length]; // Start the search from each vertex for (int i = 0; i < G.length; i++) { dfs(G, i, K, path, visited, paths); } return paths; } private static void dfs(int[][] G, int vertex, int K, List<Integer> path, boolean[] visited, List<List<Integer>> paths) { visited[vertex] = true; path.add(vertex); if (K == 0) { paths.add(new ArrayList<>(path)); } else { for (int n = 0; n < G.length; n++) { if (G[vertex][n] == 1 && !visited[n]) { dfs(G, n, K - 1, path, visited, paths); } } } visited[vertex] = false; path.remove(path.size() - 1); } }
Output
Paths of length are [0, 1, 2, 3] [0, 2, 3, 1] Total number of paths 2
Explanation
To find paths with lengths equal to K, we will be using DFS (Depth First Search) approach. There are two helper functions- fpath() and dfs().
The fpath() Function
In the fpath() function, the adjacency matrix G and K are passed as parameters. It returns a list containing lists as paths. Also, the total number of paths of length K is printed.
A list of lists containing paths is initialized and the current path list is also initialized. A boolean array is created to mark the visited vertices. The dfs() method is called for each vertex and checks if it contains a path of length K in it.
The dfs() Function
The dfs() function is used to perform the Depth First Search operation. It is a recursive function. We will be passing the graph, current vertex, remaining length K, path visited, a boolean array visited that tracks the visited vertices, and the ‘paths’ which is the list of lists containing paths to store the path.
In the given dfs() function, we will mark the vertex as visited and added to the path visited. Now the neighboring vertices are visited. If the neighboring vertex is not visited, then the dfs() function is called recursively with the new vertex and the remaining length K-1.
If the value of K=0, which is our base case, the current visited path is added to ‘paths’. Else, the function is called recursively if there exists a path from the vertex to the neighbor and if the neighbor is not visited.
After all the neighbors are visited, the vertex is marked as not visited and removed from the current path visited list. This is also called backtracking.