-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS1.java
More file actions
43 lines (37 loc) · 1.04 KB
/
Copy pathDFS1.java
File metadata and controls
43 lines (37 loc) · 1.04 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
import java.util.Scanner;
public class DFS1 {
private static Scanner sc = new Scanner(System.in);
private static int[][] G = new int[10][10];
private static int[] visited = new int[10];
private static int N;
public static void main(String[] args) {
int i, j;
System.out.println("Enter the number of vertices?");
N = sc.nextInt();
System.out.println("Enter adjacency matrix?");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
G[i][j] = sc.nextInt();
}
}
// Initialize visited array
for (int k = 0; k < N; k++) {
visited[k] = 0;
}
for (int k = 0; k < N; k++) {
if (visited[k] == 0) {
DFS(k);
}
}
}
private static void DFS(int i) {
int j;
System.out.println("\n" + i);
visited[i] = 1;
for (j = 0; j < N; j++) {
if (visited[j] == 0 && G[i][j] == 1) {
DFS(j);
}
}
}
}