-
Notifications
You must be signed in to change notification settings - Fork 0
/
All_Paths_from_src_to_dest_DFS.java
56 lines (42 loc) · 1.38 KB
/
All_Paths_from_src_to_dest_DFS.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
54
55
56
package Graphs;
import java.util.ArrayList;
public class All_Paths_from_src_to_dest_DFS {
public static class Edge{
int w;
int src;
int dest;
public Edge(int w, int s, int d){
this.w = w;
this.src = s;
this.dest = d;
}
}
public static void createGraph(ArrayList<Edge>[] graph){
for (int i=0; i<graph.length; i++){
graph[i] = new ArrayList<>();
}
graph[0].add(new Edge(1, 0 ,3));
graph[2].add(new Edge(1, 2 ,3));
graph[3].add(new Edge(1, 3 ,1));
graph[4].add(new Edge(1, 4 ,0));
graph[4].add(new Edge(1, 4 ,1));
graph[5].add(new Edge(1, 5 ,0));
graph[5].add(new Edge(1, 5 ,2));
}
public static void printAllPaths(ArrayList<Edge>[] graph, int src, int dest, String path){
if (src == dest) {
System.out.print(path + " " + dest + "\n");
return;
}
for (int i=0; i<graph[src].size(); i++){
// no need of visited array
Edge e = graph[src].get(i);
printAllPaths(graph, e.dest, dest, path+ " " + src);
}
}
public static void main(String[] args) {
ArrayList<Edge>[] graph = new ArrayList[6];
createGraph(graph);
printAllPaths(graph, 5, 1 ,"");
}
}