-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay26.java
More file actions
50 lines (44 loc) · 1.47 KB
/
Day26.java
File metadata and controls
50 lines (44 loc) · 1.47 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
import java.util.*;
class Graph {
private int vertices;
private Map<Integer, List<Integer>> adjacencyList;
public Graph(int vertices) {
this.vertices = vertices;
this.adjacencyList = new HashMap<>();
for (int i = 0; i < vertices; i++) {
adjacencyList.put(i, new LinkedList<>());
}
}
public void addEdge(int source, int destination) {
adjacencyList.get(source).add(destination);
adjacencyList.get(destination).add(source); // For undirected graph
}
public void dfsTraversal(int startVertex) {
Set<Integer> visited = new HashSet<>();
System.out.println("DFS Traversal starting from vertex " + startVertex + ":");
dfsHelper(startVertex, visited);
System.out.println();
}
private void dfsHelper(int vertex, Set<Integer> visited) {
visited.add(vertex);
System.out.print(vertex + " ");
for (int neighbor : adjacencyList.get(vertex)) {
if (!visited.contains(neighbor)) {
dfsHelper(neighbor, visited);
}
}
}
}
public class Day26 {
public static void main(String[] args) {
// Example usage:
Graph graph = new Graph(7);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 5);
graph.addEdge(2, 6);
graph.dfsTraversal(0);
}
}