forked from neiljaviya5/algos
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDFS.java
More file actions
39 lines (38 loc) · 733 Bytes
/
DFS.java
File metadata and controls
39 lines (38 loc) · 733 Bytes
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
import java.util.Scanner;
public class DFS
{
public static int[][] G = new int[5][5];
public static int[] visited = new int[5];
static int n;
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter number of vertex:");
n = input.nextInt();
System.out.print("enter matrix:");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
G[i][j] = input.nextInt();
}
}
for(int i=0 ; i<n ; i++)
{
visited[i] = 0;
}
dfs(0);
}
public static void dfs(int i)
{
visited[i] = 1;
System.out.println(i);
for(int j = 0; j < n; j++)
{
if(G[i][j] == 1 && visited[j] == 0)
{
dfs(j);
}
}
}
}