Graph Traversal Algorithms: BFS &
DFS
• Presented by: [Your Name]
• Course: Data Structures
• Instructor: [Instructor’s Name]
Introduction
• • Graph traversal means visiting all the
vertices of a graph.
• • BFS and DFS are two primary methods.
• • Common in pathfinding, AI, web crawling.
Graph Basics
• • Graph: Vertices (nodes) and Edges
(connections)
• • Types: Directed/Undirected,
Weighted/Unweighted
• • Represented by adjacency list or matrix.
What is BFS?
• • Breadth-First Search (BFS) explores
neighbors level by level.
• • Uses a queue (FIFO).
• • Best for shortest paths in unweighted
graphs.
What is DFS?
• • Depth-First Search (DFS) goes deep before
backtracking.
• • Uses a stack or recursion.
• • Useful for cycle detection, topological sort.
BFS Algorithm Steps
• 1. Choose a start node.
• 2. Mark it visited and enqueue.
• 3. While the queue is not empty:
• - Dequeue a node.
• - Visit all unvisited neighbors, mark and
enqueue them.
• (Pseudo-code shown here)
DFS Algorithm Steps
• 1. Choose a start node.
• 2. Mark it visited.
• 3. Visit each unvisited neighbor recursively.
• (Pseudo-code shown here)
BFS Example 1
• Graph: A-B-C, A-D, B-E, D-E
• Start: A
• BFS Order: A, B, D, C, E
• Queue at each step: (visuals or table)
DFS Example 1
• Same graph as BFS Example 1
• Start: A
• DFS Order: A, B, C, E, D
• Call stack or recursion flow
BFS Example 2 – Maze Solver
• • Grid maze
• • BFS explores level-by-level
• • Ensures shortest path is found
• • Visual: Maze/grid animation
DFS Example 2 – Puzzle Solver
• • Used in Sudoku, N-Queens, etc.
• • Explores deeply
• • Backtracks on failure
BFS vs DFS Comparison
• Feature BFS DFS
• Data Struct. Queue Stack/Recursion
• Path Found Shortest (yes) Not guaranteed
• Space Usage High (wide graphs)Low
• Application Pathfinding Cycle detection
BFS Applications
• • Finding shortest paths
• • Social networks (mutual friends)
• • GPS navigation
• • Web crawlers
DFS Applications
• • Cycle detection
• • Topological sorting
• • Solving puzzles & games
• • Checking connectivity
BFS Example 3 – Social Network
• • Nodes = People, Edges = Friendships
• • BFS helps find:
• - Friends of friends
• - Social distances
DFS Example 3 – File System
• • Folders = nodes, subfolders = edges
• • DFS explores folder hierarchy
• • Used in deep file search
Conclusion & Q/A
• • BFS and DFS are fundamental algorithms
• • Know when to use each
• • Any questions?
Real-World Analogies
• • BFS: Like waves spreading out in a pond –
reaches nearby nodes first.
• • DFS: Like exploring a cave – go deep,
backtrack on dead ends.
Mini Use Case – BFS: Emergency
Evacuation
• In an office building, BFS helps find the
shortest route to the nearest exit.
• Simulates real-time emergency escape paths.
Mini Use Case – DFS: Exploring a
Maze
• DFS is like someone trying every possible path
in a maze.
• Goes deep into one corridor until a dead end,
then backtracks.
Advanced Variants
• • Bidirectional BFS – Efficient for huge graphs.
• • Iterative Deepening DFS – Combines DFS
space with BFS completeness.
• • BFS with Priority Queue – Leads into
Dijkstra’s Algorithm.
Code Walkthrough – BFS (C++)
• // BFS Code Sample in C++
• queue<int> q;
• visited[start] = true;
• q.push(start);
• while (!q.empty()) {
• int node = q.front(); q.pop();
• for (int neighbor : adj[node]) {
• if (!visited[neighbor]) {
• visited[neighbor] = true;
Memory Usage Comparison
• • Wide Graphs: BFS uses more memory
(queue grows large).
• • Deep Graphs: DFS is space-efficient (stack or
recursion depth).
BFS & DFS in Interviews
• • Detect cycles in directed graph using DFS.
• • Use BFS when shortest path is needed in
unweighted graph.
• • When to use DFS vs BFS?
BFS & DFS in Trees vs Graphs
• • In trees: No cycles – simple traversal.
• • In graphs: Must track visited to avoid infinite
loops.
• • More complex in cyclic/undirected graphs.
Bonus Example – Web Crawler
• • BFS starts from a URL, visits all directly
linked pages.
• • Great for level-based crawling like site map
generation.
Summary Diagram / Mind Map
• BFS: Queue → Level Order → Shortest Path
• DFS: Stack/Recursion → Depth → Cycle
Detection
• Use cases, code, and analogies combined
visually.

BFS_DFS_Enhanced_Presentation124567.pptx

  • 1.
    Graph Traversal Algorithms:BFS & DFS • Presented by: [Your Name] • Course: Data Structures • Instructor: [Instructor’s Name]
  • 2.
    Introduction • • Graphtraversal means visiting all the vertices of a graph. • • BFS and DFS are two primary methods. • • Common in pathfinding, AI, web crawling.
  • 3.
    Graph Basics • •Graph: Vertices (nodes) and Edges (connections) • • Types: Directed/Undirected, Weighted/Unweighted • • Represented by adjacency list or matrix.
  • 4.
    What is BFS? •• Breadth-First Search (BFS) explores neighbors level by level. • • Uses a queue (FIFO). • • Best for shortest paths in unweighted graphs.
  • 5.
    What is DFS? •• Depth-First Search (DFS) goes deep before backtracking. • • Uses a stack or recursion. • • Useful for cycle detection, topological sort.
  • 6.
    BFS Algorithm Steps •1. Choose a start node. • 2. Mark it visited and enqueue. • 3. While the queue is not empty: • - Dequeue a node. • - Visit all unvisited neighbors, mark and enqueue them. • (Pseudo-code shown here)
  • 7.
    DFS Algorithm Steps •1. Choose a start node. • 2. Mark it visited. • 3. Visit each unvisited neighbor recursively. • (Pseudo-code shown here)
  • 8.
    BFS Example 1 •Graph: A-B-C, A-D, B-E, D-E • Start: A • BFS Order: A, B, D, C, E • Queue at each step: (visuals or table)
  • 9.
    DFS Example 1 •Same graph as BFS Example 1 • Start: A • DFS Order: A, B, C, E, D • Call stack or recursion flow
  • 10.
    BFS Example 2– Maze Solver • • Grid maze • • BFS explores level-by-level • • Ensures shortest path is found • • Visual: Maze/grid animation
  • 11.
    DFS Example 2– Puzzle Solver • • Used in Sudoku, N-Queens, etc. • • Explores deeply • • Backtracks on failure
  • 12.
    BFS vs DFSComparison • Feature BFS DFS • Data Struct. Queue Stack/Recursion • Path Found Shortest (yes) Not guaranteed • Space Usage High (wide graphs)Low • Application Pathfinding Cycle detection
  • 13.
    BFS Applications • •Finding shortest paths • • Social networks (mutual friends) • • GPS navigation • • Web crawlers
  • 14.
    DFS Applications • •Cycle detection • • Topological sorting • • Solving puzzles & games • • Checking connectivity
  • 15.
    BFS Example 3– Social Network • • Nodes = People, Edges = Friendships • • BFS helps find: • - Friends of friends • - Social distances
  • 16.
    DFS Example 3– File System • • Folders = nodes, subfolders = edges • • DFS explores folder hierarchy • • Used in deep file search
  • 17.
    Conclusion & Q/A •• BFS and DFS are fundamental algorithms • • Know when to use each • • Any questions?
  • 18.
    Real-World Analogies • •BFS: Like waves spreading out in a pond – reaches nearby nodes first. • • DFS: Like exploring a cave – go deep, backtrack on dead ends.
  • 19.
    Mini Use Case– BFS: Emergency Evacuation • In an office building, BFS helps find the shortest route to the nearest exit. • Simulates real-time emergency escape paths.
  • 20.
    Mini Use Case– DFS: Exploring a Maze • DFS is like someone trying every possible path in a maze. • Goes deep into one corridor until a dead end, then backtracks.
  • 21.
    Advanced Variants • •Bidirectional BFS – Efficient for huge graphs. • • Iterative Deepening DFS – Combines DFS space with BFS completeness. • • BFS with Priority Queue – Leads into Dijkstra’s Algorithm.
  • 22.
    Code Walkthrough –BFS (C++) • // BFS Code Sample in C++ • queue<int> q; • visited[start] = true; • q.push(start); • while (!q.empty()) { • int node = q.front(); q.pop(); • for (int neighbor : adj[node]) { • if (!visited[neighbor]) { • visited[neighbor] = true;
  • 23.
    Memory Usage Comparison •• Wide Graphs: BFS uses more memory (queue grows large). • • Deep Graphs: DFS is space-efficient (stack or recursion depth).
  • 24.
    BFS & DFSin Interviews • • Detect cycles in directed graph using DFS. • • Use BFS when shortest path is needed in unweighted graph. • • When to use DFS vs BFS?
  • 25.
    BFS & DFSin Trees vs Graphs • • In trees: No cycles – simple traversal. • • In graphs: Must track visited to avoid infinite loops. • • More complex in cyclic/undirected graphs.
  • 26.
    Bonus Example –Web Crawler • • BFS starts from a URL, visits all directly linked pages. • • Great for level-based crawling like site map generation.
  • 27.
    Summary Diagram /Mind Map • BFS: Queue → Level Order → Shortest Path • DFS: Stack/Recursion → Depth → Cycle Detection • Use cases, code, and analogies combined visually.