Presentation is loading. Please wait.

Presentation is loading. Please wait.

Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.

Similar presentations


Presentation on theme: "Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August."— Presentation transcript:

1 Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | edwin} @cs.uct.ac.za Department of Computer Science University of Cape Town August - October 2004

2 Objectives lTo introduce the brute force mind set lTo show a variety of brute-force solutions:  Brute-Force String Matching  Polynomial Evaluation  Closest-Pair and Convex-Hull by Brute Force  Exhaustive Search lTo discuss the strengths and weaknesses of a brute force strategy

3 Brute Force lA straightforward approach usually directly based on problem statement and definitions lMotto: Just do it! lCrude but often effective lExamples 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 lProblem: Find a substring in some text that matches a pattern lPattern: a string of m characters to search for lText: a (long) string of n characters to search in lAlgorithm: 1.Align pattern at beginning of text 2.Moving left to right, compare each character of pattern to the corresponding character in text UNTIL lAll characters are found to match (successful search); or lA 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.

5 Understanding String Matching lExample:  Pattern: AKA  Text: ABRAKADABRA  Trace: AKA AKA lNumber of Comparisons:  In the worst case, m comparisons before shifting, for each of n-m+1 tries lEfficiency:  (nm)

6 Brute Force Polynomial Evaluation lProblem: Find the value of polynomial p(x) = a n x n + a n-1 x n-1 +… + a 1 x 1 + a 0 at a point x = x 0 lAlgorithm: lEfficiency? 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

7 Brute Force Closest Pair lProblem:  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 lAlgorithm: lEfficiency:  (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 The Convex Hull Problem lProblem: Find the convex hull enclosing n 2-D points lConvex Hull: If S is a set of points then the Convex Hull of S is the smallest convex set containing S lConvex 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 Non-Convex

9 Brute Force Convex Hull lAlgorithm:  For each pair of points p 1 and p 2  Determine whether all other points lie to the same side of the straight line through p 1 and p 2 lEfficiency:  for n(n-1)/2 point pairs, check sidedness of (n-2) others  O(n 3 ) P1P1 P2P2 P3P3 P4P4 P5P5 P6P6 P7P7 P8P8 P9P9

10 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 infeasibly slow  Not as creative as some other design techniques

11 Exhaustive Search lDefinition:  A brute force solution to the search for an element with a special property,  Usually among combinatorial objects such a permutations, combinations, or subsets of a set lMethod: 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

12 Travelling Salesman Problem lProblem:  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 lAlternatively:  Find shortest Hamiltonian Circuit in a weighted connected graph lExample: ab cd 8 2 7 53 4

13 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 lImprovements:  Start and end at one particular city  Remove tours that differ only in direction lEfficiency: (n-1)!/2 =  (n!)

14 Knapsack Problem lProblem: Given n items  weights: w 1 w 2 … w n  values: v 1 v 2 … v n  a knapsack of capacity W  Find the most valuable subset of the items that fit into the knapsack lExample (W = 16): ItemWeightValue 12kgR200 25kgR300 310kgR500 45kgR100

15 Knapsack by Exhaustive Search lEfficiency: Ω(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

16 Generating Combinatorial Objects: Subsets lCombinatorics uses Decrease (by one) and Conquer Algorithms lSubsets: 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 }

17 Generating Combinatorial Objects: Permutations lPermutations: 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 lSatisfies Minimal-Change requirement  Next permutation obtained by swapping two elements of previous  Useful for updating style algorithms lExample:  Start: 1  Insert 2: 12 21  Insert 3: 123 132 312 321 231 213

18 Final Comments on Exhaustive Search lExhaustive search algorithms run in a realistic amount of time only on very small instances lIn many cases there are much better alternatives!  Euler circuits  Shortest paths  Minimum spanning tree  Assignment problem lIn some cases exhaustive search (or variation) is the only known solution


Download ppt "Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August."

Similar presentations


Ads by Google