PROJECTENGRAM

[HO] 17 - GRAPHS TRAVERSALS

Graphs Traversals

Tags: #◇Computer Science #◇CCDSALG #◇Graphs Traversals #◇Conceptual #◇M6


◇Overview

This document covers basic algorithms for searching a graph: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores edges out of a vertex systemically layer by layer and produces a breadth-first tree. DFS explores as deeply as possible along each branch before backtracking.

◇Important Formulas

Algorithmic Properties & Complexities:

  • Adjacency List Representation: Time complexity is Θ(V+E)\Theta(V + E), Auxiliary space is O(V)O(V) for visited tracking and frontier queue/recursion stack.
  • Adjacency Matrix Representation: Time complexity is Θ(V2)\Theta(V^2).
  • BFS Shortest Path Property: For unweighted graphs, the breadth-first tree guarantees that for every vertex vv, the tree path from source ss to vv corresponds to a shortest path (minimum number of edges).
  • DFS Parenthesis Theorem: For any two vertices uu and vv, the intervals [d[u],f[u]][d[u], f[u]] and [d[v],f[v]][d[v], f[v]] are either completely disjoint or one is strictly contained within the other.

◇Theoretical Framework: BFS vs. DFS Exploration Models

DFS Spanning Forest & Edge Taxonomy

Traversal Execution Pipelines

◇Worked Examples

  1. BFS Traversal Trace: Starting at vertex AA in graph with edges (A,B),(A,C),(B,D),(C,E)(A, B), (A, C), (B, D), (C, E).
    • Init: Q=[A]Q = [A], visited = {A}\{A\}, distances: d[A]=0d[A]=0.
    • Dequeue AA: Enqueue unvisited neighbors B,CB, C. Q=[B,C]Q = [B, C], d[B]=1,d[C]=1d[B]=1, d[C]=1.
    • Dequeue BB: Enqueue unvisited neighbor DD. Q=[C,D]Q = [C, D], d[D]=2d[D]=2.
    • Dequeue CC: Enqueue unvisited neighbor EE. Q=[D,E]Q = [D, E], d[E]=2d[E]=2.
    • Dequeue D,ED, E: No unvisited neighbors. Q=[]Q = []. Done.
  2. DFS Cycle Detection: A back edge encountered during DFS on a directed graph indicates a cycle. If neighbor vv is already marked visited and is currently on the recursion call stack (gray state in 3-color model), a directed cycle is confirmed.

◇References & Navigation