Lecture-13-CS210-2012.pptx

Embed Size (px)

Citation preview

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    1/24

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 13 Ram model of computationfurther refinement

    Algorithm paradigms

    Algorithm paradigm of Divide and Conquer

    1

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    2/24

    Word RAM model of computation

    Further refinements

    2

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    3/24

    word RAM : a model of computation

    3

    RAM

    Processor

    Data

    Program

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    4/24

    Execution of a instruction(fetching the operands, arithmetic/logical operation, storing the result back into RAM)

    4

    RAM

    Processor

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    5/24

    word RAM model of computation:

    Characteristics

    Word is the basic storageunit of RAM. Word is a collection of few bytes.

    Each input item (number, name) is stored in binary format.

    RAM can be viewed as a huge array of words. Any arbitrary location ofRAM can be accessedin the same time irrespectiveof the location.

    Data as well as Program reside fully in RAM.

    Each arithmetic or logical operation (+,-,*,/,or, xor,) involving a constant

    number of words takes a constant number of steps by the CPU.

    5

    Each arithmetic or logical operation (+,-,*,/,or, xor,) involving O( log n) bitstake a constant number of steps by the CPU, where nis the number of bits of

    input instance.

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    6/24

    Justification for the extension

    Question: How many bits are needed to access an input item from RAM ?

    Answer:At least log n.(kbits can be used to create at most different addresses)

    Current-state-of-the-art computers:

    RAM of size 4GB

    Hence 32 bits to address any item in RAM.

    Support for 64-bit arithmeticAbility to perform arithmetic/logical operations on any two 64-bit numbers.

    6

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    7/24

    Algorithm Paradigms

    7

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    8/24

    Algorithm Paradigm

    Motivation:

    Many problems whose algorithms are based on a common approach.

    A need of a systematic study of the characteristics of such widely used

    approaches.

    Algorithm Paradigms:

    Divide and Conquer

    Greedy Strategy

    Dynamic Programming Local Search

    8

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    9/24

    Divide and Conquer paradigm for

    Algorithm Design

    9

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    10/24

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    11/24

    Example 1

    11

    Sorting

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    12/24

    A Homework given in 5thLecture

    Merging two sorted arrays:

    Given two sorted arrays A andBstoring nelements each, Design an O(n) timealgorithm to output a sorted array Ccontaining all elements of A andB.

    Example: If A={1,5,17,19} B={4,7,9,13}, then output is

    C={1,4,5,7,9,13,17,19}.

    12

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    13/24

    BA

    Mergingtwo sorted arrays A and B

    13

    1 5 139717 19 4

    C 5 7 9 131 4

    17 19

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    14/24

    Pesudo-code for Merging two sorted arrays

    Merge(A[0..n-1],B[0..m-1])

    {

    i0; j0; k0;

    While(i

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    15/24

    Divide and Conquer based sorting algorithm

    MSort(A,i,j)

    { If(i

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    16/24

    Analysis of Merge Sort

    Time complexity:

    If n= 1,

    T(n) = cfor some constant c

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

    = cn+ cn+ T(n/)

    = cn+ cn+ cn+ T(n/)

    = cn+ . (Log n terms) .+ cn

    = O(nlog n)

    16

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    17/24

    Analysis of Merge Sort

    Correctness:

    Question: What is to be proved ?

    Answer: MSort(A,i,j) sorts the subarray A[i..j]

    Question: How to prove ?

    Answer:

    By induction on the length (j-i+1) of the subarray.

    Use correctness of the algorithm Merge.

    17

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    18/24

    Example 2

    18

    Faster algorithm for multiplying two

    integers

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    19/24

    Addition is fasterthan multiplication

    Given:any two -bit numbers Xand Y

    Question: how many bit-operationsare required to compute X+Y?

    Answer: O()

    Question: how many bit-operationsare required to compute X*?

    Answer: O() [left shift the number Xby places, (do it carefully)]

    Question: how many bit-operationsare required to compute X*Y?Answer: O(2) (why ??)

    19

    Can we compute

    X*Y faster ??

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    20/24

    PursuingDivide and Conquer approach

    Question: how to express X*Y in terms of multiplication/addition of{A,B,C,D} ?

    Hint:First Express X and Y in terms of {A,B,C,D}.X = A* /+ B and Y = C* /+ D

    Hence

    X*Y =(A*C)* ?? + (A*D + B*C)* ?? + B*D

    20

    X

    Y

    MSB LSB

    n-1 n-2......n/2.............................2 1 0

    A B

    C D

    /

    4 multiplications

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    21/24

    PursuingDivide and Conquer approach

    X*Y =(A*C)* ?? + (A*D + B*C)* ?? + B*D

    LetT(n) : time complexity of multiplying X and Y using the above equation.

    T(n) = cn+ 4T(n/2) for some constant c

    = cn+ 2cn+ T(n/)

    = cn+ 2cn+ cn +T(n/)

    =cn+ 2cn+ cn + 8cn + +

    = cn+ 2cn+ cn + 8cn + +

    21

    X

    Y

    MSB LSB

    n-1 n-2......n/2.............................2 1 0

    A B

    C D

    /

    O() time algo

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    22/24

    PursuingDivide and Conquer approach

    X*Y =(A*C)* ?? + (A*D + B*C)* ?? + B*D

    Observation:A*D+ B*C = (A+B)*(C+D)A*CB*D

    Question:How many multiplications do we need nowto compute X*Y ?Answer: 3multiplications :

    A*C

    B*D

    (A+B)*(C+D) . 22

    X

    Y

    MSB LSB

    n-1 n-2......n/2.............................2 1 0

    A B

    C D

    /

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    23/24

    PursuingDivide and Conquer approach

    X*Y =(A*C)* ?? + ((A+B)*(C+D) - A*CB*D)* ?? + B*D

    LetT(n) : time complexity of the new algo for multiplying two n-bit numbers

    T(n) = cn+ 3T(n/2) for some constant c= cn+ 3/2cn+ T(n/)

    = cn+ 3/2cn+ (9 )cn + +

    = O() = O(.)

    23

    X

    Y

    MSB LSB

    n-1 n-2......n/2.............................2 1 0

    A B

    C D

    /

  • 8/11/2019 Lecture-13-CS210-2012.pptx

    24/24

    Conclusion

    Theorem:There is a divide and conquer based algorithm formultiplying any two n-bit numbers inO(.) time(bit operations).

    Note:The fastest algorithm for this problem runs in a little bit more than O(log )

    time. One such algorithm was designed in 2008by Dey, Kurur, Saha, and

    Saptharishi (CSE, IIT Kanpur).

    24