Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing."— Presentation transcript:

1 Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing a n (a, n > 0, n integer) 2. GCD with consecutive integer checking 3. Computing n! 4. Multiply two n by n matrices 5. Selection sort 6. Sequential search

2 Design and Analysis of Algorithms - Chapter 32 Bubble Sort Algorithm BubbleSort(A[0..n-1]) // Input: an array A[0..n-1] of orderable elements // Output: Array A[0..n-1] sorted ascendingly for i  0 to n-2 do for j  0 to n-2-i do if A[j+1]<A[j] swap A[j] and A[j+1] Analysis for comparisons and swaps Improvements

3 Design and Analysis of Algorithms - Chapter 33 String matching b pattern: a string of m characters to search for b text: a (long) string of n characters to search in b 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.

4 Design and Analysis of Algorithms - Chapter 34 Brute force string matching – Algorithm Algorithm BruteForceStringMatch(T[0..n-1], P[0..m-1]) // Input: array T[0..n-1] for text and array P[0..m-1] for pattern // Output: the position of the first character in the text stat starts the first matching substring if the search is successful, and -1 otherwise for i  0 to n-m do j  0 while j<m and P[j]=T[i+j] do j  j+1 if j=m return I return -1

5 Design and Analysis of Algorithms - Chapter 35 Brute force string matching – Examples: 1. Pattern: 001011 Text: 10010101101001100101111010 1. Pattern: happy Text: It is never too late to have a happy childhood. Number of comparisons: Efficiency:

6 Design and Analysis of Algorithms - Chapter 36 Brute force polynomial evaluation b Problem: 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 b 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 b Efficiency:

7 Design and Analysis of Algorithms - Chapter 37 Polynomial evaluation: improvement b Improvement: evaluation from right to left: b Algorithm: p := a[0] power := 1 for i := 1 to n do power := power * x p := p + a[i] * power return p b Efficiency:

8 Design and Analysis of Algorithms - Chapter 38 Closest Pair Problem b Problem: find closest points among n ones in k-dimensional space b Algorithm: // Input: A list P of n>1 points P 1 =(x 1,y 1 ), …, P n =(x n,y n ) //Output: Indices id 1 and id 2 of the closest pair of points 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; id1  i; id2  j; return id1, id2 b Efficiency

9 Design and Analysis of Algorithms - Chapter 39 The Convex Hull Problem b Problem: find smallest convex polygon enclosing n points on the plane b Algorithm: 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 b Efficiency:

10 Design and Analysis of Algorithms - Chapter 310 Brute force strengths and weaknesses b Strengths: wide applicabilitywide applicability simplicitysimplicity yields reasonable algorithms for some important problemsyields reasonable algorithms for some important problems –searching, string matching, matrix multiplication yields standard algorithms for simple computational tasksyields standard algorithms for simple computational tasks –sum/product of n numbers, finding max/min in a list b Weaknesses: rarely yields efficient algorithmsrarely yields efficient algorithms some brute force algorithms unacceptably slowsome brute force algorithms unacceptably slow not as constructive/creative as some other design techniquesnot as constructive/creative as some other design techniques

11 Design and Analysis of Algorithms - Chapter 311 Exhaustive search b Exhaustive search is a brute force approach when searching for an element with a special property, usually among combinatorial objects such a permutations, combinations, or subsets of a set. b Method: construct a way of listing all potential solutions to the problem in a systematic mannerconstruct 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, disqualifying infeasible ones and keeping track of the best one found so farEvaluate solutions one by one, disqualifying infeasible ones and keeping track of the best one found so far When search ends, announce the winnerWhen search ends, announce the winner

12 Design and Analysis of Algorithms - Chapter 312 Example 1: Traveling salesman problem b 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. b Alternatively: Find shortest Hamiltonian circuit in a weighted connected graph. b Example: ab cd 8 2 7 53 4

13 Design and Analysis of Algorithms - Chapter 313 Traveling salesman by exhaustive search b 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 b Efficiency:

14 Design and Analysis of Algorithms - Chapter 314 Example 2: Knapsack Problem Problem: Given n items with weights w 1,w 2,…,w n and values v 1,v 2,…,v n and a knapsack of capacity W Find the most valuable subset of the items that fit into the knapsack Example: item weight value Knapsack capacity W=16 1 2 $20 2 5 $30 3 10 $50 4 5 $10

15 Design and Analysis of Algorithms - Chapter 315 Knapsack by exhaustive search Subset Total weight Total value {1} 2 $20 {1} 2 $20 {2} 5 $30 {2} 5 $30 {3} 10 $50 {3} 10 $50 {4} 5 $10 {4} 5 $10 {1,2} 7 $50 {1,2} 7 $50 {1,3} 12 $70 {1,3} 12 $70 {1,4} 7 $30 {1,4} 7 $30 {2,3} 15 $80 {2,3} 15 $80 {2,4} 10 $40 {2,4} 10 $40 {3,4} 15 $60 {3,4} 15 $60 {1,2,3} 17 not feasible {1,2,3} 17 not feasible {1,2,4} 12 $60 {1,2,4} 12 $60 {1,3,4} 17 not feasible {1,3,4} 17 not feasible {2,3,4} 20 not feasible {2,3,4} 20 not feasible {1,2,3,4} 22 not feasible Efficiency:

16 Design and Analysis of Algorithms - Chapter 316 Assignment by exhaustive search Problem: Given n people and n jobs, whereas C[i,j] is the cost if person i takes job j Assign one person per job with the minimum cost Example: Job 1 Job 2 Job 3 Job 4 Person 1 9278 Person 2 6437 Person 3 5818 Person 4 7694

17 Design and Analysis of Algorithms - Chapter 317 Assignment by exhaustive search AssignmentTotal cost 9+4+1+4=18 9+4+1+4=18 9+4+8+9=30 9+4+8+9=30 9+3+8+4=24 9+3+8+4=24 9+3+8+6=26 9+3+8+6=26 9+7+8+9=33 9+7+8+9=33 9+7+1+6=23 9+7+1+6=23 ………………………. Efficiency:

18 Design and Analysis of Algorithms - Chapter 318 Final comments: b Exhaustive search algorithms run in a realistic amount of time only on very small instances b In many cases there are much better alternatives! Euler circuitsEuler circuits shortest pathsshortest paths minimum spanning treeminimum spanning tree assignment problemassignment problem b In some cases exhaustive search (or variation) is the only known solution


Download ppt "Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing."

Similar presentations


Ads by Google