algorithm ppt 82

Upload: aftab-hussain-balouch-adv

Post on 08-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Algorithm Ppt 82

    1/47

    Divide-and-Conquer

    Divide the problem into a number of subproblems.

    Conquer the subproblems by solving them recursively. If

    the subproblem sizes are small enough, solve the

    subproblems in a straightforward manner.

  • 8/7/2019 Algorithm Ppt 82

    2/47

    Divide-and-Conquer

    Combine the solutions to the subproblems into the

    solution for the original problem.

  • 8/7/2019 Algorithm Ppt 82

    3/47

    M erge Sort Algorithm

    Divide: Divide the n-element sequence into two

    subsequences of n/2 elements each.

    Conquer: Sort the two subsequences recursively using

    merge sort.

    Combine: M erge the two sorted sequences.

  • 8/7/2019 Algorithm Ppt 82

    4/47

    H ow to merge two sorted

    sequencesWe have two subarrays A[p..q] and A[q+1..r] in sorted

    order.

    M erge sort algorithm merges them to form a single

    sorted subarray that replaces the current subarray A[p..r]

  • 8/7/2019 Algorithm Ppt 82

    5/47

    M erging Algorithm

    M erge( A, p, q, r)

    1. n1 q p + 1

    2. n2 r q

    3. Create arrays L[1..n1+1] and R[1..n2+1]

  • 8/7/2019 Algorithm Ppt 82

    6/47

    M erging Algorithm

    4. For i 1 to n1

    5. do L[i] A[p + i -1]

    6. For j 1 to n2

    7. do R[j] A[q + j]

  • 8/7/2019 Algorithm Ppt 82

    7/47

    M erging Algorithm

    8. L[n1 + 1]

    9. R[n2 + 1]

    10. i 1

    11. j 1

  • 8/7/2019 Algorithm Ppt 82

    8/47

    M erging Algorithm

    12. For k p to r

    13. Do if L[i]

  • 8/7/2019 Algorithm Ppt 82

    9/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 2 4 5 7 1 2 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (a)

  • 8/7/2019 Algorithm Ppt 82

    10/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 4 5 7 1 2 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (b)

  • 8/7/2019 Algorithm Ppt 82

    11/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 5 7 1 2 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (c)

  • 8/7/2019 Algorithm Ppt 82

    12/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 2 7 1 2 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (d)

  • 8/7/2019 Algorithm Ppt 82

    13/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 2 3 1 2 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (e)

  • 8/7/2019 Algorithm Ppt 82

    14/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 2 3 4 2 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (f)

  • 8/7/2019 Algorithm Ppt 82

    15/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 2 3 4 5 3 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (g)

  • 8/7/2019 Algorithm Ppt 82

    16/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 2 3 4 5 6 6 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (h)

  • 8/7/2019 Algorithm Ppt 82

    17/47

    M erging Algorithm8 9 10 11 12 13 14 15 16 17

    A 1 2 2 3 4 5 6 7 k

    1 2 3 4 5L 2 4 5 7

    i

    1 2 3 4 5R 1 2 3 6

    j

    (i)

  • 8/7/2019 Algorithm Ppt 82

    18/47

    M erge Sort Algorithm

    M erge-Sort( A, p, r)

    1. If p

  • 8/7/2019 Algorithm Ppt 82

    19/47

    O peration of M erge Sort

    1 2 2 3 4 5 6 7

    2 4 5 7 1 2 3 6

    2 5 4 7 1 3 2 6

    5 2 4 7 1 3 2 6

  • 8/7/2019 Algorithm Ppt 82

    20/47

    Analyzing Divide-and-Conquer

    AlgorithmWhen an algorithm contains a recursive call to

    itself, its running time can be described by a

    recurrence equation or recurrence which

    describes the running time

  • 8/7/2019 Algorithm Ppt 82

    21/47

    Recurrence

    If the problem size is small enough, say

    n

  • 8/7/2019 Algorithm Ppt 82

    22/47

    Recurrence

    If we have

    a subproblems, each of which is 1/b the

    size of the original.

    D(n) time to divide the problem

  • 8/7/2019 Algorithm Ppt 82

    23/47

    Recurrence

    C(n) time to combine the solution

    The recurrence

    T(n)= (1) if n

  • 8/7/2019 Algorithm Ppt 82

    24/47

    Recurrence

    Divide: The divide step computes the

    middle of the subarray which takes

    constant time, D(n)= (1)

  • 8/7/2019 Algorithm Ppt 82

    25/47

    Recurrence

    Conquer: We recursively solve two

    subproblems, each of size n/2, which

    contributes 2T(n/2) to the running time.

  • 8/7/2019 Algorithm Ppt 82

    26/47

    Recurrence

    Combine: M erge procedure takes (n) time

    on an n-element subarray. C(n)= (n)

    The recurrence

    T(n)= (1) if n=12T(n/2) + (n) if n>1

  • 8/7/2019 Algorithm Ppt 82

    27/47

    Recurrence

    Let us rewrite the recurrence

    T(n)=

    C represents the time required to solve

    problems of size 1

    C if n=1

    2T(n/2) + cn if n>1

  • 8/7/2019 Algorithm Ppt 82

    28/47

    A Recursion Tree for the

    RecurrenceT(n)Cn

    T(n/2) T(n/2)

  • 8/7/2019 Algorithm Ppt 82

    29/47

    A Recursion Tree for the

    RecurrenceCn

    Cn/2 Cn/2

    T(n/4) T(n/4) T(n/4) T(n/4)

  • 8/7/2019 Algorithm Ppt 82

    30/47

    A Recursion Tree for the

    RecurrenceC(n)

    Cn/2 Cn/2

    Cn/4 Cn/4 Cn/4Cn/4

    C C C C CC

    C

    cn

    cn

    cn

    cn

    lg n

  • 8/7/2019 Algorithm Ppt 82

    31/47

    Total Running Time

    The fully expanded tree has lg n +1 levels

    and each level contributes a total cost of

    cn. Therefore T(n)= cn lg n + cn = (nlg n)

  • 8/7/2019 Algorithm Ppt 82

    32/47

    G rowth of Functions

    We look at input sizes large enough to

    make only the order of growth of the

    running time relevant.

  • 8/7/2019 Algorithm Ppt 82

    33/47

    Asymptotic Notation

    Used to describe running time of an

    algorithm and defined in terms of functions

    whose domains are the set of natural

    numbers N={0,1,2--------------}

  • 8/7/2019 Algorithm Ppt 82

    34/47

    -Notation

    (g(n)) = { f(n) : there exist positive

    constants C 1, C 2, and n 0 such that

    0

  • 8/7/2019 Algorithm Ppt 82

    35/47

    -Notation

    n0n

    C2g(n)

    C1g(n)

    f(n)

    f(n)= (g(n))

  • 8/7/2019 Algorithm Ppt 82

    36/47

    -Notation

    For all n>=n 0, the function f(n) is equal to

    g(n) to within a constant factor. So g(n) is

    asymptotically tight bound for f(n).

  • 8/7/2019 Algorithm Ppt 82

    37/47

    -Notation

    Let us show that 1/2n 2- 3n= (n 2)

    To do so, we must determine C 1, C 2, and

    n0 such that C 1n2

  • 8/7/2019 Algorithm Ppt 82

    38/47

    -Notation

    Diving by n 2

    C1

  • 8/7/2019 Algorithm Ppt 82

    39/47

    O -Notation

    O(g(n)) = { f(n) : there exist positive

    constants C and n 0 such that

    0

  • 8/7/2019 Algorithm Ppt 82

    40/47

    O -Notation

    n0 n

    Cg(n)

    f(n)

    f(n)= O(g(n))

  • 8/7/2019 Algorithm Ppt 82

    41/47

    O -Notation

    O(n 2 ) bound on worst-case running time of

    insertion sort also applies to its running

    time on every input.

  • 8/7/2019 Algorithm Ppt 82

    42/47

    O -Notation

    (n2 ) bound on worst-case running time of

    insertion sort, however, does not imply a

    (n2 ) bound on the running time of

    insertion sort on every input.

  • 8/7/2019 Algorithm Ppt 82

    43/47

    -Notation

    (g(n)) = { f(n) : there exist positive

    constants C and n 0 such that

    0

  • 8/7/2019 Algorithm Ppt 82

    44/47

    -Notation

    n0 n

    Cg(n)

    f(n)

    f(n)= (g(n))

  • 8/7/2019 Algorithm Ppt 82

    45/47

    -Notation

    Since -notation describes a lower bound,

    we use it to bound the best-case running

    time of an algorithm, we also bound the

    running time of algorithm on arbitrary inputs

  • 8/7/2019 Algorithm Ppt 82

    46/47

    o -Notation

    We use o- notation to denote a upper bound

    that is not asymptotically tight. The bound

    2n 2=O(n 2) is asymptotically tight, but the

    bound 2n= O(n 2) is not.

  • 8/7/2019 Algorithm Ppt 82

    47/47

    -Notation

    We use -notation to denote a lower bound

    that is not asymptotically tight.