Brute Force: Design and Analysis of Algorithms - Chapter 3 1
Brute Force: Design and Analysis of Algorithms - Chapter 3 1
A straightforward approach usually based on problem statement and definitions Examples: 1. Computing an (a, n > 0, n integer) 2. GCD with consecutive integer checking 3. Computing n! 4. Multiply two n by n matrices 5. Selection sort 6. Sequential search
Design and Analysis of Algorithms - Chapter 3 1
Bubble Sort
Algorithm BubbleSort(A[0..n-1]) // Input: an array A[0..n-1] of orderable elements // Output: Array A[0..n-1] sorted ascendingly for i 0 to n-2 do for j 0 to n-2-i do if A[j+1]<A[j] swap A[j] and A[j+1] Analysis for comparisons and swaps Improvements
Design and Analysis of Algorithms - Chapter 3 2
String matching
pattern: a string of m characters to search for text: a (long) string of n characters to search in Brute force algorithm:
1. Align pattern at beginning of text 2. moving from left to right, compare each character of pattern to the corresponding character in text until all characters are found to match (successful search); or a mismatch is detected 3. while pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat step 2.
Design and Analysis of Algorithms - Chapter 3 3
for i 0 to n-m do j0 while j<m and P[j]=T[i+j] do j j+1 if j=m return I return -1
Design and Analysis of Algorithms - Chapter 3 4
Pattern: 001011 Text: 10010101101001100101111010 Pattern: happy Text: It is never too late to have a happy childhood.
1.
Problem: find closest points among n ones in k-dimensional space Algorithm: // Input: A list P of n>1 points P1=(x1,y1), , Pn=(xn,yn) //Output: Indices id1 and id2 of the closest pair of points dmin for i 1 to n-1 do for j i+1 to n do d sqrt[(xi-xj)2+(yi-yj)2] if d<dmin dmind; id1i; id2j; return id1, id2 Efficiency
Design and Analysis of Algorithms - Chapter 3 8
find smallest convex polygon enclosing n points on the plane Algorithm: For each pair of points p1 and p2 determine whether all other points lie to the same side of the straight line through p1 and p2 Efficiency:
Strengths:
wide applicability simplicity yields reasonable algorithms for some important problems
searching, string matching, matrix multiplication
Weaknesses:
rarely yields efficient algorithms some brute force algorithms unacceptably slow not as constructive/creative as some other design techniques
Design and Analysis of Algorithms - Chapter 3 10
Exhaustive search
Exhaustive search is a brute force approach when searching for an element with a special property, usually among combinatorial objects such a permutations, combinations, or subsets of a set. Method:
construct a way of listing all potential solutions to the problem in a systematic manner
all solutions are eventually listed no solution is repeated
Evaluate solutions one by one, disqualifying infeasible ones and keeping track of the best one found so far When search ends, announce the winner
Design and Analysis of Algorithms - Chapter 3
11
5 8 3 4
d
12
13
14
Efficiency:
15
Efficiency:
17
Final comments:
Exhaustive search algorithms run in a realistic amount of time only on very small instances In many cases there are much better alternatives!
In some cases exhaustive search (or variation) is the only known solution
18