Skip to content

Commit 400c8a2

Browse files
committed
Cleanup class AllPathsFromSourceToTarget
1 parent 843085a commit 400c8a2

File tree

1 file changed

+64
-23
lines changed

1 file changed

+64
-23
lines changed

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

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33
* Github : https://github.com/siddhant2002
44
*/
55

6-
/** Program description - To find all possible paths from source to destination*/
7-
8-
/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
6+
/** Program description - To find all possible paths from source to destination */
7+
/** Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
98
package com.thealgorithms.backtracking;
109

1110
import java.util.ArrayList;
1211
import java.util.List;
1312

13+
1414
public class AllPathsFromSourceToTarget {
1515

16-
// No. of vertices in graph
16+
/**
17+
* Common variables to "AllPathsFromSourceToTarget" class.
18+
*
19+
* 1) Number of vertices in graph.
20+
* 2) List to store the paths from source to destiny.
21+
* 3) Adjacency list.
22+
*/
1723
private final int v;
18-
19-
// To store the paths from source to destination
2024
static List<List<Integer>> nm = new ArrayList<>();
21-
// adjacency list
2225
private ArrayList<Integer>[] adjList;
2326

24-
// Constructor
27+
/**
28+
* Constructor.
29+
* @param vertices Number of vertices.
30+
*/
31+
2532
public AllPathsFromSourceToTarget(int vertices) {
2633

2734
// initialise vertex count
@@ -31,7 +38,10 @@ public AllPathsFromSourceToTarget(int vertices) {
3138
initAdjList();
3239
}
3340

34-
// utility method to initialise adjacency list
41+
/**
42+
* Creates an ArrayList for each position in adjList.
43+
*/
44+
3545
private void initAdjList() {
3646
adjList = new ArrayList[v];
3747

@@ -40,37 +50,56 @@ private void initAdjList() {
4050
}
4151
}
4252

43-
// add edge from u to v
53+
/**
54+
* Add edge from u to v
55+
*
56+
* @param u Value to select list.
57+
* @param v Value to introduce.
58+
*/
59+
4460
public void addEdge(int u, int v) {
45-
// Add v to u's list.
4661
adjList[u].add(v);
4762
}
4863

64+
/**
65+
*
66+
* @param s Value of source
67+
* @param d Value of destiny.
68+
*/
69+
4970
public void storeAllPaths(int s, int d) {
71+
// Array composed by boolean´s objects with v like lenght
5072
boolean[] isVisited = new boolean[v];
73+
// This ArrayList stores the track.
5174
ArrayList<Integer> pathList = new ArrayList<>();
52-
5375
// add source to path[]
5476
pathList.add(s);
55-
// Call recursive utility
77+
// Call recursive function utility.
5678
storeAllPathsUtil(s, d, isVisited, pathList);
5779
}
5880

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
81+
/**
82+
*
83+
* Function to StoreAllPaths
84+
*
85+
* @param u Value of source
86+
* @param d Value of destiny
87+
* @param isVisited array to keeps track of vertices in current path.
88+
* @param localPathList stores actual vertices in the current path
89+
*/
90+
6291
private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList) {
6392

93+
//If node u is equals to d the path has benn completly found it.
6494
if (u.equals(d)) {
6595
nm.add(new ArrayList<>(localPathList));
6696
return;
6797
}
6898

69-
// Mark the current node
99+
// Mark the current node as visited.
70100
isVisited[u] = true;
71101

72102
// Recursion for all the vertices adjacent to current vertex
73-
74103
for (Integer i : adjList[u]) {
75104
if (!isVisited[i]) {
76105
// store current node in path[]
@@ -82,21 +111,33 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
82111
}
83112
}
84113

85-
// Mark the current node
114+
// Mark the current node as false to release it and be used by others.
86115
isVisited[u] = false;
87116
}
88117

89-
// Driver program
118+
/**
119+
*
120+
* Driver program
121+
*
122+
* @param vertices number of vertices of the graph
123+
* @param a represents the aristas of the graph.
124+
* @param source value of source.
125+
* @param destination value of destiny.
126+
* @return A List composed by diferent lists that stores all the possible paths to the destiny.
127+
*/
128+
90129
public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) {
91130
// Create a sample graph
92131
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
132+
133+
// edges are added
93134
for (int[] i : a) {
94135
g.addEdge(i[0], i[1]);
95-
// edges are added
96136
}
97-
g.storeAllPaths(source, destination);
137+
98138
// method call to store all possible paths
139+
g.storeAllPaths(source, destination);
140+
99141
return nm;
100-
// returns all possible paths from source to destination
101142
}
102143
}

0 commit comments

Comments
 (0)