liveBook · Manning

Brute-force search

As explained in Machine Learning Algorithms in Depth

Complete search is a problem-solving technique that traverses an entire search space to find a solution, optionally pruning parts known not to contain one.

Brute-force search, also called complete search, solves a problem by traversing its entire search space until it finds a solution. It may prune branches proven unable to produce the required result. section 4.2.1

The method provides a baseline for algorithm design: enumerate candidate solutions, test or construct each one, and stop when the search reaches the required condition. Recursive and iterative implementations can express the same search. section 4.2.1

Recognize brute-force search by its role among common problem-solving paradigms:

  • Complete search examines the whole search space, unlike greedy methods that select locally optimal choices. section 1.5.2
  • Divide and conquer separates independent subproblems, while dynamic programming reuses solutions to overlapping subproblems in a table. section 4.5

Generating subsets

For a set of size n, a recursive search branches on whether each element belongs to the current subset and prints a subset when the required size is reached. An iterative bit-sequence method enumerates the same choices using the bit patterns from 0 through 2ⁿ−1; the supplied implementation demonstrates both approaches for n = 4. section 4.2.1

Listing 4.1 Subset generation
def search(k, n):
    if (k == n):
        print(subset)  #1
    else:
        search(k+1, n)
        subset.append(k)
        search(k+1, n)
        subset.pop()
    #end if

def bitseq(n):
    for b in range(1 << n):
        subset = []
        for i in range(n):
            if (b & 1 << i):
                subset.append(i)
        #end for
        print(subset)
    #end for 

if __name__ == "__main__":
    n = 4
    subset = []
    search(0, n)    #2

    subset = []
    bitseq(n)    #3

Inspect the from-scratch Python listing for recursive subset search, branch backtracking, termination, and iterative bit-sequence enumeration. section 4.2.1

How Machine Learning Algorithms in Depth covers it

How deep it goes
Defines exhaustive traversal, pruning, recursion, and iteration, then demonstrates both implementations for subset generation.
Examples
Demonstrates recursive and bit-sequence subset generation and exact inference by complete enumeration over probability tables.
What it assumes
Introduces algorithmic paradigms alongside data structures and implementation fundamentals rather than assuming a specialized search framework.
What you can do after
Build it from scratch using recursive branching, termination checks, pruning, backtracking, or bit-sequence enumeration.
How it connects
Connects complete search with exact inference, complete enumeration, graphical models, conditional independence, Bayes rule, greedy algorithms, divide and conquer, and dynamic programming.

Also explained in

Compare how 7 books explain brute-force search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Algorithms in Depth. The section links point to the parts of the book it draws on.