230 likes | 546 Views
Theory of Algorithms: Brute Force. Outline. Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses. Brute Force. A straightforward approach usually directly based on problem statement and definitions Motto: Just do it!
E N D
Outline • Examples • Brute-Force String Matching • Closest-Pair • Convex-Hull • Exhaustive Search • brute-force strengths and weaknesses
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 String Matching • Find a substring in some text • Pattern: m characters to search for • Text: n characters to search in • Align pattern at beginning of text • compare each character • while pattern is not found and the text is not exhausted, move pattern one position to the right and repeat
Brute Force String Matching • 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 • Align pattern at beginning of text • 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 • WHILE pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat step 2.
String Matching • Example: • Pattern: AKA • Text: ABRAKADABRA • Trace: AKA AKA AKA AKA • Number of Comparisons: • In the worst case, m comparisons before shifting, for each of n-m+1 tries • Efficiency: (nm)
Brute Force Closest Pair • Problem: • Find the two points that are closest together in a set of n 2-D points P1 = (x1, y1), …, Pn = (xn, yn) • Using Cartesian coordinates and Euclidean distance • Algorithm: • Efficiency: (n2) dmin ∞ fori1ton-1do forj i+1tondo d sqrt((xi - xj)2 + (yi - yj)2) ifd < dmin dmin d; index1 i; index2 j returnindex1, index2
Brute Force Closest Pairs P4 P9 P2 • Find the two points that are closest together in a set P1 = (x1, y1), …, Pn = (xn, yn) using Euclidean distance P8 P7 P3 P5 P1 P6
The Convex Hull Problem • Find the convex hull enclosing n 2-D points • Convex Set: A set of points in the plane is convex if for any two points P and Q, the line segment joining P and Q belongs to the set • Convex Hull: If S is a set of points then the Convex Hull of S is the smallest convex set containing S Convex Non-Convex
Brute Force Convex Hull P4 P9 P2 Problem: given a set of points like those above, find its convex hull (the smallest convex set enclosing them all) P8 P7 P3 P5 P1 P6
Brute Force Convex Hull P4 P9 P2 The answer for this example is shown above. P8 P7 P3 P5 P1 P6
Brute Force Convex Hull P4 P9 P2 • Algorithm: • For each pair of points p1 and p2 determine if all other points lie to the same side of the line (drawn across the plane) through p1 and p2 • Efficiency: • for n(n-1)/2 point pairs, check (n-2) others • O(n3) P8 P7 P3 P5 P1 P6
Pros and Cons of Brute Force • Strengths: • Wide applicability • Simplicity • Yields reasonable algorithms for some important problems and standard algorithms for simple computational tasks • A good yardstick for better algorithms • Sometimes doing better is not worth the bother • 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 e.g. permutations or subsets • Method: • Find a systematic way of listing all potential solutions • All solutions eventually listed • No solution repeated • Evaluate solutions one by one • When search ends, announce the winner
Travelling Salesman Problem • 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 • Example: 2 a b 5 3 4 8 c d 7
Travelling Salesman Problem Tour Cost . abcda 2+3+7+5 = 17 abdca 2+4+7+8 = 21 acbda 8+3+4+5 = 20 acdba 8+7+4+2 = 21 adbca 5+4+3+8 = 20 adcba 5+7+3+2 = 17 2 a b 5 3 4 8 c d 7
Travelling Salesman by Exhaustive Search Tour Cost . abcda 2+3+7+5 = 17 abdca 2+4+7+8 = 21 acbda 8+3+4+5 = 20 acdba 8+7+4+2 = 21 adbca 5+4+3+8 = 20 adcba 5+7+3+2 = 17 • Improvements: • Start and end at one particular city • Remove tours that differ only in direction • Efficiency: • (n-1)!/2 = O(n!)
Knapsack Problem • Find the most valuable subset of items that fit in the knapsack, given n items • weights: w1 , w2 … wn • values: v1 , v2 … vn • a knapsack of capacity W • Example (W = 16):
Knapsack by Exhaustive Search • Efficiency: Ω(2n)
Generating Combinatorials: Subsets • Combinatorics uses Decrease (by one) and Conquer Algorithms • Subsets: generate all 2n subsets of A = {a1, …, an} • Divide into subsets of {a1, …, an-1} that contain an and those that don’t • Sneaky Solution: establish a correspondence between bit strings and subsets. Bit n denotes presence (1) or absence (0) of element an • Generate numbers from 0 to 2n-1 convert to bit strings interpret as subsets • Examples: 000 = Ø , 010 = {a2} , 110 = {a1, a2}
Generating Combinatorials: Permutations • Permutations: generate all n! reorderings of {1, …, n} • Generate all (n-1)! permutations of {1, …, n-1} • Insert n into each possible position (starting from the right or left, alternately) • Implemented by the Johnson-Trotter algorithm • Satisfies Minimal-Change requirement • Next permutation obtained by swapping two elements of previous • Useful for updating style algorithms • Example: • Start: 1 • Insert 2: 12 21 • Insert 3: 123 132 312 321 231 213
Final Comments on Exhaustive Search • run in a realistic amount of time only on very small instances • Often there are much better alternatives! • Euler circuits • Shortest paths • Minimum spanning tree • Assignment problem • In some cases exhaustive search is the only known solution