Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms: Brute-Force Algorithms.

Similar presentations


Presentation on theme: "Introduction to Algorithms: Brute-Force Algorithms."— Presentation transcript:

1 Introduction to Algorithms: Brute-Force Algorithms

2 Introduction to Algorithms Brute Force Powering a Number Selection Sort Exhaustive Search 0/1 Knapsack Problem Assignment Problem CS 421 - Analysis of Algorithms 2

3 Brute-Force Algorithm Design CS 421 - Analysis of Algorithms 3 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.

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

5 Selection Sort CS 421 - Analysis of Algorithms Given a list of n orderable items, rearrange them in non- decreasing 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

6 Selection Sort - Example CS 421 - Analysis of Algorithms 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. 6 | 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

7 Selection Sort - Analysis CS 421 - Analysis of Algorithms 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 ) 7

8 Exhaustive Search CS 421 - Analysis of Algorithms 8 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).

9 0/1 Knapsack Problem CS 421 - Analysis of Algorithms 9 Given n items of known weights w 1, w 2, …, w n and values v 1, v 2, …, v n, 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.

10 0/1 Knapsack Problem Analysis CS 421 - Analysis of Algorithms 10 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.

11 Assignment Problem CS 421 - Analysis of Algorithms 11 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 solution with the minimum value.

12 Assignment Problem Analysis CS 421 - Analysis of Algorithms 12 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.

13 Conclusion CS 421 - Analysis of Algorithms 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


Download ppt "Introduction to Algorithms: Brute-Force Algorithms."

Similar presentations


Ads by Google