This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Designing Algorithms Brute F B t Force
Dr. Khaled Bashir Shaban i Brute Force
A straightforward approach, usually based directly on the problem's statement and definitions of the concepts involved Examples: 1. Computing an (a > 0, n a nonnegative integer)
2. Computing n! Multiplying two matrices Searching for a key of a given value in a list 3. 4. BruteBruteForce Sorting Algorithm
Selection Sort Scan the array to find its smallest element and
swap it with the first element. Then, starting with the second element, scan the elements to the right of it to find the smallest among them and swap it with the second elements elements. Generally, on pass i (0 i n2), find the smallest element in A[i..n1] and swap it with A[i]: i..n A[0] . . . A[i1]  A[i], . . . , A[min], . . ., A[n1]
in their final positions Example: 7 3 2 5 Analysis of Selection Sort Time efficiency: (n2) BruteBruteForce String Matching
pattern: a string of m characters to search for text: a (longer) string of n characters to search in
problem: find a substring in the text that matches the pattern BruteBruteforce algorithm Step Align tt St 1 Ali pattern at beginning of text tb i i ft t Step 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 Step 3 While pattern is not found and the text is not yet exhausted, realign pattern one p g p position to the right and g repeat Step 2 Examples of BruteForce String Matching Brute1. Pattern: 001011 Text: 10010101101001100101111010 2. Pattern: happy Text: i T t It is never too late to have a happy childhood. t l t t h h hildh d Pseudocode and Efficiency Efficiency: Worst case (nm) y ( ) BruteBruteForce Polynomial Evaluation
Problem: Find the value of polynomial p(x) = anxn + an1xn1 +... + a1x1 + a0 at a point x = x0 BruteBruteforce algorithm p 0.0 for i n downto 0 do power 1 p for j 1 to i do //compute xi power power x p p + a[i] power return p
Efficiency: (n2) Polynomial Evaluation: Improvement
We can do better by evaluating from right to left: Better bruteforce algorithm brutep a[0] power 1 for f i1t nd to do power power x p p + a[i] power return p Efficiency: (n) ClosestClosestPair Problem
Find the two closest points in a set of n points (in the twotwodimensional Cartesian plane). BruteBruteforce algorithm Compute the distance between every pair of distinct points and return the indexes of the points for which the distance is the smallest. ClosestClosestPair BruteForce Algorithm (cont.) Brute Efficiency: (n2) BruteBruteForce Strengths and Weaknesses
Strengths wide applicability simplicity yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting, searching, string matching) Weaknesses rarely yields efficient algorithms some bruteforce algorithms are unacceptably slow brute not as constructive as some other design techniques Exhaustive Search
A brute force solution to a problem involving search for an element with a special property usually among property, combinatorial objects such as permutations, combinations, or subsets of a set. Method: generate a list of all potential solutions to the problem in a systematic manner evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far when search ends announce the solution(s) found ends, Example 1: Traveling Salesman Problem
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 i a A i i i i i i in weighted connected graph Example: E l
2 a
5 8 3 b
4 c 7 d TSP by Exhaustive Search
Tour abcda abdca acbda acdba adbca adcba Efficiency: ( !) Effi i (n!) Cost 2+3+7+5 = 17 2+4+7+8 = 21 8+3+4+5 = 20 8+7+4+2 = 21 5+4+3+8 = 20 5+7+3+2 = 17 Example 2: Knapsack Problem
Given n items: weights: w1 w2 ... wn values: v1 v2 ... vn ak knapsack of capacity W k f it Find most valuable subset of the items that fit into the knapsack Example: Knapsack capacity W=16 W=16 item weight i i h value l 1 2 $20 2 5 $30 3 10 $50 4 5 $10 Knapsack Problem by Exhaustive Search
Subset Total weight
{1} {2} {3} {4} {1,2} {1,3} {1,4} {2,3} {2,4} {3,4} {1,2,3} {1,2,4} {1,3,4} {2,3,4} {1,2,3,4} 2 5 10 5 7 12 7 15 10 15 17 12 17 20 22 Total value
$20 $30 $50 $10 $50 $70 $30 $80 $40 $60 not feasible $60 not feasible not feasible not feasible Efficiency: (n2) Example 3: The Assignment Problem
There are n people who need to be assigned to n jobs, one person per job The cost of assigning person i to job j is C[i,j] job. ]. Find an assignment that minimizes the total cost.
Person 0 Person 1 Person 2 Person 3 Job 0 Job 1 Job 2 Job 3 9 2 7 8 6 4 3 7 5 8 1 8 7 6 9 4 Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one. How many assignments are there? Pose the problem as the one about a cost matrix: p Assignment Problem by Exhaustive Search
C= 5 8 1 8
9 2 7 8 6 4 3 7 7 6 9 4 Assignment (col.#s) col.#s) 1, 2, 3, 4 1, 2, 4, 3 1, 3, 2, 4 1, 3, 4, 2 1, 4, 2, 3 1, 4, 3, 2 Total Cost 9+4+1+4=18 9+4+8+9=30 9+3+8+4=24 9+3+8+6=26 9+7+8+9=33 9+7+1+6=23 etc. Final Comments on Exhaustive Search
ExhaustiveExhaustivesearch algorithms run in a realistic amount of time only on very small instances In some cases, there are much better alternatives! shortest paths minimum spanning tree assignment problem In many cases, exhaustive search or its variation is the only known way to get exact solution ...
View Full
Document
 Spring '11
 Shaban

Click to edit the document details