|
1 |
| -/** |
2 |
| - * Author : Siddhant Swarup Mallick |
3 |
| - * Github : https://github.com/siddhant2002 |
4 |
| - */ |
5 |
| - |
6 |
| -/** Program description - To find all possible paths from source to destination*/ |
7 |
| - |
8 |
| -/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */ |
9 | 1 | package com.thealgorithms.backtracking;
|
10 | 2 |
|
11 | 3 | import java.util.ArrayList;
|
12 | 4 | import java.util.List;
|
13 | 5 |
|
| 6 | +/** |
| 7 | + * To find all possible paths from source to destination. See |
| 8 | + * <a href="https://en.wikipedia.org/wiki/Shortest_path_problem">Wikipedia</a> |
| 9 | + * (Note: This algorithm finds all paths, not the shortest paths). |
| 10 | + */ |
14 | 11 | public class AllPathsFromSourceToTarget {
|
15 | 12 |
|
16 |
| - // No. of vertices in graph |
17 |
| - private final int v; |
18 |
| - |
19 |
| - // To store the paths from source to destination |
20 |
| - static List<List<Integer>> nm = new ArrayList<>(); |
21 |
| - // adjacency list |
22 |
| - private ArrayList<Integer>[] adjList; |
23 |
| - |
24 |
| - // Constructor |
25 |
| - public AllPathsFromSourceToTarget(int vertices) { |
| 13 | + private final int vertexCount; |
| 14 | + private final List<List<Integer>> allPaths; |
| 15 | + private final List<Integer>[] adjList; |
26 | 16 |
|
27 |
| - // initialise vertex count |
28 |
| - this.v = vertices; |
29 |
| - |
30 |
| - // initialise adjacency list |
31 |
| - initAdjList(); |
| 17 | + private AllPathsFromSourceToTarget(int vertices) { |
| 18 | + this.vertexCount = vertices; |
| 19 | + this.allPaths = new ArrayList<>(); |
| 20 | + this.adjList = new ArrayList[vertices]; |
| 21 | + initializeAdjacencyList(); |
32 | 22 | }
|
33 | 23 |
|
34 |
| - // utility method to initialise adjacency list |
35 |
| - private void initAdjList() { |
36 |
| - adjList = new ArrayList[v]; |
37 |
| - |
38 |
| - for (int i = 0; i < v; i++) { |
| 24 | + /** |
| 25 | + * Initializes the adjacency list to represent a graph with the specified number of vertices. |
| 26 | + */ |
| 27 | + private void initializeAdjacencyList() { |
| 28 | + for (int i = 0; i < vertexCount; i++) { |
39 | 29 | adjList[i] = new ArrayList<>();
|
40 | 30 | }
|
41 | 31 | }
|
42 | 32 |
|
43 |
| - // add edge from u to v |
44 |
| - public void addEdge(int u, int v) { |
45 |
| - // Add v to u's list. |
46 |
| - adjList[u].add(v); |
| 33 | + /** |
| 34 | + * Adds a directed edge from source vertex to destination vertex in the graph. |
| 35 | + * @param source The starting vertex of the edge. |
| 36 | + * @param destination The ending vertex of the edge. |
| 37 | + */ |
| 38 | + private void addEdge(int source, int destination) { |
| 39 | + adjList[source].add(destination); |
47 | 40 | }
|
48 | 41 |
|
49 |
| - public void storeAllPaths(int s, int d) { |
50 |
| - boolean[] isVisited = new boolean[v]; |
51 |
| - ArrayList<Integer> pathList = new ArrayList<>(); |
52 |
| - |
53 |
| - // add source to path[] |
54 |
| - pathList.add(s); |
55 |
| - // Call recursive utility |
56 |
| - storeAllPathsUtil(s, d, isVisited, pathList); |
| 42 | + /** |
| 43 | + * Finds all possible paths from a given source vertex to a destination vertex in the graph. |
| 44 | + * This method uses a backtracking approach to explore all possible paths. |
| 45 | + * @param source The starting vertex of the path search. |
| 46 | + * @param destination The ending vertex of the path search. |
| 47 | + * @return A list containing all the found paths represented as lists of vertex indices. |
| 48 | + */ |
| 49 | + private List<List<Integer>> findAllPaths(int source, int destination) { |
| 50 | + boolean[] visited = new boolean[vertexCount]; |
| 51 | + List<Integer> currentPath = new ArrayList<>(); |
| 52 | + currentPath.add(source); |
| 53 | + findAllPathsUtil(source, destination, visited, currentPath); |
| 54 | + return allPaths; |
57 | 55 | }
|
58 | 56 |
|
59 |
| - // A recursive function to print all paths from 'u' to 'd'. |
60 |
| - // isVisited[] keeps track of vertices in current path. |
61 |
| - // localPathList<> stores actual vertices in the current path |
62 |
| - private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList) { |
63 |
| - |
64 |
| - if (u.equals(d)) { |
65 |
| - nm.add(new ArrayList<>(localPathList)); |
| 57 | + /** |
| 58 | + * A recursive helper function that implements the backtracking logic to find all paths. |
| 59 | + * @param source The current vertex being explored in the path search. |
| 60 | + * @param destination The ending vertex of the path search. |
| 61 | + * @param visited A boolean array to keep track of visited vertices. |
| 62 | + * @param currentPath A list representing the current path being constructed. |
| 63 | + */ |
| 64 | + private void findAllPathsUtil(int source, int destination, boolean[] visited, List<Integer> currentPath) { |
| 65 | + if (source == destination) { |
| 66 | + allPaths.add(new ArrayList<>(currentPath)); |
66 | 67 | return;
|
67 | 68 | }
|
68 | 69 |
|
69 |
| - // Mark the current node |
70 |
| - isVisited[u] = true; |
| 70 | + visited[source] = true; |
71 | 71 |
|
72 |
| - // Recursion for all the vertices adjacent to current vertex |
73 |
| - |
74 |
| - for (Integer i : adjList[u]) { |
75 |
| - if (!isVisited[i]) { |
76 |
| - // store current node in path[] |
77 |
| - localPathList.add(i); |
78 |
| - storeAllPathsUtil(i, d, isVisited, localPathList); |
79 |
| - |
80 |
| - // remove current node in path[] |
81 |
| - localPathList.remove(i); |
| 72 | + for (int neighbor : adjList[source]) { |
| 73 | + if (!visited[neighbor]) { |
| 74 | + currentPath.add(neighbor); |
| 75 | + findAllPathsUtil(neighbor, destination, visited, currentPath); |
| 76 | + currentPath.removeLast(); |
82 | 77 | }
|
83 | 78 | }
|
84 | 79 |
|
85 |
| - // Mark the current node |
86 |
| - isVisited[u] = false; |
| 80 | + visited[source] = false; |
87 | 81 | }
|
88 | 82 |
|
89 |
| - // Driver program |
90 |
| - public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) { |
91 |
| - // Create a sample graph |
| 83 | + |
| 84 | + /** |
| 85 | + * Finds all possible paths from a given source vertex to a destination vertex in the graph. |
| 86 | + * This method uses a backtracking approach to explore all possible paths. |
| 87 | + * The found paths are stored in the `allPaths` member variable. |
| 88 | + * |
| 89 | + * @param source The starting vertex of the path search. |
| 90 | + * @param destination The ending vertex of the path search. |
| 91 | + * @return A list containing all the found paths represented as lists of vertex indices. |
| 92 | + */ |
| 93 | + public static List<List<Integer>> findAllPaths(int vertices, int[][] a, int source, int destination) { |
92 | 94 | AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
|
93 | 95 | for (int[] i : a) {
|
94 | 96 | g.addEdge(i[0], i[1]);
|
95 |
| - // edges are added |
96 | 97 | }
|
97 |
| - g.storeAllPaths(source, destination); |
98 |
| - // method call to store all possible paths |
99 |
| - return nm; |
100 |
| - // returns all possible paths from source to destination |
| 98 | + return g.findAllPaths(source, destination); |
101 | 99 | }
|
102 | 100 | }
|
0 commit comments