-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepth.java
More file actions
93 lines (79 loc) · 2.32 KB
/
Depth.java
File metadata and controls
93 lines (79 loc) · 2.32 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Depth {
public ArrayList<ArrayList<Integer>> adj;
public Depth(int n){
adj = new ArrayList<>();
for(int i =0; i < n; i++){
adj.add(new ArrayList<>());
}
}
public void addedge(int source , int destination){
adj.get(source).add(destination);
adj.get(destination).add(source);
}
public void dfs(int start,boolean vis[],ArrayList<Integer> ans){
vis[start] = true;
ans.add(start);
for(int it : adj.get(start)){
if(!vis[it]){
dfs(it,vis,ans);
}
}
}
public void bfs(int n){
Queue<Integer> q = new LinkedList<>();
boolean vis[] = new boolean[n];
q.offer(0);
ArrayList<Integer> ans = new ArrayList<>();
// vis[0] = true;
while(!q.isEmpty()){
int node = q.poll();
if(vis[node]){
continue;
}
vis[node] = true;
ans.add(node);
for(int it : adj.get(node)){
if(!vis[it]){
q.offer(it);
}
}
}
System.out.println("BFS Traversal:");
for (int node : ans) {
System.out.print(node + " ");
}
System.out.println();
}
public void print(){
for(int i = 0; i < adj.size(); i++){
for(int j = 0; j < adj.get(i).size(); j++){
System.out.print(adj.get(i).get(j) + " ");
}
System.out.println();
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Depth d = new Depth(n);
int m = sc.nextInt();
for(int i = 0; i < m; i++){
int src = sc.nextInt();
int dest = sc.nextInt();
d.addedge(src,dest);
}
boolean vis[] = new boolean[n];
ArrayList<Integer> ans = new ArrayList<>();
d.dfs(0,vis,ans);
for(int i = 0; i < ans.size();i++){
System.out.println(ans.get(i));
}
System.out.println("ADJ LIST>>>>>>>>>>>>>>>>>");
d.print();
d.bfs(n);
}
}