import
java.io.*;
import
java.util.*;
class
GFG {
static
List<List<Integer> > adjList;
static
boolean
[] inDeg0;
static
boolean
[] outDeg0;
static
void
dfs(
int
s, List<Integer> localPath,
boolean
[] localVisited)
{
localPath.add(s);
localVisited[s] =
true
;
if
(outDeg0[s] && inDeg0[localPath.get(
0
)]) {
for
(
int
i =
0
; i < localPath.size(); i++) {
System.out.print(localPath.get(i) +
" "
);
}
System.out.println();
}
for
(
int
v : adjList.get(s)) {
if
(!localVisited[v]) {
dfs(v, localPath, localVisited);
}
}
localPath.remove(localPath.size() -
1
);
localVisited[s] =
false
;
}
static
void
print_all_paths(
int
n)
{
for
(
int
i =
0
; i < n; i++) {
if
(inDeg0[i] && !adjList.get(i).isEmpty()) {
List<Integer> localPath =
new
ArrayList<>();
boolean
[] localVisited =
new
boolean
[n];
dfs(i, localPath, localVisited);
}
}
}
public
static
void
main(String[] args)
{
int
n =
6
;
adjList =
new
ArrayList<>();
inDeg0 =
new
boolean
[n];
outDeg0 =
new
boolean
[n];
for
(
int
i =
0
; i < n; i++) {
adjList.add(
new
ArrayList<>());
inDeg0[i] =
true
;
outDeg0[i] =
true
;
}
int
[][] edges = { {
5
,
0
}, {
5
,
2
}, {
2
,
3
},
{
4
,
0
}, {
4
,
1
}, {
3
,
1
} };
for
(
int
[] edge : edges) {
int
u = edge[
0
];
int
v = edge[
1
];
adjList.get(u).add(v);
inDeg0[v] =
false
;
outDeg0[u] =
false
;
}
System.out.println(
"All possible paths:"
);
print_all_paths(n);
}
}