Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. Here 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

  2. 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 …

  3. 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.

  4. 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 …

  5. DFS and BFS Explained in Java | Data Structures | Depth First …

    Nov 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
  6. 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 …

  7. 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 …

  8. 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 …

  9. 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 …

  10. 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 …