1
+ /** Author : Siddhant Swarup Mallick
2
+ * Github : https://github.com/siddhant2002
3
+ */
4
+
5
+ /** Program description - To find all possible paths from source to destination*/
6
+
7
+
8
+ package main .java .com .thealgorithms .backtracking ;
9
+
10
+ // JAVA program to print all paths from a source to destination.
11
+ import java .util .*;
12
+
13
+ // A directed graph using adjacency list representation
14
+
15
+
16
+ public class All_Paths_From_Source_To_Target {
17
+
18
+ // No. of vertices in graph
19
+ private int v ;
20
+
21
+ // adjacency list
22
+ private ArrayList <Integer >[] adjList ;
23
+
24
+ // Constructor
25
+ public All_Paths_From_Source_To_Target (int vertices )
26
+ {
27
+
28
+ // initialise vertex count
29
+ this .v = vertices ;
30
+
31
+ // initialise adjacency list
32
+ initAdjList ();
33
+ }
34
+
35
+ // utility method to initialise adjacency list
36
+
37
+ @ SuppressWarnings ("unchecked" )
38
+ private void initAdjList ()
39
+ {
40
+ adjList = new ArrayList [v ];
41
+
42
+ for (int i = 0 ; i < v ; i ++) {
43
+ adjList [i ] = new ArrayList <>();
44
+ }
45
+ }
46
+
47
+ // add edge from u to v
48
+ public void addEdge (int u , int v )
49
+ {
50
+ // Add v to u's list.
51
+ adjList [u ].add (v );
52
+ }
53
+
54
+ // Prints all paths from 's' to 'd'
55
+
56
+ public void printAllPaths (int s , int d , int c )
57
+ {
58
+ boolean [] isVisited = new boolean [v ];
59
+ ArrayList <Integer > pathList = new ArrayList <>();
60
+
61
+ // add source to path[]
62
+ pathList .add (s );
63
+
64
+ // Call recursive utility
65
+ printAllPathsUtil (s , d , isVisited , pathList );
66
+ return a [0 ];
67
+ }
68
+
69
+ // A recursive function to print all paths from 'u' to 'd'.
70
+ // isVisited[] keeps track of vertices in current path.
71
+ // localPathList<> stores actual vertices in the current path
72
+ private int printAllPathsUtil (Integer u , Integer d , boolean [] isVisited , List <Integer > localPathList , int a [])
73
+ {
74
+
75
+ if (u .equals (d )) {
76
+ System .out .println (localPathList );
77
+ a [0 ]++;
78
+ // if match found then no need to traverse more till depth
79
+ return a [0 ];
80
+ }
81
+
82
+ // Mark the current node
83
+ isVisited [u ] = true ;
84
+
85
+ // Recursion for all the vertices adjacent to current vertex
86
+
87
+ for (Integer i : adjList [u ]) {
88
+ if (!isVisited [i ]) {
89
+ // store current node in path[]
90
+ localPathList .add (i );
91
+ printAllPathsUtil (i , d , isVisited , localPathList );
92
+
93
+ // remove current node in path[]
94
+ localPathList .remove (i );
95
+ }
96
+ }
97
+
98
+ // Mark the current node
99
+ isVisited [u ] = false ;
100
+ return a [0 ];
101
+ // returns number of paths from source to destination
102
+ }
103
+
104
+ // Driver program
105
+ public static boolean all_Paths_From_Source_To_Target (int vertices , int a [][], int source , int destination , int num_of_paths )
106
+ {
107
+ // Create a sample graph
108
+ All_Paths_From_Source_To_Target g = new All_Paths_From_Source_To_Target (vertices );
109
+ for (int i =0 ; i <a .length ; i ++)
110
+ {
111
+ g .addEdge (a [i ][0 ], a [i ][1 ]);
112
+ // edges are added
113
+ }
114
+ System .out .println ("Following are all different paths from " + source + " to " + destination );
115
+ int c = g .printAllPaths (source , destination );
116
+ // method call to find number of paths
117
+ return c == num_of_paths ;
118
+ // returns true if number of paths calculated from source to destination matches with given paths
119
+ }
120
+ }
0 commit comments