DEV Community

Ajith R
Ajith R

Posted on

DFS vs BFS

Exploring DFS (Depth-First Search) and BFS (Breadth-First Search)

DFS and BFS are two fundamental graph traversal algorithms used to explore and search through graphs. Let's delve into each algorithm, discuss their differences, and analyze their complexities.

Depth-First Search (DFS)

DFS explores a graph by traversing as far as possible along each branch before backtracking. Here's an implementation of DFS in JavaScript:

class Graph {
    constructor() {
        this.list = {};
    }

    DFS(vertex, visited = {}) {
        visited[vertex] = true;
        console.log(vertex);
        for (let i = 0; i < this.list[vertex].length; i++) {
            let neighbor = this.list[vertex][i];
            if (!visited[neighbor]) {
                this.DFS(neighbor, visited);
            }
        }
    }

    // Other methods for adding/removing vertices and edges...
}
Enter fullscreen mode Exit fullscreen mode

DFS starts at a specified vertex, visits all its neighbors recursively, and continues exploring unvisited vertices until it reaches a dead end.

Breadth-First Search (BFS)

BFS explores a graph level by level, visiting all neighbors of a vertex before moving to the next level. Here's an implementation of BFS in JavaScript:

class Graph {
    constructor() {
        this.list = {};
    }

    BFS(start) {
        let queue = [start];
        let visited = {};
        let result = [];
        while (queue.length) {
            let currentVertex = queue.shift();
            result.push(currentVertex);
            this.list[currentVertex].forEach((neighbor) => {
                if (!visited[neighbor]) {
                    visited[neighbor] = true;
                    queue.push(neighbor);
                }
            });
        }
        return result;
    }

    // Other methods for adding/removing vertices and edges...
}
Enter fullscreen mode Exit fullscreen mode

BFS explores vertices level by level, ensuring that all vertices at the current level are visited before moving to the next level.

Comparison and Complexity

  • Traversal Order: DFS explores vertices in depth-first order, while BFS explores vertices in breadth-first order.
  • Space Complexity: DFS typically uses less memory compared to BFS as it only needs to store the path to the current vertex. In contrast, BFS requires a queue to store vertices at each level, resulting in higher memory usage.
  • Time Complexity: Both DFS and BFS have a time complexity of O(V + E), where V is the number of vertices and E is the number of edges in the graph. However, the actual runtime may vary based on the graph's structure.

Conclusion

In conclusion, DFS and BFS are powerful algorithms for traversing and searching graphs. DFS is well-suited for tasks like finding connected components and detecting cycles, while BFS is ideal for shortest path and minimum spanning tree problems. Understanding the differences and trade-offs between DFS and BFS is essential for choosing the right algorithm for your specific graph-related tasks.

Top comments (0)