Here’s a generic template for Depth First Search (DFS) on a graph represented using an adjacency list:
import java.util.*;
public class Graph {
private int numVertices;
private Map<Integer, List<Integer>> adjList;
public Graph(int numVertices) {
this.numVertices = numVertices;
adjList = new HashMap<>();
for (int i = 0; i < numVertices; i++) {
adjList.put(i, new ArrayList<>());
}
}
// Add an edge between vertices v and w
public void addEdge(int v, int w) {
adjList.get(v).add(w);
}
// DFS starting from vertex v
public void DFS(int v) {
boolean[] visited = new boolean[numVertices];
DFSUtil(v, visited);
}
// Recursive utility function for DFS
private void DFSUtil(int v, boolean[] visited) {
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v + " ");
// Visit all the adjacent vertices of the current node that are not visited
for (int adjVertex : adjList.get(v)) {
if (!visited[adjVertex]) {
DFSUtil(adjVertex, visited);
}
}
}
public static void main(String[] args) {
Graph g = new Graph(4); // create a graph with 4 vertices
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Depth First Traversal starting from vertex 2:");
g.DFS(2);
}
}
- Above code represents the graph using an adjacency list. Each vertex is represented by an integer, and
adjList
maps each vertex to a list of its adjacent vertices. - The
DFS
method provides an interface to start a DFS from a particular vertex. The recursive helper functionDFSUtil
is used to explore the graph. - The
visited
array keeps track of which vertices have been visited to avoid cycles.
Let’s expand the previous example to use a complex node object instead of just an integer.
Here’s a generic template for Depth First Search (DFS) on a graph with nodes represented by a complex object:
import java.util.*;
class Node {
private String id;
private String data;
public Node(String id, String data) {
this.id = id;
this.data = data;
}
public String getId() {
return id;
}
public String getData() {
return data;
}
}
public class Graph {
private Map<Node, List<Node>> adjList; // adjacency list representation of a graph
public Graph() {
adjList = new HashMap<>();
}
// Add a node to the graph
public void addNode(Node node) {
adjList.putIfAbsent(node, new ArrayList<>());
}
// Add an edge between nodes v and w
public void addEdge(Node v, Node w) {
adjList.get(v).add(w);
}
// DFS starting from node v
public void DFS(Node startNode) {
Set<Node> visited = new HashSet<>();
DFSUtil(startNode, visited);
}
// Recursive utility function for DFS
private void DFSUtil(Node current, Set<Node> visited) {
// Mark the current node as visited and print it
visited.add(current);
System.out.print(current.getId() + " ");
// Visit all the adjacent nodes of the current node that are not visited
for (Node adjNode : adjList.get(current)) {
if (!visited.contains(adjNode)) {
DFSUtil(adjNode, visited);
}
}
}
public static void main(String[] args) {
Graph g = new Graph();
Node node0 = new Node("0", "data0");
Node node1 = new Node("1", "data1");
Node node2 = new Node("2", "data2");
Node node3 = new Node("3", "data3");
g.addNode(node0);
g.addNode(node1);
g.addNode(node2);
g.addNode(node3);
g.addEdge(node0, node1);
g.addEdge(node0, node2);
g.addEdge(node1, node2);
g.addEdge(node2, node0);
g.addEdge(node2, node3);
g.addEdge(node3, node3);
System.out.println("Depth First Traversal starting from node with ID 2:");
g.DFS(node2);
}
}
- In the above code, we have a
Node
class that encapsulates more complex data, with an ID and some other data. The ID is used to determine uniqueness (viaequals
andhashCode
methods). - Instead of an adjacency list using integers, we now use
Node
objects. - For the DFS traversal, we’re using a
Set
to keep track of visited nodes, sinceNode
objects are unique based on their IDs. - When performing the DFS, we’re printing the ID of the node, but you can also print/access other properties of the node as required.
You can expand upon this template and add more properties or methods to the Node
class based on your specific needs.