Académique Documents
Professionnel Documents
Culture Documents
Objectives
To introduce the brute-force mind set To show a variety of brute-force solutions:
Brute-Force String Matching Polynomial Evaluation Closest-Pair Convex-Hull Exhaustive Search
Brute Force
A straightforward approach usually directly based on problem statement and definitions Motto: Just do it! Crude but often effective Examples already encountered:
Computing an (a > 0, n a nonnegative integer) by multiplying a together n times Computation of n! using recursion Multiplying two n by n matrices Selection sort
Brute-Force Sorting
What is the most straight-forward approach to sorting?
selection sort?
(n-1)n/2 O(n2) comparisons however, number of key swaps is n-1 O(n)
Bubble sort?
(n-1)n/2 - O(n2) comparisons number of key swaps depends on input Worst case?
4
Problem:
Find a substring in some text that matches a pattern Pattern: a string of m characters to search for Text: a (long) string of n characters to search in 1. Align pattern at beginning of text 2. Moving 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.
Number of Comparisons:
In the worst case, m comparisons before shifting, for each of n-m+1 tries
Efficiency: !(nm)
Problem:
Algorithm:
Efficiency? Improvements?
Problem:
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? Improvements?
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Efficiency: !(n2)
Algorithm:
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 ! ! ! dmin " d; index1 " i; index2 " j return index1, index2
Efficiency: !(n2)
NonConvex
Convex
NonConvex
Convex
NonConvex
Convex
NonConvex
Convex
NonConvex
Convex
P2 P3 P1 P5 P6 P7
P9
P8
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:
for n(n-1)/2 point pairs, check sidedness of (n-2) others O(n3)
" Weaknesses:
Rarely produces efficient algorithms Some brute force algorithms are infeasibly slow Not as creative as some other design techniques
Exhaustive Search
Definition:
A brute force solution to the search for an element with a special property Usually among combinatorial objects such a permutations or subsets suggests generating each and every element of the problems domain
Method:
1. Construct a way of listing all potential solutions to the problem in a systematic manner
All solutions are eventually listed No solution is repeated
2. Evaluate solutions one by one (disqualifying infeasible ones) keeping track of the best one found so far 3. When search ends, announce the winner
Alternatively:
Find shortest Hamiltonian Circuit in a weighted connected graph
Example:
8
b
3 4
Start and end at one particular city Remove tours that differ only in direction
Efficiency:
(n-1)!/2 = O(n!)
Knapsack Problem
Problem: Given n items
weights: w1 , w2 wn values: v1 , v2 vn a knapsack of capacity W Find the most valuable subset of the items that fit into the knapsack Item Weight Value Example (W = 16): 1 2 3 4 2kg 5kg 10kg 5kg R200 R300 R500 R100
Efficiency: !(2n)