-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.java
45 lines (41 loc) · 1.01 KB
/
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
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class dfs {
static class Edge{
int src;
int dest;
int w;
Edge(int s,int d,int w){
this.src=s;
this.dest=d;
this.w=w;
}
}
static void DFS(ArrayList<Edge> graph[],int curr,boolean vis[]){
if(vis[curr]==true)
return;
System.out.print(curr+"->");
vis[curr]=true;
for(int i=0;i<graph[curr].size();i++)
{
Edge s=graph[curr].get(i);
DFS(graph,s.dest,vis);
}
return;
}
static void creategraph(ArrayList<Edge> graph[]){
for(int i=0;i<graph.length;i++)
{
graph[i]=new ArrayList<>();
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int v=s.nextInt();
ArrayList<Edge>graph[]=new ArrayList[v];
creategraph(graph);
boolean[] vis=new boolean[v];
DFS(graph, 0,vis);
}
}