Presentation is loading. Please wait.

Presentation is loading. Please wait.

Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.

Similar presentations


Presentation on theme: "Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses."— Presentation transcript:

1 Theory of Algorithms: Brute Force

2 Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses

3 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 a n (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

4 Brute Force String Matching Find a substring in some text Pattern: m characters to search for Text: n characters to search in 1.Align pattern at beginning of text 2.compare each character 3.while pattern is not found and the text is not exhausted, move pattern one position to the right and repeat

5 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 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.

6 String Matching Example: Pattern: AKA Text: ABRA KADABRA Trace: A KA A KA Number of Comparisons: In the worst case, m comparisons before shifting, for each of n-m+1 tries Efficiency:  (nm)

7 Brute Force Closest Pair Problem: Find the two points that are closest together in a set of n 2-D points P 1 = (x 1, y 1 ), …, P n = (x n, y n ) Using Cartesian coordinates and Euclidean distance Algorithm: Efficiency:  (n 2 ) dmin  ∞ for i  1 to n-1 do for j  i+1 to n do d  sqrt((x i - x j ) 2 + (y i - y j ) 2 ) if d < dmin dmin  d; index1  i; index2  j return index1, index2

8 Brute Force Closest Pairs Find the two points that are closest together in a set P 1 = (x 1, y 1 ), …, P n = (x n, y n ) using Euclidean distance P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 P8P8 P9P9

9 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

10 Brute Force Convex Hull Problem: given a set of points like those above, find its convex hull (the smallest convex set enclosing them all) P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 P8P8 P9P9

11 Brute Force Convex Hull The answer for this example is shown above. P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 P8P8 P9P9

12 Brute Force Convex Hull Algorithm: For each pair of points p 1 and p 2 determine if all other points lie to the same side of the line (drawn across the plane) through p 1 and p 2 Efficiency: for n(n-1)/2 point pairs, check (n-2) others O(n 3 ) P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 P8P8 P9P9

13 Pros and Cons of Brute Force JStrengths: 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

14 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: 1.Find a systematic way of listing all potential solutions All solutions eventually listed No solution repeated 2.Evaluate solutions one by one 3.When search ends, announce the winner

15 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: ab cd 8 2 7 5 3 4

16 Travelling Salesman Problem Tour Cost. a  b  c  d  a 2+3+7+5 = 17 a  b  d  c  a 2+4+7+8 = 21 a  c  b  d  a 8+3+4+5 = 20 a  c  d  b  a 8+7+4+2 = 21 a  d  b  c  a 5+4+3+8 = 20 a  d  c  b  a 5+7+3+2 = 17 ab cd 8 2 7 5 3 4

17 Travelling Salesman by Exhaustive Search Tour Cost. a  b  c  d  a 2+3+7+5 = 17 a  b  d  c  a 2+4+7+8 = 21 a  c  b  d  a 8+3+4+5 = 20 a  c  d  b  a 8+7+4+2 = 21 a  d  b  c  a 5+4+3+8 = 20 a  d  c  b  a 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!)

18 Knapsack Problem Find the most valuable subset of items that fit in the knapsack, given n items weights: w 1, w 2 … w n values: v 1, v 2 … v n a knapsack of capacity W Example (W = 16): ItemWeightValue 12kgR200 25kgR300 310kgR500 45kgR100

19 Knapsack by Exhaustive Search Efficiency: Ω(2 n ) SubsetTotal Wgt Total Value {1} 2 kgR200 {2} 5 kgR300 {3} 10 kgR500 {4} 5 kgR100 {1,2} 7 kgR500 {1,3} 12 kgR700 {1,4} 7 kgR300 {2,3} 15 kgR800 SubsetTotal Wgt Total Value {2,4} 10 kgR400 {3,4} 15 kgR600 {1,2,3} 17 kgn/a {1,2,4} 12 kgR600 {1,3,4} 17 kgn/a {2,3,4} 20 kgn/a {1,2,3,4} 22 kgn/a

20 Generating Combinatorials: Subsets Combinatorics uses Decrease (by one) and Conquer Algorithms Subsets: generate all 2 n subsets of A = {a 1, …, a n } Divide into subsets of {a 1, …, a n-1 } that contain a n 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 a n Generate numbers from 0 to 2n-1  convert to bit strings  interpret as subsets Examples: 000 = Ø, 010 = {a 2 }, 110 = {a 1, a 2 }

21 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

22 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


Download ppt "Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses."

Similar presentations


Ads by Google