Algorithm Analysis Design Lecture3 PowerPoint Presentation

Embed Size (px)

Citation preview

  • 11

    Brute Force Brute Force: A straightforward approach usually

    based on problem statement and definitions

    Examples: Which of the following algorithms are based on the brute-force approach?

    1. Computing 1 + 2 + + n by adding once number at a time.2. Euclids algorithm for GCD3. Consecutive integer checking algorithm for GCD.4. Sequential search

    5. Binary search6. Definition-based matrix multiplication7. Selection sort & bubble sort

    11/14/2014

    2

    Brute force strengths and weaknesses (1)

    Strengths:

    applicable to a wide variety of problems

    simple

    yields standard algorithms for simple computational tasks

    sum/product of n numbers

    finding max/min in a list

    yields reasonable algorithms for some important problems

    searching

    string matching

    matrix multiplication

    11/14/2014

    3

    Brute force strengths and weaknesses (2)

    Weaknesses:

    rarely yields efficient algorithms

    some brute force algorithms unacceptably slow

    not as creative as some other design techniques

    11/14/2014

  • 24

    Selection Sort

    Algorithm SelectionSort(A[0..n-1])

    //The algorithm sorts a given array by selection sort

    //Input: An array A[0..n-1] of orderable elements

    //Output: Array A[0..n-1] sorted in ascending order

    for i 0 to n 2 do

    min i

    for j i + 1 to n 1 do

    if A[j] < A[min]

    min j

    swap A[i] and A[min]

    C(n)=(n-1)(n)/2

    (n2)

    11/14/2014

    5

    Bubble Sort (bubble-up from left to right)

    Algorithm BubbleSort(A[0..n-1])

    //The algorithm sorts array A[0..n-1] by bubble sort

    //Input: An array A[0..n-1] of orderable elements

    //Output: Array A[0..n-1] sorted in ascending order

    for i 0 to n 2 do //bubble up the ith largest element

    for j 0 to n-2-i do //j: the index of the left element in each pair

    if A[j] > A[j+1]

    swap A[j] and A[j+1]C(n)=(n-1)(n)/2

    (n2)

    11/14/2014

    6

    Bubble Sort (bubble-up from right to left)

    Algorithm BubbleSort(A[0..n-1])

    //The algorithm sorts array A[0..n-1] by bubble sort

    //Input: An array A[0..n-1] of orderable elements

    //Output: Array A[0..n-1] sorted in ascending order

    for i 0 to n 2 do //bubble up the ith element

    for j n-1 to i+1 do

    if A[j] < A[j-1]

    swap A[j] and A[j-1]

    11/14/2014

  • 37

    Sequential Search

    Algorithm SequentialSearch(A[0..n-1], K)//Searches for a given value in a given array by sequential search

    //Input: An array A[0..n-1] and a search key K

    //Output: Returns the index of the first element of A that matches K or 1 if there are no matching elements

    i 0

    while i < n and A[i] K do

    i i + 1

    if i < n

    return i

    else

    return -1

    Algorithm SequentialSearch2(A[0..n], K)//The algorithm implements sequential search with a search key as a sentinel

    A[n] = Ki 0while A[i] K do

    i i + 1if i < n

    return i

    elsereturn -1

    C(n)=n

    (n)

    11/14/2014

    8

    Brute Force Polynomial Evaluation Problem: Find the value of polynomial

    p(x) = anxn + an-1x

    n-1 + + a1x1 + a0 at a point x = x0

    Algorithm BruteForcePoly (a[0..n], x)//computes the value of polynomial at a given point x by the highest-tolowest

    term brute-force algorithm.//Input: Array a[0..n] of the coefficients of a polynomial of degree n, stored

    from the lowest to the highest and a number x

    //Output: the value of the polynomial at the point x

    P 0

    for i 1 to n do

    p p + a[i] * power

    return p

    Efficiency?

    power 1 // calculatingx i

    for j 1 to i do

    power power * x

    11/14/2014

    9

    Polynomial Evaluation: Improvement

    We can do better by evaluating from right to left

    Algorithm LinearPoly (a[0..n], x)//computes the value of polynomial at a given point x by the lowest-

    to-highest term linear algorithm.

    //Input: Array a[0..n] of the coefficients of a polynomial of degree n, stored from the lowest to the highest and a number x

    //Output: the value of the polynomial at the point x

    P a[0]

    power 1

    for i 1 to n do

    power power * x // calculatingx i

    p p + a[i] * power

    return p

    Efficiency?

    11/14/2014

  • 410

    String Matching (1) The problem

    Given: A text: a (long) string of n characters to search in

    A pattern: a string of m characters (m

  • 513

    Brute-Force String Matching Algorithm

    Algorithm BruteForceStringMatch(T[0..n-1], P[0..m-1])//The algorithm implements brute-force string matching

    //Input: An array T[0..n-1] of n characters representing a text; an array P[0..m-1] of m characters representing a pattern.

    //Output: The position of the first character in the text that starts the first matching substring if the search is successful and 1 otherwise

    for i 0 to n m do //totally n-m+1 entries

    j 0 //j: the index for the pattern

    while j < m and P[j] = T[ i+j ] do

    j = j + 1

    if j = m return i //the corresponding text for the current entry is exhausted.

    return -1

    11/14/2014

    14

    Closest-Pair Problem

    Find the two closest points in a set of n points (in the two-dimensional Cartesian plane).

    Points in question can represent such physical objects as:

    airplanes

    post offices

    statistical samples

    DNA sequences, and so on

    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.

    11/14/2014

    15

    Closest-Pair Brute-Force Algorithm (cont.)

    Efficiency:

    How to make it faster?

    11/14/2014

  • 616

    Exhaustive Search A combinatorial problem is one that asks to find a combinatorial object

    such as a permutation, a combination, or a subset that satisfies certain constraints and has some desired property (e.g., maximizes a value or minimizes a cost). E.g., traveling salesman problem, graph coloring problem (assign the smallest

    number of colors to vertices of a graph so that no two adjacent vertices are the same color.)

    Exhaustive search is a brute force solution to combinatorial problems.

    Method: construct 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, perhaps disqualifying infeasible ones and keeping track of the best one found so far

    When search ends, announce the winner

    11/14/2014

    17

    Example 1: Traveling Salesman Problem (TSP)

    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 in a weighted connected graph.

    Example:a b

    c d

    8

    2

    7

    5 34

    Hamiltonian Circuit: a cycle that passes through all the vertices of the graph exactly once.

    11/14/2014

    18

    Traveling Salesman by Exhaustive Search

    Tour Costabcda 2+3+7+5 = 17

    abdca 2+4+7+8 = 21acbda 8+3+4+5 = 20acdba 8+7+4+2 = 21

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

    a b

    c d

    8

    2

    7

    5 34

    Efficiency: How many tours do we need to generate for an

    exhaustive search?

    The problem is to find a sequence of n+1 adjacent vertices, where the first equals to the last and the other n-1 are distinct.

    Without loss of generality, assume that the circuit starts and end with a specific node.

    We can get the tour by generating all permutations of the n-1 intermediate cities, compute the length, and find the smallest.

    (n-1)!

    11/14/2014

  • 719

    Example 2: Knapsack Problem

    Given n items: weights: w1 w2 wn values: v1 v2 vn a knapsack of capacity W

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

    11/14/2014

    20

    Knapsack by Exhaustive SearchSubset 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 feasibleEfficiency: how many subsets are

    generated for an exhaustive search?

    Example:Knapsack capacity W=16

    item weight value

    1. 2 $20

    2. 5 $30

    3. 10 $50

    4. 5 $10

    List all the subsets

    compute the total weight and value for each subset.

    find the subset with the largest value and W weight.

    2n-1

    11/14/2014

    21

    The traveling salesman problem and knapsack problem are the best-known examples of NP-hard problems. No Polynomial time algorithm is known for any NP hard

    problems.

    It hasnt been proved that polynomial algorithms to solve these problems do not exist.

    Exhaustive search only works for NP-hard problems of very small sizes.

    11/14/2014