1
- /** Author : Siddhant Swarup Mallick
1
+ /**
2
+ * Author : Siddhant Swarup Mallick
2
3
* Github : https://github.com/siddhant2002
3
4
*/
4
5
@@ -15,13 +16,12 @@ public class AllPathsFromSourceToTarget {
15
16
private int v ;
16
17
17
18
// To store the paths from source to destination
18
- static List <List <Integer >> nm = new ArrayList <>();
19
+ static List <List <Integer >> nm = new ArrayList <>();
19
20
// adjacency list
20
21
private ArrayList <Integer >[] adjList ;
21
22
22
23
// Constructor
23
- public AllPathsFromSourceToTarget (int vertices )
24
- {
24
+ public AllPathsFromSourceToTarget (int vertices ) {
25
25
26
26
// initialise vertex count
27
27
this .v = vertices ;
@@ -31,8 +31,7 @@ public AllPathsFromSourceToTarget(int vertices)
31
31
}
32
32
33
33
// utility method to initialise adjacency list
34
- private void initAdjList ()
35
- {
34
+ private void initAdjList () {
36
35
adjList = new ArrayList [v ];
37
36
38
37
for (int i = 0 ; i < v ; i ++) {
@@ -41,15 +40,12 @@ private void initAdjList()
41
40
}
42
41
43
42
// add edge from u to v
44
- public void addEdge (int u , int v )
45
- {
43
+ public void addEdge (int u , int v ) {
46
44
// Add v to u's list.
47
45
adjList [u ].add (v );
48
46
}
49
47
50
-
51
- public void storeAllPaths (int s , int d )
52
- {
48
+ public void storeAllPaths (int s , int d ) {
53
49
boolean [] isVisited = new boolean [v ];
54
50
ArrayList <Integer > pathList = new ArrayList <>();
55
51
@@ -61,9 +57,9 @@ public void storeAllPaths(int s, int d)
61
57
62
58
// A recursive function to print all paths from 'u' to 'd'.
63
59
// isVisited[] keeps track of vertices in current path.
64
- // localPathList<> stores actual vertices in the current path
65
- private void storeAllPathsUtil (Integer u , Integer d , boolean [] isVisited , List < Integer > localPathList )
66
- {
60
+ // localPathList<> stores actual vertices in the current path
61
+ private void storeAllPathsUtil (
62
+ Integer u , Integer d , boolean [] isVisited , List < Integer > localPathList ) {
67
63
68
64
if (u .equals (d )) {
69
65
nm .add (new ArrayList <>(localPathList ));
@@ -74,7 +70,7 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
74
70
isVisited [u ] = true ;
75
71
76
72
// Recursion for all the vertices adjacent to current vertex
77
-
73
+
78
74
for (Integer i : adjList [u ]) {
79
75
if (!isVisited [i ]) {
80
76
// store current node in path[]
@@ -91,12 +87,11 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
91
87
}
92
88
93
89
// Driver program
94
- public static List <List <Integer >> allPathsFromSourceToTarget (int vertices , int [][] a , int source , int destination )
95
- {
90
+ public static List <List <Integer >> allPathsFromSourceToTarget (
91
+ int vertices , int [][] a , int source , int destination ) {
96
92
// Create a sample graph
97
93
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget (vertices );
98
- for (int i =0 ; i <a .length ; i ++)
99
- {
94
+ for (int i = 0 ; i < a .length ; i ++) {
100
95
g .addEdge (a [i ][0 ], a [i ][1 ]);
101
96
// edges are added
102
97
}
0 commit comments