-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-Path DFS
More file actions
55 lines (53 loc) · 1.44 KB
/
Copy pathGet-Path DFS
File metadata and controls
55 lines (53 loc) · 1.44 KB
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
import java.util.Scanner;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int V = s.nextInt();
int E = s.nextInt();
/* Write Your Code Here
* Complete the Rest of the Program
* You have to take input and print the output yourself
*/
int edges[][] = new int[V][V];
for (int i=0;i<E;i++){
int fv = s.nextInt();
int sv = s.nextInt();
edges[fv][sv] = 1;
edges[sv][fv] = 1;
}
int start = s.nextInt();
int end = s.nextInt();
boolean visited[] = new boolean[edges.length];
ArrayList<Integer> arr = getPath(start,end,edges,visited);
if(arr!=null){
for(int i=0;i<arr.size();i++){
System.out.print(arr.get(i)+" ");
}
}
}
public static ArrayList<Integer> getPath(int start,int end,int[][] edges,boolean[] visited){
if(start == end){
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(end);
return arr;
}
/* if(edges[start][end]==1 ){
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(end);
arr.add(start);
return arr;
}*/
visited[start] = true;
for(int i=0;i<edges.length;i++){
if(edges[start][i]==1 && !visited[i]){
ArrayList<Integer> arr= getPath(i,end,edges,visited);
if(arr!=null){
arr.add(start);
return arr;
}
}
}
return null;
}
}