Download Design and Analysis of Algorithms - Chapter 3: Brute Force Algorithms and more Study notes Algorithms and Programming in PDF only on Docsity! 1 Design and Analysis of Algorithms Chapter 3 Design and Analysis of Algorithms - Chapter 3 1 http://www.cse.unl.edu/~goddard/Courses/CSCE310J Brute Force Dr. Steve Goddard goddard@cse.unl.edu Design and Analysis of Algorithms - Chapter 3 2 Giving credit where credit is due: • Most of the lecture notes are based on the slides from the Textbook’s companion website – http://www.aw.com/cssuport/ • I have modified them and added new slides Design and Analysis of Algorithms - Chapter 3 3 A straightforward approach usually based on problem statement and definitions Examples: 1. Computing an (a > 0, n a nonnegative integer) 2. Computing n! 3. Multiply two n by n matrices 4. Selection sort 5. Sequential search Design and Analysis of Algorithms - Chapter 3 4 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 5 1. Pattern: Text: 2. Pattern: Text: Number of comparisons: Efficiency: Design and Analysis of Algorithms - Chapter 3 6 ! " Problem: Find the value of polynomial p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0 Algorithm: Efficiency: 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 2 Design and Analysis of Algorithms Chapter 3 Design and Analysis of Algorithms - Chapter 3 7 #! " " We can do better by evaluating from right to left: Algorithm: Efficiency: p := a[0] power := 1 for i := 1 to n do power := power * x p := p + a[i] * power return p Design and Analysis of Algorithms - Chapter 3 8 $ % Closest pair • Problem: find closest among n points in k-dimensional space • Algorithm: Compute distance between each pair of points • Efficiency: Convex hull • 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 9 &' ( 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 " A brute force solution to a problem involving search 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, perhaps 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 ) " % 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: a b c d 8 2 7 5 3 4 Design and Analysis of Algorithms - Chapter 3 12 ) " %! " 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 Efficiency: