brute force algorithm
computer science
algorithm basics
programming techniques
problem solving

what exactly is the brute force algorithm

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In the realm of computer science and algorithm design, a variety of approaches are used to solve problems efficiently. One fundamental strategy, known as the brute force algorithm, represents the most straightforward methodology for problem-solving: it attempts all possibilities to find a solution. Despite its simplicity, this approach holds significant value in both theoretical and practical applications.

Understanding the Brute Force Algorithm

At its core, a brute force algorithm is an exhaustive search strategy used to find all possible solutions and select the most suitable one. It iterates through potential options until it finds a valid solution or confirms that none exists. This method is the simplest and most general approach to problem-solving, applicable to many domains but often criticized for its inefficiency.

Technical Explanation

A brute force algorithm systematically enumerates all possible candidates for the solution and checks whether each candidate satisfies the problem's statement:

Linear Search: A classic example, where a brute force algorithm runs through each element of a list to find a target value. If the list contains nn elements, it will check nn times, resulting in a time complexity of O(n)\mathcal{O}(n).

String Matching: In tasks where a substring needs to be found within a larger string, the brute force method checks each possible position independently. Given strings of lengths mm and nn , this leads to a time complexity of O(mn)\mathcal{O}(mn).

Combinatorial Problems: Brute force is often applied in problems like the Traveling Salesman Problem (TSP), where all permutations of cities are considered to find the shortest route, leading to a time complexity of O(n!)\mathcal{O}(n!).

Example

Consider a problem where you need to guess a 4-digit pin, where each digit ranges from 0 to 9:

Cryptography: To test the strength of cryptographic keys by attempting every possible key until the correct one is found. • Puzzle Solving: Solving games or puzzles where paths or permutations are generated and validated. • Search and Optimization: Used as a baseline to compare more sophisticated algorithms against, often in combination with heuristics or optimization techniques. • Divide and Conquer: Splits the problem into smaller, more manageable parts. • Dynamic Programming: Utilizes overlapping subproblems and optimal substructure properties. • Greedy Algorithms: Makes locally optimal choices to find a global optimum. • Backtracking: Utilizes incremental approach and abandons suboptimal solutions.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.