Introduction to Algorithms BruteForce Algorithms Introduction to Algorithms

Introduction to Algorithms: Brute-Force Algorithms

Introduction to Algorithms Brute Force • Powering a Number • Selection Sort • Exhaustive Search • 0/1 Knapsack Problem • Assignment Problem 2

Brute-Force Algorithm Design • Straightforward, usually based on problem definition. • Rarely the most efficient but can be applied to wide range of problems. • For some elementary problems, almost as good as most efficient. May not be worth cost. • May work just as well on smaller data sets. • Used to measure other algorithms against. 3

Calculating Powers of a Number Problem: Compute a n, where n N. Naive algorithm: (n). an= n * n * …* n a times CS 421 - Analysis of Algorithms 4

Selection Sort Given a list of n orderable items, rearrange them in nondecreasing order. 1. Scan entire list to find smallest item. 2. Exchange it with first item. First element now in its final, sorted position. 3. Scan remaining n – 1 items, starting with second element, and find smallest item. 4. Exchange it with second item. Second element now in its final, sorted position. 5. Repeat for a total of n – 1 times. 5

Selection Sort Example Selection sort on the list: 89, 45, 68, 90, 29, 34, 17. Each line corresponds to an iteration of the algorithm. The values in bold are the smallest item for that iterations. Elements to the left of the vertical bar are in their final positions. | 89 45 68 90 29 34 17 17 |45 68 90 29 34 89 17 29 | 68 90 45 34 89 17 29 34 | 90 45 68 89 17 29 34 45 | 90 68 89 17 29 34 45 68 | 90 89 17 29 34 45 68 89 | 90 CS 421 - Analysis of Algorithms 6

Selection Sort Analysis Selection sort is implemented using nested for loops: Outer loop: iterates from 0 to n – 2 – don’t have to visit element because already in final sorted position Inner loop: finds smallest value remaining the list. Even though gets smaller each iteration, still on order of n. Therefore: T(n) = (n 2) CS 421 - Analysis of Algorithms 7

Exhaustive Search Brute-force approach to combinatorial problems (i. e. permutations, combinations, subsets of a given set). 1. Generate all elements of problem domain (all possible solutions). 2. Select those that satisfy the constraints of the problem. 3. Chose one or more that are most desirable (i. e. optimize the objective function). 8

0/1 Knapsack Problem Given n items of known weights w 1, w 2, …, wn and values v 1, v 2, …, vn , and a knapsack of capacity W, find most valuable subset of items that will fit. 1. Generate all subsets of n items. 2. Calculate the weights of each and eliminate all the infeasible solutions. 3. Find the subset with the maximum value. 9

0/1 Knapsack Problem Analysis The most costly operation is generating all of the subsets of n items. Since there are 2 n subsets, Brute-force Approach to Knapsack problem: Ω(2 n). Not feasible for any but the smallest values of n. 10

Assignment Problem There are n jobs that need to be completed, and n people that need to be assigned to a job, one person per job. The cost for the i th person to perform job j is known C[i, j]. Find the assignment with the lowest cost. 1. Generate all permutations of n people assigned to n jobs. 2. Calculate the cost of each permutation/solution. 3. Find the with the minimum value. CS solution 421 - Analysis of Algorithms 11

Assignment Problem Analysis The most costly operation is generating all of the permutations. Since there are n! permutations, Brute-force Approach to Assignment problem: Θ(n!). Not feasible for any but the smallest values of n. 12

Conclusion The Brute Force approach: • Straightforward approach to solving problem, usually based directly on problem description. • Wide applicability and simple. • But in general, has subpar performance. 13
- Slides: 13