Fiveable

๐Ÿ”Data Structures Unit 14 Review

QR code for Data Structures practice questions

14.2 Tree and Graph Search Algorithms

๐Ÿ”Data Structures
Unit 14 Review

14.2 Tree and Graph Search Algorithms

Written by the Fiveable Content Team โ€ข Last updated September 2025
Written by the Fiveable Content Team โ€ข Last updated September 2025
๐Ÿ”Data Structures
Unit & Topic Study Guides

Tree and graph traversal algorithms are essential tools for exploring and analyzing complex data structures. These methods, including Depth-First Search (DFS) and Breadth-First Search (BFS), offer different approaches to navigating through interconnected nodes and edges.

Understanding these traversal techniques is crucial for solving various problems in computer science. From finding shortest paths to identifying connected components, these algorithms form the foundation for more advanced graph and tree operations.

Tree and Graph Traversal Algorithms

DFS and BFS for traversal

  • DFS traverses tree or graph by exploring as far as possible along each branch before backtracking
    • Implemented using stack data structure or recursion
    • DFS traversal orders for trees:
      • Pre-order: Visit root, traverse left subtree, traverse right subtree (binary tree, expression tree)
      • In-order: Traverse left subtree, visit root, traverse right subtree (binary search tree)
      • Post-order: Traverse left subtree, traverse right subtree, visit root (directory tree, game tree)
    • DFS traversal for graphs explores as far as possible along each branch before backtracking
      • Marks visited nodes to avoid revisiting and prevent infinite loops (cyclic graph, maze)
  • BFS traverses tree or graph level by level, exploring all neighbors before moving to next level
    • Implemented using queue data structure
    • BFS traversal order for trees visits nodes at each level from left to right before moving to next level (complete binary tree, heap)
    • BFS traversal for graphs explores all neighbors of a node before moving to next level
      • Marks visited nodes to avoid revisiting and prevent infinite loops (social network, web crawler)

Complexity of search algorithms

  • Time complexity:
    • DFS: $O(V + E)$, where $V$ is number of vertices and $E$ is number of edges
      • In worst case, DFS visits each vertex and edge once (dense graph, complete graph)
    • BFS: $O(V + E)$, where $V$ is number of vertices and $E$ is number of edges
      • In worst case, BFS visits each vertex and edge once (dense graph, complete graph)
  • Space complexity:
    • DFS: $O(V)$, where $V$ is number of vertices
      • In worst case, stack or recursion depth can be as large as number of vertices (skewed tree, linked list)
    • BFS: $O(V)$, where $V$ is number of vertices
      • In worst case, queue can contain all vertices at a given level (complete binary tree, star graph)

Tree Search Algorithms

Search in tree structures

  • Binary search trees (BSTs) allow efficient searching, insertion, and deletion operations
    • Balanced BSTs (AVL trees, Red-Black trees) ensure $O(\log n)$ time complexity for search, insertion, and deletion
      • Maintain height difference of at most 1 between left and right subtrees of any node (balanced AVL tree, balanced Red-Black tree)
    • Unbalanced BSTs may degenerate into linked list, resulting in $O(n)$ time complexity for search, insertion, and deletion (skewed BST, degenerate BST)
  • Searching in a BST:
    1. Compare target value with current node's value
    2. If target is smaller, recursively search left subtree
    3. If target is larger, recursively search right subtree
    4. If target is equal to current node's value, search is successful (found node, search hit)
    • Time complexity: $O(\log n)$ for balanced BSTs, $O(n)$ for unbalanced BSTs

Applications of search algorithms

  • Shortest path problems:
    • Dijkstra's algorithm finds shortest path from single source vertex to all other vertices in weighted graph with non-negative edge weights
      • Maintains priority queue of vertices ordered by tentative distances from source (min-heap, Fibonacci heap)
      • Relaxes edges by updating tentative distances of neighboring vertices (edge relaxation, greedy algorithm)
      • Time complexity: $O((V + E) \log V)$ with binary heap, or $O(V^2)$ with simple array
    • Bellman-Ford algorithm finds shortest path from single source vertex to all other vertices in weighted graph, allowing negative edge weights
      • Relaxes all edges $V-1$ times, where $V$ is number of vertices (dynamic programming, shortest path tree)
      • Detects negative-weight cycles and reports their presence (arbitrage opportunity, inconsistent graph)
      • Time complexity: $O(VE)$
  • Connected components:
    • Use DFS or BFS to traverse graph and identify connected components
      1. Start from unvisited vertex and perform DFS or BFS
      2. All vertices visited during traversal belong to same connected component (strongly connected component, weakly connected component)
      3. Repeat process starting from unvisited vertex until all vertices have been visited
    • Time complexity: $O(V + E)$ for both DFS and BFS