-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdfs.java
57 lines (48 loc) · 1.63 KB
/
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Graph {
private Map<Integer, List<Integer>> adjList;
// Constructor to initialize the graph
public Graph() {
adjList = new HashMap<>();
}
// Method to add an edge between two nodes (directed graph)
public void addEdge(int source, int destination) {
adjList.putIfAbsent(source, new ArrayList<>());
adjList.get(source).add(destination);
}
// DFS utility function
private void dfsUtil(int vertex, boolean[] visited) {
visited[vertex] = true;
System.out.print(vertex + " "); // Print the vertex as part of DFS traversal
// Visit all neighbors of the current vertex
List<Integer> neighbors = adjList.get(vertex);
if (neighbors != null) {
for (int neighbor : neighbors) {
if (!visited[neighbor]) {
dfsUtil(neighbor, visited);
}
}
}
}
// DFS traversal function
public void dfs(int startVertex) {
boolean[] visited = new boolean[adjList.size()];
System.out.println("DFS Traversal starting from vertex " + startVertex + ":");
dfsUtil(startVertex, visited);
}
public static void main(String[] args) {
Graph graph = new Graph();
// Add edges to the graph
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 2);
graph.addEdge(2, 0);
graph.addEdge(2, 3);
graph.addEdge(3, 3);
// Perform DFS starting from vertex 2
graph.dfs(2);
}
}