-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDFS.java
53 lines (53 loc) · 971 Bytes
/
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
import java.util.Scanner;
import java.util.LinkedList;
class Dfs
{
int v;
LinkedList<Integer> adj[];
Dfs(int v)
{
this.v=v;
adj=new LinkedList[v];
for(int i=0;i<v;i++)
{
adj[i]=new LinkedList<>();
}
}
void addEdge(int src,int dest)
{
adj[src].add(dest);
}
void dfsUtil(int s,boolean visited[])
{
visited[s]=true;
System.out.print(s+" ");
for(Integer i:adj[s])
{
if(!visited[i])
{
dfsUtil(i,visited);
}
}
}
void mdfs(int s)
{
boolean visited[]=new boolean[v];
dfsUtil(s,visited);
}
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int e=sc.nextInt();
Dfs d=new Dfs(v);
for(int i=1;i<=e;i++)
{
int src=sc.nextInt();
int dest=sc.nextInt();
d.addEdge(src,dest);
}
//Vertix where we want to start Dfs
int s=sc.nextInt();
d.mdfs(s);
}
}