|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * Program description - To find all possible paths from source to destination |
8 |
| - * <a href="https://en.wikipedia.org/wiki/Shortest_path_problem">Wikipedia</a> |
9 |
| - * |
10 |
| - * @author <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a> |
11 |
| - */ |
12 | 6 | public class AllPathsFromSourceToTarget {
|
13 | 7 |
|
14 |
| - // No. of vertices in graph |
15 | 8 | private final int v;
|
16 |
| - |
17 |
| - // To store the paths from source to destination |
18 |
| - static List<List<Integer>> nm = new ArrayList<>(); |
19 |
| - // adjacency list |
| 9 | + private final List<List<Integer>> allPaths = new ArrayList<>(); |
20 | 10 | private ArrayList<Integer>[] adjList;
|
21 | 11 |
|
22 |
| - // Constructor |
23 | 12 | public AllPathsFromSourceToTarget(int vertices) {
|
24 |
| - |
25 |
| - // initialise vertex count |
26 | 13 | this.v = vertices;
|
27 |
| - |
28 |
| - // initialise adjacency list |
29 | 14 | initAdjList();
|
30 | 15 | }
|
31 | 16 |
|
32 |
| - // utility method to initialise adjacency list |
| 17 | + @SuppressWarnings("unchecked") |
33 | 18 | private void initAdjList() {
|
34 | 19 | adjList = new ArrayList[v];
|
35 |
| - |
36 | 20 | for (int i = 0; i < v; i++) {
|
37 | 21 | adjList[i] = new ArrayList<>();
|
38 | 22 | }
|
39 | 23 | }
|
40 | 24 |
|
41 |
| - // add edge from u to v |
42 | 25 | public void addEdge(int u, int v) {
|
43 |
| - // Add v to u's list. |
44 | 26 | adjList[u].add(v);
|
45 | 27 | }
|
46 | 28 |
|
47 |
| - public void storeAllPaths(int s, int d) { |
| 29 | + public void findAllPaths(int s, int d) { |
48 | 30 | boolean[] isVisited = new boolean[v];
|
49 |
| - ArrayList<Integer> pathList = new ArrayList<>(); |
50 |
| - |
51 |
| - // add source to path[] |
52 |
| - pathList.add(s); |
53 |
| - // Call recursive utility |
54 |
| - storeAllPathsUtil(s, d, isVisited, pathList); |
| 31 | + List<Integer> path = new ArrayList<>(); |
| 32 | + path.add(s); |
| 33 | + findAllPathsUtil(s, d, isVisited, path); |
55 | 34 | }
|
56 | 35 |
|
57 |
| - // A recursive function to print all paths from 'u' to 'd'. |
58 |
| - // isVisited[] keeps track of vertices in current path. |
59 |
| - // localPathList<> stores actual vertices in the current path |
60 |
| - private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList) { |
61 |
| - |
62 |
| - if (u.equals(d)) { |
63 |
| - nm.add(new ArrayList<>(localPathList)); |
| 36 | + private void findAllPathsUtil(int u, int d, boolean[] isVisited, List<Integer> currentPath) { |
| 37 | + if (u == d) { |
| 38 | + allPaths.add(new ArrayList<>(currentPath)); |
64 | 39 | return;
|
65 | 40 | }
|
66 | 41 |
|
67 |
| - // Mark the current node |
68 | 42 | isVisited[u] = true;
|
69 | 43 |
|
70 |
| - // Recursion for all the vertices adjacent to current vertex |
71 |
| - |
72 |
| - for (Integer i : adjList[u]) { |
73 |
| - if (!isVisited[i]) { |
74 |
| - // store current node in path[] |
75 |
| - localPathList.add(i); |
76 |
| - storeAllPathsUtil(i, d, isVisited, localPathList); |
77 |
| - |
78 |
| - // remove current node in path[] |
79 |
| - localPathList.remove(i); |
| 44 | + for (Integer neighbor : adjList[u]) { |
| 45 | + if (!isVisited[neighbor]) { |
| 46 | + currentPath.add(neighbor); |
| 47 | + findAllPathsUtil(neighbor, d, isVisited, currentPath); |
| 48 | + currentPath.remove(neighbor); |
80 | 49 | }
|
81 | 50 | }
|
82 | 51 |
|
83 |
| - // Mark the current node |
84 | 52 | isVisited[u] = false;
|
85 | 53 | }
|
86 | 54 |
|
87 |
| - // Driver program |
88 |
| - public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) { |
89 |
| - // Create a sample graph |
90 |
| - AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices); |
91 |
| - for (int[] i : a) { |
92 |
| - g.addEdge(i[0], i[1]); |
93 |
| - // edges are added |
| 55 | + public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] edges, int source, int destination) { |
| 56 | + AllPathsFromSourceToTarget graph = new AllPathsFromSourceToTarget(vertices); |
| 57 | + for (int[] edge : edges) { |
| 58 | + graph.addEdge(edge[0], edge[1]); |
94 | 59 | }
|
95 |
| - g.storeAllPaths(source, destination); |
96 |
| - // method call to store all possible paths |
97 |
| - return nm; |
98 |
| - // returns all possible paths from source to destination |
| 60 | + graph.findAllPaths(source, destination); |
| 61 | + return graph.allPaths; |
99 | 62 | }
|
100 | 63 | }
|
0 commit comments