-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBFSnDFSInGraphs.java
92 lines (75 loc) · 2.65 KB
/
BFSnDFSInGraphs.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
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
import java.util.*;
class BFSnDFSInGraphs{
static class Edge{
int src;
int dest;
Edge(int src, int dest){
this.src = src;
this.dest = dest;
}
}
static void initializeGraph(ArrayList<ArrayList<Integer>> graph, int v, int e){
for(int i = 0; i<v; i++){
graph.add(new ArrayList<Integer>());
}
}
static void createAdjList(ArrayList<ArrayList<Integer>> graph, Edge e){
if((0<=e.src) && (e.src<graph.size())){
graph.get(e.src).add(e.dest);
System.out.println("src : " + e.src + " dest: " +e.dest);
}
else{
System.out.println("Node out of graph bounds, enter a valid source.");
}
}
static void bfs(ArrayList<ArrayList<Integer>> graph, boolean[] visited, int start) {
Queue<Integer> q = new LinkedList<>();
visited[start] = true;
q.add(start);
while (!q.isEmpty()) {
int current = q.poll();
System.out.print(current + " ");
for (int neighbor : graph.get(current)) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.add(neighbor);
}
}
}
System.out.println();
}
static void dfs(ArrayList<ArrayList<Integer>> graph, boolean[] visited, int current) {
if (visited[current]) {
return;
}
visited[current] = true;
System.out.print(current + " ");
for (int neighbor : graph.get(current)) {
if (!visited[neighbor]) {
dfs(graph, visited, neighbor);
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the no.of vertices: ");
int v = input.nextInt();
System.out.println("Enter the no.of edges: ");
int e = input.nextInt();
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
initializeGraph(graph, v, e);
for(int i = 0; i<e; i++){
System.out.println("Enter the edge (src, dest): ");
createAdjList(graph, new Edge(input.nextInt(), input.nextInt()));
}
boolean[] visited = new boolean[v];
System.out.println("Enter a start node for BFS/DFS: ");
int startNode = input.nextInt();
System.out.println("BFS Traversal:");
bfs(graph, visited, startNode);
// Reset visited array for DFS
Arrays.fill(visited, false);
System.out.println("DFS Traversal:");
dfs(graph, visited, startNode);
}
}