Depth First Search (DFS) Technique in Java
Depth First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking.
It uses a stack data structure, either explicitly or implicitly through recursion, to keep track of the vertices to be visited next.
DFS can be used to solve various problems such as finding connected components, topological sorting, and detecting cycles in graphs.
Java Implementation of DFS
// Import necessary packages import java.util.*; /** * This class represents a graph using adjacency list representation */ class Graph { // Number of vertices in the graph private int V; // Array of lists for adjacency list representation private LinkedList<Integer> adj[]; /** * Constructor to initialize the graph * * @param v Number of vertices */ Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList(); } /** * Function to add an edge into the graph * * @param v Source vertex * @param w Destination vertex */ void addEdge(int v, int w) { adj[v].add(w); } /** * A function used by DFS * * @param v The starting vertex * @param visited Array to keep track of visited vertices */ void DFSUtil(int v, boolean visited[]) { // Mark the current node as visited and print it visited[v] = true; System.out.print(v + " "); // Recur for all the vertices adjacent to this vertex for (Integer n : adj[v]) { if (!visited[n]) DFSUtil(n, visited); } } /** * The function to do DFS traversal. It uses DFSUtil() * * @param v The starting vertex */ void DFS(int v) { // Mark all the vertices as not visited (set as false by default in Java) boolean visited[] = new boolean[V]; // Call the recursive helper function to print DFS traversal</span DFSUtil(v, visited); } /** * Main method to test the DFS algorithm */ public static void main(String args[]) { Graph g = new Graph(4); 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("Following is Depth First Traversal (starting from vertex 2)"); g.DFS(2); } }
Explanation
The above program demonstrates the Depth First Search (DFS) algorithm in Java:
- Graph Class: The
Graph
class represents a graph using an adjacency list representation. It has methods to add edges and perform DFS. - DFSUtil Method: This is a recursive method used by the
DFS
method to visit nodes. It marks the current node as visited, prints it, and then recursively visits all adjacent vertices that have not been visited yet. - DFS Method: This method initializes the visited array and calls the
DFSUtil
method to start the traversal from the given starting vertex. - Main Method: This is the entry point of the program where a graph is created, edges are added, and the DFS traversal is initiated starting from vertex 2.
In the main method, we create a graph with 4 vertices and add edges between them. The DFS traversal starts from vertex 2 and prints the nodes in the order they are visited.
https://athens-rentalcars.com/pl/
You commit an error. I suggest it to discuss. Write to me in PM, we will communicate.
The authoritative message :), cognitively…
It is not pleasant to you?