1- /** Author : Siddhant Swarup Mallick
1+ /**
2+ * Author : Siddhant Swarup Mallick
23 * Github : https://github.com/siddhant2002
34 */
45
@@ -15,13 +16,12 @@ public class AllPathsFromSourceToTarget {
1516 private int v ;
1617
1718 // To store the paths from source to destination
18- static List <List <Integer >> nm = new ArrayList <>();
19+ static List <List <Integer >> nm = new ArrayList <>();
1920 // adjacency list
2021 private ArrayList <Integer >[] adjList ;
2122
2223 // Constructor
23- public AllPathsFromSourceToTarget (int vertices )
24- {
24+ public AllPathsFromSourceToTarget (int vertices ) {
2525
2626 // initialise vertex count
2727 this .v = vertices ;
@@ -31,8 +31,7 @@ public AllPathsFromSourceToTarget(int vertices)
3131 }
3232
3333 // utility method to initialise adjacency list
34- private void initAdjList ()
35- {
34+ private void initAdjList () {
3635 adjList = new ArrayList [v ];
3736
3837 for (int i = 0 ; i < v ; i ++) {
@@ -41,15 +40,12 @@ private void initAdjList()
4140 }
4241
4342 // add edge from u to v
44- public void addEdge (int u , int v )
45- {
43+ public void addEdge (int u , int v ) {
4644 // Add v to u's list.
4745 adjList [u ].add (v );
4846 }
4947
50-
51- public void storeAllPaths (int s , int d )
52- {
48+ public void storeAllPaths (int s , int d ) {
5349 boolean [] isVisited = new boolean [v ];
5450 ArrayList <Integer > pathList = new ArrayList <>();
5551
@@ -61,9 +57,9 @@ public void storeAllPaths(int s, int d)
6157
6258 // A recursive function to print all paths from 'u' to 'd'.
6359 // 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 ) {
6763
6864 if (u .equals (d )) {
6965 nm .add (new ArrayList <>(localPathList ));
@@ -74,7 +70,7 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
7470 isVisited [u ] = true ;
7571
7672 // Recursion for all the vertices adjacent to current vertex
77-
73+
7874 for (Integer i : adjList [u ]) {
7975 if (!isVisited [i ]) {
8076 // store current node in path[]
@@ -91,12 +87,11 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
9187 }
9288
9389 // 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 ) {
9692 // Create a sample graph
9793 AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget (vertices );
98- for (int i =0 ; i <a .length ; i ++)
99- {
94+ for (int i = 0 ; i < a .length ; i ++) {
10095 g .addEdge (a [i ][0 ], a [i ][1 ]);
10196 // edges are added
10297 }
0 commit comments