You are on page 1of 20

Brute Force Technique

Characteristics of the brute force algorithm


• It's an intuitive, direct, and easy problem-solving
approach in which all of the alternative ways or
solutions to a particular problem are listed.
• The brute force method is used to solve many
issues in everyday life, such as investigating all the
ways to a local market to discover the shortest
path.
• Arranging books on a rack, taking advantage of all
available options to maximize rack space, and so
on.

1

PROS AND CONS OF BRUTE FORCE ALGORITHM


Pros:
 
• Listing all possible candidate solutions for the
issue is a sure way to locate the proper solution
using the brute force method.
• It is a general approach that may be used to a
wide range of issues.
• For small and simple issues, the brute force
technique is excellent.
• It is recognized for its simplicity and may be used
as a baseline for comparison.

2

PROS AND CONS OF BRUTE FORCE ALGORITHM


Cons:
 
• Using brute force to solve a problem is inefficient.
Algorithm analysis for real-time issues frequently
exceeds the O(N!) order of growth.
• Rather than a smart algorithm design, this technique
depends on sacrificing the power of a computer
system to solve a problem.
• It takes a long time for brute force algorithms to
work.
• When compared to algorithms built utilizing
heuristics, brute force algorithms are not constructive
or creative.
3
BRUTE FORCE ALGORITHM
✓ First, unlike some of the other strategies, brute force is applicable
to a very wide variety of problems.

✓ Second, for some important problems—e.g., sorting, searching,


matrix multiplication, string matching— the brute-force approach
yields reasonable algorithms of at least some practical value with
no limitation on instance size.

✓ Third, the expense of designing a more efficient algorithm may be


unjustifiable if only a few instances of a problem need to be
solved and a brute-force algorithm can solve those instances with
acceptable speed.

4
Brute force Algorithm

5
Brute Force and Exhaustive Search

Brute force is a straightforward approach to solving a problem,


usually directly based on the problem statement and definitions of
the concepts involved.

As an example, consider the exponentiation problem: compute an


for a nonzero number a and a nonnegative integer n. By the
definition of exponentiation, an = a∗ a∗ a∗ a ∗ ...∗a n times .

This suggests simply computing an by multiplying 1 by a n times.

6
Selection Sort
✓We start selection sort by scanning the entire given list to find its
smallest element and exchange it with the first element, putting the
smallest element in its final position in the sorted list.
✓Then we scan the list, starting with the second element, to find the
smallest among the last n−1 elements and exchange it with the second
element, putting the second smallest element in its final position.

7
Selection Chart

8
Implementation

9
Example

10
Time Complexity

(N-1) comparisons are necessary to identify the smallest element from an


array of N items. The size of an unsorted array decreases to (N-1) when
the minimum element is placed in its correct location, and then (N-2)
comparisons are necessary to determine the minimum in the unsorted
array.

11
Bubble Sort
Another brute-force application to the sorting problem is to compare
adjacent elements of the list and exchange them if they are out of
order. By doing it repeatedly, we end up “bubbling up” the largest
element to the last position on the list. The next pass bubbles up the
second largest element, and so on, until after n − 1 passes the list is
sorted.

12
Bubble Sort

13
The action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is
illustrated as an example in Figure below.

14
Exhaustive Search
Exhaustive search is simply a brute-force approach to combinatorial
problems. It suggests generating each and every element of the
problem domain, selecting those of them that satisfy all the
constraints, and then finding a desired element.

15
Traveling Salesman Problem
✓ In layman’s terms, the problem asks to find the shortest tour
through a given set of n cities that visits each city exactly once
before returning to the city where it started.

✓ The problem can be conveniently modelled by a weighted graph,


with the graph’s vertices representing the cities and the edge
weights specifying the distances. Then the problem can be stated as
the problem of finding the shortest Hamiltonian circuit of the
graph.

(A Hamiltonian circuit is defined as a cycle that passes through all the


vertices of the graph exactly once).

16
It is easy to see that a Hamiltonian circuit can also be defined as a sequence of n + 1
adjacent vertices vi0, vi1, . . . , vin−1, vi0, where the first vertex of the sequence is the same
as the last one and all the other n − 1 vertices are distinct.

Thus, we can get all the tours by generating all the permutations of n−1 intermediate-
cities, compute the tour lengths, and find the shortest among them. Figure below presents
a small instance of the problem and its solution by this method.

The number of permutation of


intermediate vertices b, c and d is
(n-1)! = 3! = 6 i.e. 6 different path of
the figure below.

17
An inspection of Figure reveals three pairs of tours that differ only by
their direction. Here results are 18, 11 and 23 and we got each of the
result twice as shown in fig. below. For example path length of a → b
→ c → d → a and a → d → c → b → a are both 18 since path is same
but only direction is different.

18
Hence, we could cut the number of vertex permutations by half. We
could, for example, choose any two intermediate vertices, say, b and c,
and then consider only permutations in which b precedes c.

The total number of permutations needed is 1/2(n − 1)! = 3, which


makes the exhaustive-search approach impractical for large values of n.

19
Knapsack Problem
Given n items of known weights w1, w2, . . . , wn and values v1, v2, . . . ,
vn and a knapsack of capacity W, find the most valuable subset of the
items that fit into the knapsack.

Instance of the knapsack problem

Its solution by exhaustive search. The information about


20
the optimal selection is in bold

You might also like