Brute Force
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
Brute force string matching Algorithm
Algorithm BruteForceStringMatch(T[0..n-1], P[0..m-1]) // Input: array T[0..n-1] for text and array P[0..m-1] for pattern
// Output: the position of the first character in the text stat starts the first matching substring if the search is successful, and -1 otherwise
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
Brute force string matching Examples:
1.
Pattern: 001011 Text: 10010101101001100101111010 Pattern: happy Text: It is never too late to have a happy childhood.
1.
Number of comparisons: Efficiency:
Design and Analysis of Algorithms - Chapter 3 5
Brute force polynomial evaluation
Problem: Find the value of polynomial p(x) = anxn + an-1xn-1 + + a1x1 + a0 at a point x = x0 Algorithm p := 0.0 for i := n down to 0 do power := 1 for j := 1 to i do power := power * x p := p + a[i] * power return p Efficiency:
Design and Analysis of Algorithms - Chapter 3 6
Polynomial evaluation: improvement
Improvement: evaluation from right to left: Algorithm: p := a[0] power := 1 for i := 1 to n do power := power * x p := p + a[i] * power return p Efficiency:
Design and Analysis of Algorithms - Chapter 3
Closest Pair Problem
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
The Convex Hull Problem
Problem:
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:
Design and Analysis of Algorithms - Chapter 3
Brute force strengths and weaknesses
Strengths:
wide applicability simplicity yields reasonable algorithms for some important problems
searching, string matching, matrix multiplication
yields standard algorithms for simple computational tasks
sum/product of n numbers, finding max/min in a list
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
Example 1: Traveling salesman problem
Given n cities with known distances between each pair, find the shortest tour that passes through all the cities exactly once before returning to the starting city. Alternatively: Find shortest Hamiltonian circuit in a weighted connected graph. 2 Example: a b
5 8 3 4
d
12
Design and Analysis of Algorithms - Chapter 3
Traveling salesman by exhaustive search
Tour abcda abdca acbda acdba adbca adcba Efficiency:
Cost 2+3+7+5 = 17 2+4+7+8 = 21 8+3+4+5 = 20 8+7+4+2 = 21 5+4+3+8 = 20 5+7+3+2 = 17
Design and Analysis of Algorithms - Chapter 3
13
Example 2: Knapsack Problem
Problem: Given n items with 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 Example: item weight 1 2 2 5 3 10 4 5
value $20 $30 $50 $10
Knapsack capacity W=16
Design and Analysis of Algorithms - Chapter 3
14
Knapsack by exhaustive search
Subset Total weight {1} 2 {2} 5 {3} 10 {4} 5 {1,2} 7 {1,3} 12 {1,4} 7 {2,3} 15 {2,4} 10 {3,4} 15 {1,2,3} 17 {1,2,4} 12 {1,3,4} 17 {2,3,4} 20 {1,2,3,4} 22 Total value $20 $30 $50 $10 $50 $70 $30 $80 $40 $60 not feasible $60 not feasible not feasible not feasible
Efficiency:
Design and Analysis of Algorithms - Chapter 3
15
Assignment by exhaustive search
Problem: Given n people and n jobs, whereas C[i,j] is the cost if person i takes job j Assign one person per job with the minimum cost Example: Job 1 Job 2 Job 3 Job 4
Person 1 9 Person 2 6 Person 3 5 Person 4 7 2 4 8 6 7 3 1 9 8 7 8 4
16
Design and Analysis of Algorithms - Chapter 3
Assignment by exhaustive search
Assignment <1,2,3,4> <1,2,4,3> <1,3,2,4> <1,3,4,2> <1,4,2,3> <1,4,3,2> Total cost 9+4+1+4=18 9+4+8+9=30 9+3+8+4=24 9+3+8+6=26 9+7+8+9=33 9+7+1+6=23 .
Design and Analysis of Algorithms - Chapter 3
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!
Euler circuits shortest paths minimum spanning tree assignment problem
In some cases exhaustive search (or variation) is the only known solution
Design and Analysis of Algorithms - Chapter 3
18