-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs_dfs.java
59 lines (58 loc) · 2.02 KB
/
bfs_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
54
55
56
57
58
59
import java.util.*;
public class bfs_dfs {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of vertices in the graph");
int n = input.nextInt();
int[][] matrix = new int[n][n];
System.out.println("Enter the adjacency matrix");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = input.nextInt();
}
}
System.out.println("Enter the source vertex");
int source = input.nextInt();
bfs(matrix,source);
dfs(matrix,source);
}
static void bfs(int[][] matrix, int source){
boolean[] visited = new boolean[matrix.length];
visited[source-1] = true;
Queue<Integer> queue = new LinkedList<>();
queue.add(source);
System.out.println("The breadth first order is:");
while(!queue.isEmpty()){
System.out.print(queue.peek()+" ");
int x = queue.poll();
int i;
for(i=0; i<matrix.length;i++){
if(matrix[x-1][i] == 1 && visited[i] == false){
queue.add(i+1);
visited[i] = true;
}
}
}
}
static void dfs(int[][] matrix, int source){
boolean[] visited = new boolean[matrix.length];
visited[source-1] = true;
Stack<Integer> stack = new Stack<>();
stack.push(source);
int i,x;
System.out.println("The depth first order is:");
System.out.println(source);
while(!stack.isEmpty()){
x = stack.pop();
for(i=0; i<matrix.length; i++){
if(matrix[x-1][i] == 1 && visited[i] == false){
stack.push(x);
visited[i] = true;
System.out.print(" "+(i+1));
x = i+1;
i = -1;
}
}
}
}
}