Unit 5 Brute Force Method: Structure
Unit 5 Brute Force Method: Structure
Unit 5 Brute Force Method: Structure
5.1 Introduction
In the earlier unit you studied about the mathematical analysis of algorithms.
In this unit you will study about brute force method in detail with algorithms.
Brute force is a problem solving technique wherein we compute a series of
possible answers and test each possible answer for accuracy. It simply tries
all possibilities until a satisfactory solution is found. Once it finds the value
for the best solution it stops. In this unit we will discuss various algorithms
which make use of the brute force method.
Objectives:
After studying this unit, you should be able to:
define brute force method
describe and analyze selection sort and bubble sort
analyze and compute sequential search and brute force string matching
explain and discuss the exhaustive search
Let di = denomination of pi
In the Indian system pi = { Rs 1, Rs 2, Rs 5, Rs 10,..} the di = { 1, 2,5, 10..}
If we have to count a given sum of money A we need to find the smallest
Such that
x
i 1
i
Such that
n
dx = A
i 1
i i
Activity 1
Write a brute force algorithm to compare 25 text characters in an array
and match with one character.
If the array is already sorted in descending order then the worst case
occurs. "If A[j] < min y" is computed exactly the same number of times in
every case then the variation in time is only due to the number of times the
"then" part (i.e., min j ← j; min y ← A[j] of this test is executed.
Analysis
We can analyze selection sort very easily compared to other sorting
algorithms since none of the loops depend on the data that is present in the
array. For selecting the lowest element in the array we scan all n elements
(this takes n − 1 comparisons) and then swap it with the first position. For
finding the next lowest element we scan the remaining n − 1 element and so
on for (n − 1) + (n − 2) + ... + 2 + 1 = n (n − 1) / 2 Є Θ(n2) comparisons. Each
of these scans require one swap for n − 1 elements (because the final
element is already in place).
Comparison with other sorting algorithms
Amongst simple average-case Θ(n2) algorithms, selection sort always
outperforms bubble sort, insertion sort and gnome sort. Insertion sort's main
advantage is that it can only scan as many elements as it needs in order to
place the k + 1st element, while selection sort scans all remaining elements
to find the k + 1st element.
Another key difference which we can observe is that selection sort always
performs Θ(n) swaps while insertion sort performs Θ(n2) swaps in the
average and worst cases because generally swaps require writing to the
array. In such case, selection sort is more preferable.
5.3.2 Bubble sort
Definition – A bubble sort is a sorting algorithm that continuously moves
through a list swapping the items till they appear in a correct order. Bubble
sort is the simplest sorting algorithm.
We can execute bubble sort by iterating it down an array (that has to be
sorted) from the first element to the last and compare with each pair of
elements and switch their positions if required. We should repeat this
process till the array is sorted.
Performance of algorithm
Let us now analyze the performance of bubble sort algorithm. For analyzing
the algorithm, we assume an array containing elements to be sorted. Now
we will look through the array and pick the smallest element to put it in
position 1. This is the first pass. For second pass, we need to consider the
remaining list from the second element to the last in order to put the next
smallest element in position 2 and so on till we sort all the elements.
For instance, let us consider the array of numbers as A[4] = {4, 5, 3, 2}.
(A[0]=4, A[1]=5, A[2]=3, A[3]=2) Pictorial representation of how sorting (first
pass and second pass) is performed is shown in figure 5.2.
In the first pass of figure 5.2, we check for the smallest element in the array.
The smallest element is located at .A[3] i.e. ‘2’. Now we swap ‘2’ with ‘3’
(that is located at A[2]). Let us compare A[1] and A[2]. We find ‘2’ as the
smallest element. So we swap ‘2’ with ‘5’ (that is present in A[1]). Let us now
compare A[0] with A[1]. After comparison we swap 2 with the element that is
present in A[0]=’4’. Hence the order of elements in first pass is 2, 4, 3, 5.
In second pass of figure 5.2, we take the order of elements obtained from
the first pass i.e. 2 4 3 5. Let us compare A[3] with A[2] and swap the
smallest element. So ‘3’ is swapped with ‘5’. In the next step we compare
A[2] with A[1] and swap ‘2’ with ‘5’.In the last step, we compare A[1] with
A[0] and swap ‘2’ with ‘4’. Hence after sorting the elements, the order of the
elements is 2 3 4 5.
Algorithm analysis
In the above example, the outer loop runs n times. The complexity in this
analysis lies in the inner loop. If we run the inner loop for a single time, we
get a simple bound by noting that it can never loop more than n times. Since
the outer loop makes the inner loop to complete n times, we cannot
compare more than O(n2) times. This seems to be a very high bound
because when we run the inner loop for last time, it will only make one
comparison which is less than n. When we run the inner loop for first time, it
will make n-1 comparisons, then next time it will make n-2 comparisons; and
n1
so on. Therefore, the actual number of comparisons is k that has a value
k 1
of (n-1)n/2 which is also O(n2). Thus bubble sort has worst, best and
average case run-time of O(n2).
Let us now discuss the pseudocode of bubble sort for sorting an array of
integers.
Activity 2
Write an algorithm to sort four elements in an array list.
In this pseudocode implementation, we execute the last line only after all list
items are examined with none matching.
If we store the list as an array data structure then the location of the item at
the index of the list will be usually between 1 and n, or 0 and n−1. In such
case the invalid location (Λ) will be any index before the first element (such
as 0 or −1, respectively) or after the last element (n+1 or n, respectively).
Recursive version
We will next describe the recursive algorithm of sequential search.
Pseudocode for recursive version of sequential search algorithm
If the list is empty, return Λ;// Invalid location
else
if the first item of the list has the desired value, return its location;
else search the value in the remainder of the list and return the
result.
Analysis
A list with n items has best case when the value of n is equal to the first
element of the list and we do not need to do any comparisons in this case.
The worst case happens when the value is not in the list or appears only
once at the end of the list and in this case we need n comparisons.
When the value which we require occurs k times in the list then the
estimated number of comparisons are asymptotic. Hence O(n) is the worst-
case cost and also the expected cost of sequential search.
Searching the array elements
We program sequential search in an array by stepping up an index variable
until it reaches the last index. We normally require two comparison
instructions for each list item. One, we use for checking whether the index
has reached the end of the array and another for checking whether the item
contains the desired value.
Let us consider an array A with elements indexed from1 to n. We have to
search for a value x in the array. We can perform a forward search using
pseudocode and this code returns n + 1 if the value is not found. Let us now
see the pseudocode for forward search.
Let us now search array using pseudocode in the reverse order and return 0
when the element is not found.
Hope you are clear about the brute force method and the different
algorithms that use this method.
Self Assessment Questions
10. Exhaustive search implementation is more important than _________.
11. Exhaustive search algorithm gives the ______________ for every
candidate that is a solution to the given instance P.
12. Exhaustive search is typically used when the problem size is
___________.
5.6 Summary
It is very essential for us to obtain a best algorithm for any analysis. Brute
force method is a mathematical proof which helps in simplifying the finite
number of classes of each case and proves each case separately for
analysis.
We analyzed the performance of selection sort and bubble sort algorithms
and implemented the algorithms in pseudocode with suitable examples and
figures.
Sequential search and brute–force string matching algorithms are the
simplest algorithms to implement. In this unit we examined the performance
of sequential search algorithm in a systematic way. We implemented
sequential search algorithm in the following ways - forward iteration,
recursive version, searched in an ordered list and reverse order search.
Exhaustive search is a method which helps us in determining all the
possible candidates for the solutions and helps in verifying whether the
candidates satisfy the problem’s solution.
5.7 Glossary
Term Description
Pseudocode Pseudocode is an artificial and informal language that helps
programmers to develop algorithms.
Heuristics Heuristics is an adjective for experience-based techniques
that help in problem solving, learning and discovery.
Gnome sort Gnome sort is a sorting algorithm which is similar to insertion
sort but moves an element to its proper place by a series of
swaps as in bubble sort.
Substring Substring of a string is a subset of the symbols in a string
where order of the elements is preserved.
5.9 Answers
Self Assessment Questions
1. Feasible solution
2. Objective function
3. O(n2n).
4. Performance oriented
5. Worst
6. Bubble sort
7. Sequential search
8. Stepping up
9. Last line
10. Speed
11. Output
12. Limited
Terminal Questions
1. Refer section 5.2.1 – Brute-force algorithm
2. Refer section 5.3.1 – Selection sort
3. Refer section 5.3.2 – Bubble sort
4. Refer section 5.4.1 – Sequential search
5. Refer section 5.5.1 – Definition of exhaustive search
References
Rashid Bin Muhammad. Design and Analysis of Computer Algorithms.
E-Reference
http://caveshadow.com/CS566/Sabin%20M.%20Thomas%20-%20String
%20 Matching%20Algorithms.ppt
http://www.cse.unl.edu/~ylu/csce310/notes/BruteForce.ppt.
http://www.cs.miami.edu/~burt/learning/Csc517.051/notes/selection.html
http://cprogramminglanguage.net/c-bubble-sort-source-code.aspx
http://knol.google.com/k/bubble-sort#
cs.unco.edu/course/CS101/F06/Chapter09.ppt
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/
Sorting/selectionSort.htm
http://webspace.ship.edu/cawell/Sorting/bubanal.htm