3
3
* Github : https://github.com/siddhant2002
4
4
*/
5
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 */
6
+ /** Program description - To find all possible paths from source to destination */
7
+ /** Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
9
8
package com .thealgorithms .backtracking ;
10
9
11
10
import java .util .ArrayList ;
12
11
import java .util .List ;
13
12
13
+
14
14
public class AllPathsFromSourceToTarget {
15
15
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
+ */
17
23
private final int v ;
18
-
19
- // To store the paths from source to destination
20
24
static List <List <Integer >> nm = new ArrayList <>();
21
- // adjacency list
22
25
private ArrayList <Integer >[] adjList ;
23
26
24
- // Constructor
27
+ /**
28
+ * Constructor.
29
+ * @param vertices Number of vertices.
30
+ */
31
+
25
32
public AllPathsFromSourceToTarget (int vertices ) {
26
33
27
34
// initialise vertex count
@@ -31,7 +38,10 @@ public AllPathsFromSourceToTarget(int vertices) {
31
38
initAdjList ();
32
39
}
33
40
34
- // utility method to initialise adjacency list
41
+ /**
42
+ * Creates an ArrayList for each position in adjList.
43
+ */
44
+
35
45
private void initAdjList () {
36
46
adjList = new ArrayList [v ];
37
47
@@ -40,37 +50,56 @@ private void initAdjList() {
40
50
}
41
51
}
42
52
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
+
44
60
public void addEdge (int u , int v ) {
45
- // Add v to u's list.
46
61
adjList [u ].add (v );
47
62
}
48
63
64
+ /**
65
+ *
66
+ * @param s Value of source
67
+ * @param d Value of destiny.
68
+ */
69
+
49
70
public void storeAllPaths (int s , int d ) {
71
+ // Array composed by boolean´s objects with v like lenght
50
72
boolean [] isVisited = new boolean [v ];
73
+ // This ArrayList stores the track.
51
74
ArrayList <Integer > pathList = new ArrayList <>();
52
-
53
75
// add source to path[]
54
76
pathList .add (s );
55
- // Call recursive utility
77
+ // Call recursive function utility.
56
78
storeAllPathsUtil (s , d , isVisited , pathList );
57
79
}
58
80
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
+
62
91
private void storeAllPathsUtil (Integer u , Integer d , boolean [] isVisited , List <Integer > localPathList ) {
63
92
93
+ //If node u is equals to d the path has benn completly found it.
64
94
if (u .equals (d )) {
65
95
nm .add (new ArrayList <>(localPathList ));
66
96
return ;
67
97
}
68
98
69
- // Mark the current node
99
+ // Mark the current node as visited.
70
100
isVisited [u ] = true ;
71
101
72
102
// Recursion for all the vertices adjacent to current vertex
73
-
74
103
for (Integer i : adjList [u ]) {
75
104
if (!isVisited [i ]) {
76
105
// store current node in path[]
@@ -82,21 +111,33 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
82
111
}
83
112
}
84
113
85
- // Mark the current node
114
+ // Mark the current node as false to release it and be used by others.
86
115
isVisited [u ] = false ;
87
116
}
88
117
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
+
90
129
public static List <List <Integer >> allPathsFromSourceToTarget (int vertices , int [][] a , int source , int destination ) {
91
130
// Create a sample graph
92
131
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget (vertices );
132
+
133
+ // edges are added
93
134
for (int [] i : a ) {
94
135
g .addEdge (i [0 ], i [1 ]);
95
- // edges are added
96
136
}
97
- g . storeAllPaths ( source , destination );
137
+
98
138
// method call to store all possible paths
139
+ g .storeAllPaths (source , destination );
140
+
99
141
return nm ;
100
- // returns all possible paths from source to destination
101
142
}
102
143
}
0 commit comments