my c book

download my c book

of 24

Transcript of my c book

  • 7/29/2019 my c book

    1/24

    Chapter 3

    Brute Force

    Copyright 2007 Pearson Addison-Wesley. All rights reserved.

  • 7/29/2019 my c book

    2/24

    Brute Force

    A straightforward approach, usually based directly on the

    problems statement and definitions of the concepts involved

    Examples:

    1. Computing an (a > 0, n a nonnegative integer)

    2. Computing n!

    3. Multiplying two matrices

    4. Searching for a key of a given value in a list

  • 7/29/2019 my c book

    3/24

    Brute-Force Sorting Algorithm

    Selection Sort1. Scan the array to find its smallest element and swap it with the first

    element.

    2. Then, starting with the second element, scan the elements to its right to

    find the smallest among them and swap it with the second elements.

    3. Generally, on pass i (0 i n-2), find the smallest element inA[i..n-1]and swap it withA[i]:

    A[0] . . . A[i-1] | A[i], . . . ,A[min], . . .,A[n-1]in their final positions

  • 7/29/2019 my c book

    4/24

    Selection Sort & Bubble Sort

    Selection Sort1. Start with the 1st element, scan the entire list to find its smallest element and

    exchange it with the 1st element

    2. Start with the 2nd element, scan the remaining list to find the the smallestamong the last (N-1) elements and exchange it with the 2nd element

    3.

    Example: 89 45 68 90 29 34 17

    17 | 45 68 90 29 34 89

    29 | 68 90 45 34 89

    34 | 90 45 68 89

    45 | 90 68 89

    68 | 90 89

    89 | 90

    90

  • 7/29/2019 my c book

    5/24

    Selection Sort

    Algorithm

    for i 0 to N-2 do

    min i; i=0

    for j i+1 to N-1 do c

    if (A[j] < A[min]) min j; j=i+1

    }

    swap A[i] and A[min] ;

    }

    Analysis:

    T(N) = c = c (N-1-i) = c (N-1)N / 2 in O(N )

    i=0 j=i+1 i=0

    N-2

    N-1

    N-1N-2 N-2 2

  • 7/29/2019 my c book

    6/24

    Bubble Sort

    Bubble Sort1. In each pass, we compare adjacent elements and swap them if they are out of

    order until the end of the list. By doing so, the 1stpass ends up bubbling upthe largest element to the last position on the list

    2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1 passes, thelist is sorted.

    Example:

    Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17

    45 89 | 68 90 29 34 17 45 68 | 89 29 34 17

    68 89 | 90 29 34 17 68 89 | 29 34 17

    89 90 | 29 34 17 29 89 | 34 17

    29 90 | 34 17 34 89 | 1734 90 | 17 17 89

    17 90

    45 68 89 29 34 17 90 45 68 29 34 17 89

    largest 2nd largest

  • 7/29/2019 my c book

    7/24

    Bubble Sort

    Algorithm

    for i 0 to N-2 do

    i=0

    for j 0 to N-2-i do c

    j=i+1

    if (A[j+1] < A[j]) swap A[j] and A[j+1] ;

    }

    }

    Analysis:T(N) = c = c (N-1-i) = c (N-1)N / 2 in O(N )

    i=0 j=0 i=0

    N-2

    N-2-i

    N-2-iN-2 N-22

  • 7/29/2019 my c book

    8/24

    Sec 3.2 Sequential Search and String Matching

    Sequential search

    Compare successive elements of a given list with a search key until1. either a match is encountered

    2. or the list is exhausted without a match.

    0 1 N-1 N

    Algorithm:extra position

    SequentialSearch(A[0..N], key){ to store the keyA[N] key;i 0;

    while (i

  • 7/29/2019 my c book

    9/24

    Brute-Force String Matching

    pattern: a string ofm characters to search for

    text: a (longer) string ofn characters to search in problem: find a substring in the text that matches the pattern

    Brute-force algorithm

    Step 1 Align pattern at beginning of textStep 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 position to the right and

    repeat Step 2

  • 7/29/2019 my c book

    10/24

    Examples of Brute-Force String Matching

    1. Pattern: 001011Text: 10010101101001100101111010

    2. Pattern: happy

    Text: It is never too late to have a happy childhood.

  • 7/29/2019 my c book

    11/24

    Brute-force String Matching

    Pattern: p[0] p[1] p[m-1] m characters (m

  • 7/29/2019 my c book

    12/24

    Brute-Force Polynomial Evaluation

    Problem: Find the value of polynomial

    p(x) =anxn+an-1x

    n-1 + + a1x1 +a0 at a pointx =x0

    Brute-force algorithm

    Efficiency:

    p 0.0;

    for i n downto 0 do {

    power 1;

    for j 1 to i do //computexi

    power power x;

    p p +a[i] power;

    }

    returnp;

  • 7/29/2019 my c book

    13/24

    Polynomial Evaluation: Improvement

    We can do better by evaluating from right to left:

    Better brute-force algorithm

    Efficiency:

    p a[0]

    power 1

    for i 1 ton do {power power x

    p p +a[i] power

    }

    returnp

  • 7/29/2019 my c book

    14/24

    Closest-Pair Problem

    Find the two closest points in a set ofn points

    (in the two-dimensional Cartesian plane).

    Brute-force algorithm

    Compute the distance between every pair of distinct

    points

    and return the indexes of the points for which the distance

    is the smallest.

  • 7/29/2019 my c book

    15/24

    Closest-Pair Brute-Force Algorithm (cont.)

    Efficiency:

    How to make it faster?

  • 7/29/2019 my c book

    16/24

    Brute-Force Strengths and Weaknesses

    Strengths

    wide applicability

    simplicity

    yields reasonable algorithms for some important problems(e.g., matrix multiplication, sorting, searching, stringmatching)

    Weaknesses

    rarely yields efficient algorithms

    some brute-force algorithms are unacceptably slow

    not as constructive as some other design techniques

  • 7/29/2019 my c book

    17/24

    Exhaustive Search

    A brute force solution to a problem involving search for an elementwith a special property, usually among combinatorial objects such aspermutations, combinations, or subsets of a set.

    Method:

    generate a list of all potential solutions to the problem in asystematic manner (see algorithms in Sec. 5.4)

    evaluate potential solutions one by one, disqualifying infeasibleones and, for an optimization problem, keeping track of the bestone found so far

    when search ends, announce the solution(s) found

  • 7/29/2019 my c book

    18/24

    Example 1: Traveling Salesman Problem

    Given n cities with known distances between each pair,

    find the shortest tour that passes through all the citiesexactly once before returning to the starting city

    Alternatively: Find shortestHamiltonian circuit in a

    weighted connected graph

    Example:

    a b

    c d

    8

    2

    7

    5 34

  • 7/29/2019 my c book

    19/24

    TSP by Exhaustive Search

    Tour Cost

    abcda 2+3+7+5 = 17

    abdca 2+4+7+8 = 21

    acbda 8+3+4+5 = 20

    acdba 8+7+4+2 = 21

    adbca 5+4+3+8 = 20adcba 5+7+3+2 = 17

    More tours?

    Less tours?

    Efficiency:

  • 7/29/2019 my c book

    20/24

    Example 2: Knapsack Problem

    Given n items:

    weights: w1 w2 wn

    values: v1 v2 vn

    a knapsack of capacity W

    Find most valuable subset of the items that fit into the knapsack

    Example: Knapsack capacity W=16

    item weight value

    1 2 $20

    2

    5 $303 10 $50

    4 5 $10

  • 7/29/2019 my c book

    21/24

    Knapsack Problem by Exhaustive Search

    Subset Total weight Total value

    {1} 2 $20

    {2} 5 $30{3} 10 $50

    {4} 5 $10

    {1,2} 7 $50

    {1,3} 12 $70

    {1,4} 7 $30{2,3} 15 $80

    {2,4} 10 $40

    {3,4} 15 $60

    {1,2,3} 17 not feasible

    {1,2,4} 12 $60{1,3,4} 17 not feasible

    {2,3,4} 20 not feasible

    {1,2,3,4} 22 not feasible

    Efficiency:

  • 7/29/2019 my c book

    22/24

    Sec 3.4 Exhaustive Search

    Assignment Problem1. Assign N jobs to N people, one person per job.

    2. Find an assignment such that the total cost is smallest.

    Example:Let C[i, j] be the cost of assigning job j to person i.

    J1 J2 J3 J4 jobs

    P1 9 2 7 8C = P2 6 4 3 7

    P3 5 8 1 8 cost

    P4 7 6 9 4

    persons

    * Select one element in each row so that all selected elements are in

    different columns and the total sum of the cost is the smallest.

    * n-tuple notation: t =

    t[i] = the column (job) # of the element selected in row i

    * C(1,2) + C(2,3) + C(3,4) + C(4,1) = 2 + 3 + 8 + 7 = 20

  • 7/29/2019 my c book

    23/24

    Few iterationsPerson 1 2 3 4

    ========< 1, 2, 3, 4 > 9 + 4 + 1 + 4 = 18< 1, 2, 4, 3 > 9 + 4 + 8 + 9 = 30< 1, 3, 2, 4 > 9 + 3 + 8 + 4 = 24< 1, 3, 4, 2 > 9 + 3 + 8 + 6 = 26< 1, 4, 2, 3 > 9 + 7 + 8 + 9 = 33

    < 1, 4, 3, 2 > 9 + 7 + 1 + 6 = 23

    < 2, 1, 3, 4 > 2 + 6 + 1 + 4 = 13< 2, 1, 4, 3 > 2 + 6 + 8 + 9 = 25

    etc. j1 j2 j3 jN

    Total # of permutations = N !p1 p2 p3 pN

  • 7/29/2019 my c book

    24/24

    Final Comments on Exhaustive Search

    Exhaustive-search algorithms run in a realistic amount of

    time only on very small instances

    In some cases, there are much better alternatives!

    Euler circuits

    shortest paths

    minimum spanning tree

    assignment problem

    In many cases, exhaustive search or its variation is the

    only known way to get exact solution