-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintAllPath.java
53 lines (41 loc) · 1.2 KB
/
PrintAllPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package Graph;
import java.util.ArrayList;
public class PrintAllPath {
static class edge{
int src;
int dest;
public edge(int src,int dest){
this.src=src;
this.dest=dest;
}
}
public static void createGraph(ArrayList<edge>[] graph){
for(int i=0;i< graph.length;i++){
graph[i]=new ArrayList<>();
}
graph[0].add(new edge(0,3));
graph[2].add(new edge(2,3));
graph[3].add(new edge(3,1));
graph[4].add(new edge(4,0));
graph[4].add(new edge(4,1));
graph[5].add(new edge(5,0));
graph[5].add(new edge(5,2));
}
public static void findAllPath(ArrayList<edge>[] graph,int src,int dest,String path){
if(src==dest){
System.out.println(path+dest);
return;
}
for(int i=0;i<graph[src].size();i++){
edge e= graph[src].get(i);
findAllPath(graph,e.dest,dest,path+src);
}
}
public static void main(String[] args) {
int v=6;
ArrayList<edge>[] graph= new ArrayList[v];
createGraph(graph);
int src=5,dest=1;
findAllPath(graph,src,dest,"");
}
}