Top Banner

of 21

Lecture-14-CS210-2012 (1).pptx

Jun 03, 2018

Download

Documents

Moazzam Hussain
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    1/21

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 14 Algorithm paradigm of Divide and Conquercontinued

    Counting the number of inversions

    1

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    2/21

    Divide and Conquer paradigm for

    Algorithm Design

    2

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    3/21

    Divide and Conquer paradigm

    An Overview

    A problem in this paradigm is solved in the following way.

    1. FirstDividethe problem instance into two or more instancesof the same problem. Solve each smaller instances

    recursively(base case suitably defined).

    2. Combinethe solutions of the smaller instances to get thesolution of the original instance.

    3

    This is usually the main nontrivialstep

    in the design of an algorithm using

    divide and conquer strategy

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    4/21

    Example 3

    4

    Counting the number of inversions

    in an array

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    5/21

    Counting Inversions in an arrayProblem description

    Definition (Inversion): Given an array Aof size n, a pair (i,j), 0i

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    6/21

    Counting Inversions in an arrayProblem familiarization

    Trivial-algo(A[0..n-1])

    {count0;

    For(j=1 to n-1) do

    { For( i=0toj-1)

    { If (A[i]>A[j]) countcount + 1;

    }

    }

    }

    Time complexity: O(

    )Question: What can be the max. no. of inversions in an array A?

    Answer:

    , which is O().

    Question: Is the algorithm given above optimal ?

    Answer: No, our aim is notto report all inversions but to report the count.6

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    7/21

    Let us try to design a

    Divide and Conquer based algorithm

    7

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    8/21

    How do we approach using divide & conquer

    8

    A 3 15 8 19 9 67 11 27

    0 1 2 3 4 5 6 7

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    9/21

    Counting Inversions

    Divide and Conquer based algorithm

    CountInversion( A,i,k) // Counting no. of inversions in A[i..k]

    If (i=k) return 0;

    Else{ mid(i+k)/2;

    CountInversion(A,i,mid);

    CountInversion(A,mid+1,k);

    . Code for .

    return + + ;

    }

    9

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    10/21

    How to efficiently compute

    (Inversions of type III)?

    Aim:For each mid

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    11/21

    How to efficiently compute

    (Inversions of type III)?

    Key Observation: We have to perform n/2operations of the same kind:

    How many elements in A[i..mid] aregreaterthanA[j] ?

    Lesson from Data Structures :

    We should build a suitable data structure storing elements of A[i..mid] so

    that the above operation can be performed efficiently for anyj.

    Question: What should be the data structure ?

    Answer: Sorted subarray A[i..mid].

    11

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    12/21

    Counting Inversions

    First algorithm based on divide & conquer

    CountInversion( A,i,k)

    If (i=k) return 0;

    Else{ mid(i+k)/2;

    CountInversion(A,i,mid);

    CountInversion(A,mid+1,k);

    . Code for .

    return + + ;

    }

    12

    Sort(A,i,mid);

    Foreach mid

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    13/21

    Counting Inversions

    First algorithm based on divide & conquer

    Time complexity analysis:

    If n= 1,

    T(n) = cfor some constant c

    If n> 1,T(n)= cn logn+ 2T(n/2)

    = cnlogn + cn ((logn)-1) + T(n/)

    = cnlogn + cn ((logn)-1) + cn ((logn)-2) + T(n/)

    = O(nn)

    13

    Can we improve it further ?

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    14/21

    Sequence of observations

    To achieve better running time

    The extra logn factor arises because for the combine step, we are

    spending O(nlog n) time instead of O(n).

    The reason for O(nlog n) time for the combine step is due to the

    following tasks: SortingA[0..n/2] takesO(nlog n) time.

    Doing Binary Search for n/2elements from A[n/2n-1]

    Each of the above tasks have optimal running time.

    So the only way to improve the running time of combine step is to look

    for some new ideas to compute .

    14

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    15/21

    Learn from the past knowledge

    15

    Many of you noticed some similaritybetween

    the code of the O(nn) time algorithm and

    Merge Sort. Explore these similarities moreclosely.

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    16/21

    Revisiting MergeSortalgorithm

    MSort(A,i,k)// Sorting A[i..k]

    { If(i< k)

    { mid(i+k)/2;

    MSort(A,i,mid);

    MSort(A,mid+1,k);

    Create a temporary array C[0..k-i]

    Merge(A,i,mid,k,C);

    CopyC[0..k-i] to A[i..k]

    }}

    16

    We shall carefully look at the Merge()

    procedure to find an efficient way to count

    the number of elements from A[i..mid]

    which are smaller than A[j] for any given

    mid

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    17/21

    RelookMerging A[i..mid] and A[mid+1..k]

    17

    A

    Sorted Sorted

    j

    x

    C

    x

    >x

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    18/21

    Pesudo-code for Merging two sorted arrays

    Merge(A,i,mid,k,C)

    pi; jmid+1; r0;

    While(p mid andj k)

    { If(A[p]< A[j]) { C[r]A[p]; r++; p++ }

    Else { C[r]A[j]; r++; j++ }

    }

    While(p mid) { C[k]A[i]; k++; i++ }

    While(j k) { C[k]

    A[j]; k++; j++ }returnC ;

    18

    We shall makejust a slight change in the above

    pseudo-code to achieve our main objective of

    computing

    . If you understood the discussion

    of the previous slide, can you guess it now ?

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    19/21

    Pesudo-code for

    Merging and counting inversions

    Merge_and_CountInversion(A,i,mid,k, C)

    pi; jmid+1; r0;

    0;

    While(p mid andj k)

    { If(A[p]< A[j]) { C[r]A[p]; r++; p++ }

    Else { C[r]A[j]; r++; j++;

    ??

    }

    }While(p mid) { C[k]A[p]; k++; i++ }

    While(j k) { C[k]A[j]; k++; j++ }

    return;

    19

    + (mid-p+1);

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    20/21

    Counting Inversions

    Final algorithm based on divide & conquer

    Sort_and_CountInversion(A,i,k)

    { If(i=k) return 0;

    else

    { mid(i+k)/2;

    Sort_and_CountInversion (A,i,mid);

    Sort_and_CountInversion (A,mid+1,k);

    Create a temporary array C[0..k-i]

    Merge_and_CountInversion(A,i,mid,k,C);

    CopyC[0..k-i] to A[i..k];return + + ;

    }

    }

    20

    2 T(n/2)

    O(n)

  • 8/11/2019 Lecture-14-CS210-2012 (1).pptx

    21/21

    Counting Inversions

    Final algorithm based on divide & conquer

    Time complexity analysis:

    If n= 1,

    T(n) = cfor some constant c

    If n> 1,T(n)= cn + 2T(n/2)

    = O(nlogn)

    Theorem:There is a divide and conquer based algorithm forcomputing the number of inversions in an array of size n. The

    running time of the algorithm is O(nlogn).

    21

    The second practice sheet of problems is available on

    moodle. Make sincere attempts to solve its exercises.