CS201 Week 1 Chapter1 Introduction

download CS201 Week 1 Chapter1 Introduction

of 22

Transcript of CS201 Week 1 Chapter1 Introduction

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    1/22

    1

    CS 201

    Data Structures & Algorithms

    Chapter 1 Introduction

    Text: Read Weiss, 1.1 1.3

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    2/22

    2

    Course Policy Syllabus

    Grading

    Labs always in C programming language

    Each assignment starts and ends in the same

    Lab session. Late assignments will not be

    accepted. Study hard!

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    3/22

    3

    Introduction

    See that how a program performs for

    reasonably large input is just as important as

    its performance on moderate amounts of

    input

    Summarize basic mathematical background

    needed Review recursion

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    4/22

    4

    Motivating Examples: Selection

    Selection problem: you have a group ofNnumbers and

    would like to determine the kth largest.

    I: read them into an array. Sort them in decreasing order.

    Return the kth element.

    II: read the first kelements into the array. Sort them indecreasing order. Next read the remaining elements one by

    one. If the new element read is smaller than the last, ignore

    it otherwise place in the correct spot in the array bumping

    one element out of the array.

    A simulation with a random file of 10 million elements andk = 5,000,000 shows that each requires several days of

    computer processing.

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    5/22

    5

    Motivating Examples: Word Puzzles

    Solving a popular word puzzle: Input consists of a twodimensional array of letters and a list of words. Theobjective is to find the words lying horizontally, verticallyor diagonally in either direction.

    I: for each word in the word list, check(row, column,

    orientation) II: for each ordered quadruple (row, column, orientation,

    number of characters), test whether the word is in the wordlist.

    {this, two, fat, that}

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    6/22

    6

    Math Review - Exponents

    XAXB = XA+B

    XA

    /XB

    = XA-B

    (XA)B = XAB

    XN+XN = 2XN !=X2N

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    7/22

    7

    Math Review Logarithms I

    In computer science, all logarithms are to the base 2 unlessspecified otherwise.

    Definition 1.1. XA = B iff logXB=A

    Theorem 1.1. logAB = logCB/logCAwhere A, B, C > 0, A != 1

    Proof: Let X=logCB, Y=logCA, Z=logAB

    CX=B, CY=A, AZ=B by Definition 1.1.

    B=CX=(CY)Z.Therefore, X=YZ

    Theorem 1.2. log AB = logA + logB where A, B > 0

    Proof: X=logA, Y=logB, and Z=logAB,

    2X=A, 2Y=B, and 2Z=AB, 2X2Y=AB=2Z.

    Therefore, X+Y=Z

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    8/22

    Math Review Logarithms II

    log A/B = logA logB

    log(AB)=BlogA

    logX < X for all X > 0

    log1 = 0, log2 = 1, log1024 = 10

    8Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    9/22

    9

    Math Review Series I Geometric Series

    If 0 < A < 1, then and as N

    tends to g, the sum approaches1/(1-A)

    S=1+A+A2+A3+A4

    ... AS=A+A2+A3+A4+A5...

    S-AS= 1 which implies S=1/(1-A)

    112

    0

    2 !!

    NN

    i

    i

    1

    1

    0

    1

    !

    !

    A

    AN

    i

    iA

    N

    A

    N

    i

    iA

    e

    ! 1

    1

    0

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    10/22

    10

    Math Review Series II

    Arithmetic Series

    2...

    2

    1

    2

    1

    2

    112

    ...2

    4

    2

    3

    2

    212

    ...2

    4

    2

    3

    2

    2

    2

    1

    ?

    1

    2/

    32

    32

    432

    !!!

    !

    !

    !

    !

    SSS

    S

    S

    i

    ii

    2/)1(

    1...1112

    _______________________________

    1...21

    ...321

    )(2

    2

    2

    )1(

    1

    !

    !

    !

    !

    }

    !!

    NNS

    NNNNS

    NNN

    N

    S

    S

    MethodGaussNNNN

    i

    i

    2/)13(

    2/)1(31

    1

    1

    3

    1

    1

    1

    3

    1

    13

    :xample

    !

    !

    !

    !

    !

    !

    kk

    kkk

    k

    i

    k

    i

    i

    k

    i

    k

    i

    ik

    i

    i

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    11/22

    11

    Math Review Series III

    3/3

    6/)12)(1(

    6/)132

    2(

    6/)2)1(322(

    3/)12/)1(32

    (

    3/)2/)1(33

    (

    2/)1(333

    1 13

    2

    31 )13

    2

    3

    3

    (

    3

    3

    1

    3)1(

    1

    3

    1

    3)1(

    3

    1

    2

    N

    NNNS

    NNNS

    NNNS

    NNNS

    NNNNS

    NNNSN

    N

    i ii

    N

    i iiii

    N

    N

    i

    iN

    i

    iN

    i

    ii

    SN

    i

    i

    }

    !

    !

    !

    !

    !

    !

    !

    !!

    !

    !

    !

    !

    !

    !

    !

    11

    1

    1{

    }!

    kwhenk

    kNN

    iki

    1log

    1

    1

    SeriesHarmonicthN

    !}!

    ! kwhenNe

    N

    i iN

    H

    The error in theapproximation tends to

    Eulers constant

    K = 0.57721566

    !

    !

    !

    !

    !

    !

    10

    1

    )(

    1

    )(

    0

    )(

    )(

    1

    )(

    onsManipulatilgebraiceneral

    n

    i

    ifN

    i

    ifN

    ni

    if

    NNfN

    i

    Nf

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    12/22

    12

    Math Review Modular Arithmetic

    A | B (mod N) meansA is congruent to B modulo N,

    If N divides A-B

    (remainders are the same)

    Example:81 | 61 | 1 (mod 10)

    if A | B (mod N), then

    A + C | B + C (mod N) and

    AD | BD (mod N)

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    13/22

    13

    The P Word Proof by Induction There are various ways of proving statements in

    data structures analysis

    Proof by Induction: It has two standart parts: The

    first step is proving a base case. Establishing that

    a theorem is true for some small (usuallydegenerate) value(s). This step is almost always

    trivial.

    Next, an inductive hypothesis is assumed.

    Generally this means that the theorem is assumedto be true for all cases up to some limit k.Using the

    assumption, the theorem is then shown to be true

    for the next value, typically k+1.

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    14/22

    14

    The P Word Induction Example I Example: Prove that Fibonacci Numbers F0=1, F1=1, and

    Fi=Fi-1+Fi-2 for i > 1, satisfy Fi < (5/3)i for i 1. Proof: Verify that the theorem is true for the trivial cases

    (base cases): F1=1 < (5/3)1 and, F2 = 2 < (5/3)

    2. These

    prove the basis. We now assume that the theorem is true

    for i = 1, 2, ..., k; this is the inductive hypothesis. To provethe theorem, we need to prove Fk+1

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    15/22

    15

    Example: IfN 1 then

    Proof: For the base case, the theorem is true when N= 1.For the inductive hypothesis, assume the theorem is true

    for 1 k N. Lets try to prove that it is true forN+1

    6/)12)(1(1

    2 ! ! NNNNi

    i

    6

    )32)(2)(1(6

    6722)1(

    )1(6

    )12()1(

    2)1(6

    )12)(1(1

    1

    2

    2)1(

    1

    21

    1

    2

    !

    !

    !

    !

    !

    !

    !

    !

    NNN

    NNN

    NNN

    N

    HypothesisInductivebyNNNNN

    i

    i

    NN

    i

    iN

    i

    i

    The P Word Induction Example II

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    16/22

    Proof by Counterexample: Best way for

    proving that a statement is false.

    Example: The statement Fk k2 is false.

    The easiest way to prove this is to compute

    F11 = 144 > 112 = 121

    16

    Proof by Counterexample

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    17/22

    Proof by Contradiction

    It proceeds by assuming that the theorem is false andshowing that this assumption implies that some known

    property is false, and hence the original assumption is

    erroneous.

    Example: Prove that there is an infinite number of primes.

    Proof: Assume the theorem is false, so that there is some

    largest prime Pk. Let P1, P2, ..., Pkbe all the primes in

    order and considerN= P1P2...Pk+ 1. Clearly, N> Pk, so by

    assumptionNcan not be prime.

    However, none ofP1, P2, ..., Pkdivides Nexactly, becauseremainders are all 1. This is a contradiction: numbers are

    either prime or a product of primes. Hence the original

    assumption is false implying that the theorem is true.

    17Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    18/22

    18

    A Brief Introduction to Recursion A function that is defined in terms of itself is called

    recursive. Not all mathematically recursive functions arecorrectly or efficiently implemented by recursion.

    Example: for all integers x 0 with f(0)=0

    #include

    int F( int X ) {

    if( X == 0 ) /* base case */

    return 0;

    else

    return 2 * F( X - 1 ) + X * X;

    }

    main( )

    {

    printf( "F(4) = %d\n", F( 4 ) );

    return 0;

    }

    2)1(2)( xxfxf !

    If F is called with a value of

    4, then 2*F(3)+

    4*4

    will berequired to be computed. Thus

    a call is made to find F(3).

    F(4)=2*F(3)+4*4

    F(3)=2*F(2)+3*3

    F(2)=2*F(1)+2*2 F(1)=2*F(0)+1*1

    F(0)=0 base case. Recursive

    calls until a base case. F(-1)=

    Automatic Bookkeeping

    Izmir University of Economics

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    19/22

    Recursion - Bad

    #include

    int Bad( unsigned int N ) {

    if( N == 0 )

    return 0;

    else

    return Bad( N / 3 + 1 ) + N - 1;

    }

    main( )

    {

    printf( "Bad is infinite recursion\n" );

    return 0;

    }

    19Izmir University of Economics

    Bad(0)=0, Bad(N)=Bad(N/3 + 1) + N 1

    To compute Bad(1), the computer will repeatedly makecalls to Bad(1). Eventually, it will run out of space

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    20/22

    Fundamental Rules of Recursion - I 1) Base cases: You must always have some

    base cases, which can be solved without

    recursion.

    2)Making progress: For the cases to be

    solved recursively, the recursive call must

    always be to a case that makes progresstoward a base case.

    Izmir University of Economics 20

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    21/22

  • 8/6/2019 CS201 Week 1 Chapter1 Introduction

    22/22

    3)D

    esign rule: Assume that all therecursive calls work. This rule is important.

    It relieves you of the burden of thinking

    about the details of bookkeeping.

    4) Compound interest rule: Never duplicate

    work by solving the same instance of a

    problem in separate calls.

    Hidden bookkeeping costs are mostlyjustifiable. However; It should neverbe

    used as a substitute for a simple loop.

    Izmir University of Economics 22

    Fundamental Rules of Recursion - II