Skip to content

Commit 96403db

Browse files
author
Anurag
committed
Related to TheAlgorithms#5164 AllPathsFromSourceToTarget class cleaned up
1 parent 1e8abf1 commit 96403db

File tree

1 file changed

+20
-57
lines changed

1 file changed

+20
-57
lines changed

src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java

+20-57
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,61 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

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-
*/
126
public class AllPathsFromSourceToTarget {
137

14-
// No. of vertices in graph
158
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<>();
2010
private ArrayList<Integer>[] adjList;
2111

22-
// Constructor
2312
public AllPathsFromSourceToTarget(int vertices) {
24-
25-
// initialise vertex count
2613
this.v = vertices;
27-
28-
// initialise adjacency list
2914
initAdjList();
3015
}
3116

32-
// utility method to initialise adjacency list
17+
@SuppressWarnings("unchecked")
3318
private void initAdjList() {
3419
adjList = new ArrayList[v];
35-
3620
for (int i = 0; i < v; i++) {
3721
adjList[i] = new ArrayList<>();
3822
}
3923
}
4024

41-
// add edge from u to v
4225
public void addEdge(int u, int v) {
43-
// Add v to u's list.
4426
adjList[u].add(v);
4527
}
4628

47-
public void storeAllPaths(int s, int d) {
29+
public void findAllPaths(int s, int d) {
4830
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);
5534
}
5635

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));
6439
return;
6540
}
6641

67-
// Mark the current node
6842
isVisited[u] = true;
6943

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);
8049
}
8150
}
8251

83-
// Mark the current node
8452
isVisited[u] = false;
8553
}
8654

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]);
9459
}
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;
9962
}
10063
}

0 commit comments

Comments
 (0)