- This summary was generated by AI from multiple online sources. Find the source links used for this summary under "Based on sources".
Learn more about Bing search results how Bing delivers search resultsHere is a simple implementation of Depth First Search (DFS) in Java for a graph using both recursive and iterative approaches.DFS Implementation in Java
1. Recursive Approach
import java.util.*; class Graph { private int vertices; // Number of vertices private LinkedList<Integer> adjList[]; // Adjacency list // Constructor Graph(int v) { vertices = v; adjList = new LinkedList[v]; for (int i = 0; i < v; i++) { adjList[i] = new LinkedList<>(); } } // Add edge to the graph void addEdge(int v, int w) { adjList[v].add(w); // Add w to v's list } // Recursive DFS method void DFSUtil(int v, boolean visited[]) { visited[v] = true; // Mark the current node as visited System.out.print(v + " "); // Print the node // Recur for all the vertices adjacent to this vertex for (Integer neighbor : adjList[v]) { if (!visited[neighbor]) { DFSUtil(neighbor, visited); } } } // The function to do DFS traversal void DFS(int start) { boolean visited[] = new boolean[vertices]; // Mark all the vertices as not visited DFSUtil(start, visited); // Call the recursive helper function } } // Example usage public class Main { public static void main(String args[]) { Graph g = new Graph(5); // Create a graph with 5 vertices g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(2, 4); System.out.println("Depth First Traversal starting from vertex 0:"); g.DFS(0); // Start DFS from vertex 0 } }2. Iterative Approach
import java.util.*; class Graph { private int vertices; // Number of vertices private LinkedList<Integer> adjList[]; // Adjacency list // Constructor Graph(int v) { vertices = v; adjList = new LinkedList[v]; for (int i = 0; i < v; i++) { adjList[i] = new LinkedList<>(); } } // Add edge to the graph void addEdge(int v, int w) { adjList[v].add(w); // Add w to v's list } // Iterative DFS method void DFSIterative(int start) { boolean visited[] = new boolean[vertices]; // Mark all the vertices as not visited Stack<Integer> stack = new Stack<>(); // Create a stack for DFS stack.push(start); // Push the starting vertex while (!stack.isEmpty()) { int v = stack.pop(); // Get the top vertex if (!visited[v]) { visited[v] = true; // Mark it as visited System.out.print(v + " "); // Print the vertex } // Push all unvisited adjacent vertices to the stack for (Integer neighbor : adjList[v]) { if (!visited[neighbor]) { stack.push(neighbor); } } } } } // Example usage public class Main { public static void main(String args[]) { Graph g = new Graph(5); // Create a graph with 5 vertices g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(2, 4); System.out.println("Depth First Traversal (Iterative) starting from vertex 0:"); g.DFSIterative(0); // Start DFS from vertex 0 } }Explanation
- Graph Representation: The graph is represented using an adjacency list.
- DFS Methods: The
DFSUtilmethod is used for the recursive approach, while theDFSIterativemethod implements the iterative approach using a stack.
GeeksForGeeksJava Program for Depth First Search or DFS for a GraphDepth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. Prerequisite: Graph knowledge is important to understand the concept of DFS. What is DFS? …https://www.geeksforgeeks.org › java › java-program-for-depth-first-search-or-dfs-for-a-graphProgramizDepth First Search (DFS) Algorithm - ProgramizLearn how to implement the DFS algorithm in Java with an example of an undirected graph. The web page also explains the pseudocode, complexity, and applications of DFS.https://www.programiz.com › dsa › graph-dfs Level Order Traversal (Breadth First Search) of Binary Tree
Dec 8, 2025 · Level Order Traversal technique is a method to traverse a Tree such that all nodes present in the same level are traversed completely before traversing the next level. Input: The …
See results only from geeksforgeeks.orgZigZag Tree Traversal
[Naive Approach] - Using Recursion The …
Topological Sorting
The main idea is to perform a Depth First …
Depth First Search (DFS) - NamasteDev Blogs
Nov 24, 2025 · We implement Depth-First Search (DFS) using a Stack data structure. Push the start node into the stack. Create a visited set to track visited nodes. Pop the top node. If not …
DFS vs BFS Algorithm (All Differences With Example)
Nov 26, 2025 · Learn the key differences between DFS vs BFS algorithms with examples. Understand their applications, time complexity, and how they work in graph traversal.
5. Graph Traversal: Depth-First Search (DFS)
Nov 29, 2025 · Depth-First Search (DFS) is a traversal algorithm that explores the graph by going as "deep" as possible down one path before backtracking. Imagine exploring a maze with only …
DFS and BFS Explained in Java | Data Structures | Depth First …
Watch full videoNov 26, 2025 · In this video, I fully explain the Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms using the Java programming language. This video was created to fulfill the Practical...
- Author: Tofik Nuryanto
- Views: 1
Use cases of DFS and BFS | Labuladong Algo Notes
Dec 8, 2025 · Before reading this article, you need to first learn: In real algorithm problems, the DFS algorithm is commonly used to enumerate all paths, while the BFS algorithm is often used …
Topological Sorting - GeeksforGeeks
Dec 6, 2025 · The main idea is to perform a Depth First Search (DFS) on the Directed Acyclic Graph (DAG) and, for each vertex, push it onto a stack …
Tarjan's Algorithm to find Strongly Connected …
Dec 8, 2025 · Strongly Connected Components form subtrees of the DFS tree. If we can find the head of such subtrees, we can print/store all the …
Strongly Connected Components - GeeksforGeeks
Dec 12, 2025 · We first do a DFS on the original graph and record the finish times of nodes (i.e., the time at which the DFS finishes exploring a node …
Breadth First Search or BFS for a Graph
Dec 6, 2025 · BFS is different from DFS in a way that closest vertices are visited before others. We mainly traverse vertices level by level. Popular …