Search

Depth-First Search and Backtracking

Introduction to DFS (Depth-First Search) What is a Search Algorithm? A search algorithm exhaustively explores part or all of the solution space of a problem to find its solution. Depth-First Search (DFS) Essence: DFS is essentially brute-force enumeration. “Depth-first” principle: Go as far down one path as possible; only backtrack when no further progress can be made. Example: Finding a Path from Node 1 to Node 8 Starting from node 1, always move to an unvisited node if one exists; otherwise, backtrack.

Read note →

Binary Search

1. Core Concepts Binary search is the standard algorithm for locating a specific element in a sorted sequence. It operates on the divide-and-conquer principle. Mechanism: Repeatedly halves the search space. By comparing the target with the middle element, we eliminate half of the candidates instantly. Prerequisite: The sequence must be sorted (monotonic). Complexity: Time: $O(\log n)$ (Dramatically faster than the $O(n)$ of linear search). Space: $O(1)$ (Iterative implementation). 2. Python’s bisect Module implementing binary search manually is error-prone (off-by-one errors are common). Python’s bisect module provides a standardized, robust C-optimized implementation.

Read note →