제2 강. 알고리즘과알고리즘의성능dblab.duksung.ac.kr/ds/pdf/chap02.pdf · < c...

Download 제2 강. 알고리즘과알고리즘의성능dblab.duksung.ac.kr/ds/pdf/Chap02.pdf · < c 자료구조입문> 1 제2 강. ... < c 자료구조입문> 11 ... •프로그램

If you can't read please download the document

Upload: phamcong

Post on 06-Feb-2018

242 views

Category:

Documents


5 download

TRANSCRIPT

  • < C >1

    2 .

    1.

    2.

    3.

  • < C >2

    1. . . (Algorithms are used for calculation, data processing, and automated reasoning

    (Definition)

    ( a finite set of instructions to accomplish a particular task )

    (Criteria) (1) : zero or more inputs

    (2) : at least one output

    (3) : clear and unambiguous instruction

    (4) : terminates after a finite number of steps

    3 (, , ) .

    ?a step-by-step procedure for calculations.

  • < C >3

    1.1

    3

    (sequence) (repetition) .(condition) .

    1) LA

    .

    .

    LA

    .

  • < C >4

    2)

    .

    1.1

    ??

    .

    LA

    .

    .

    Yes

    No

  • < C >5

    1.2

    ( )

    n

    .

    Yes

    No

    Big x

    (x)

    Big

    Big > x

  • [ C ]6

    2.

    (Sorting) (Searching).

    (, )

    .

    ( , )

    .

    .

    ( 1 , 1 ).

  • < C >7

    2.1

    : n .

    : n .

    .

    (n-1) .

    ( )

    10 13 25 15 4 20 5 29 14 21

    4 5 10

    A[10]

    B[10]

  • < C >8

    /* A B */

    => => ( 1 ) =>

    void selection_sort(int *A, int*B){

    int i, j, small, temp; //int A[10], B[10];

    small=0; for(i=0; i A[i]) small=i; }B[0]=A[small]; A[small]=99;

    /*small=0;for(i=1; i A[i]) small=i; }B[1]=A[small]; A[small]=99;

    small=0;for(i=1; i A[i]) small=i; }B[2]=A[small]; A[small]=99;

    }

    [] sortingsearching.txt

  • < C >9

    () A .

    10 13 25 15 4 20 5 29 14 21

    4 13 25 15 10 20 5 29 14 21

    2.1

    ( )

    A[10]

    A[10]

    ( )

  • < C >10

    /* A A */

    void selection_sort(int *A, int*B){

    int i, j, small, temp; //int A[10], B[10];

    for(j=0; j

  • < C >11

    ( )

    : n .

    for (i=0; i

  • < C >12

    ( )

    for (i=0; i

  • < C >13

    2.2

    (Search) : n .

    (Sequential Search) .

    n

    ( )

    (Binary Search) .

    /

    ( .)

  • 9 15 16 19 21 39 51 65 76 85 99

    9 15 16 19 21 39

    < C >14

    ( )

    * 65

    9 15 16 19 21 39 51 65 76 85 99

    51 65

    51 65 76 85 99

    .

    39 65 . 65 39

    .

    76 65 . 65 76

    .

    2.2

  • < C >15

    ( ) =>

    left right

    - left right ,

    - left=0, right=n-1

    list

    middle = (left+right)/2

    list[middle] searchnum

    1) searchnum < list[middle]

    right middle (right=middle-1)

    2) searchnum = list[middle]

    middle (return middle)

    3) searchnum > list[middle]

    left middle+1 (left=middle+1)

    middle

    2.2

  • < C >16

    /* , . */

    int binsearch(int list[],int searchnum, int left,int right)

    {

    /*searchnum list [0]

  • Q/A 8 ?

    (1) 55 (2) 45 (3) 9 (4) 10

    7 ?(1) 55 (2) 45 (3) 9 (4) 10

    int data[]={20, 1, 50, 55, 34, 21, 4, 66, 71, 8};

    void selection_sort(int data[])

    { int i, j, t, min;

    for(i=0; i17

  • Q/A

    binsearch(list, 15, 0, 10)

    3 ?

    1 2 3 4

    int list[]={9, 15, 16, 19, 21, 39, 51, 55, 76, 85, 99 };

    int binsearch(int list[], int searchnum, int left, int right)

    { /*searchnum list [0]

  • < C >19

    3.

    (performance)

    -

    :

    : (Complexity Theory)

    -

    : .

    .

    (Space Complexity)

    :

    (Time Complexity)

    :

  • < C >20

    3.1 (Space Complexity)

    -

    .

    )

    -

    .

    n 2 x n, 3 x n, n x n .

    . ( !)

  • < C >21

    - n

    list[], n, tempsum, i => n+3

    float sum(float list[], int n) { float tempsum = 0;

    int i;for(i = 0; i < n; i++)

    tempsum += list[i];return tempsum;

    }

    -

    = = n + 3

  • < C >22

    3.2 (Time complexity)

    P t . 1GHz 2GHz ?

    t T(P) ! T(P) = count the number of operations the program performs

  • < C >23

    1 : float sum(int n) { 2 : int count=0;3 : int i;4 : for(i = 0; i < n; i++)5 : count+=2;6 : count += 3;7 : return count;8 : }

    n=0 :for loop

    4 = 2, 4, 6, 7

    n=0 :

    2 x n + 4 = 2 + 4, 5 n + 4, 6, 7

    : (n 2)2-4-5-4-5-4-6-7(n 3 )2-4-5-4-5-4-5-4-6-7

    3.2 (Time complexity)

  • < C >24

    . .

    void add(int a[][M_SIZE],int b[][M_SIZE], int c[][M_SIZE],int rows,int cols)

    {int i, j;for(i = 0; i < rows; i++)for(j = 0; j < cols; j++)c[i][j] = a[i][j] + b[i][j];

    }

    = rows + 1 + (rows) * (cols + cols + 1))

    2(rows)(cols) + 2(rows) + 1

    rows cols n = 2n2 + 2n + 1

    3.2 (Time complexity)

  • < C >25

    -

    2rows x cols + 2rows + 1

    0 0 0

    0 0 0

    1 rows+1 rows+1

    1 rows x (cols+1) rows x cols+rows

    1 rows x cols rows x cols

    0 0 0

    void add(int a[ ], [Max_SIZE])

    {

    int i,j;

    for(i=0;i

  • < C >26

    3.3

    n .

    ?

    1 2

    f(n) g(n)

    : (time complexity)1) (input size)

    - n .T(n) = ?

    2)

    - : A(n) = ?

    : W(n) = ?- .- .

  • < C >27

    n , n

    Big O ) A f(n) O(g(n)) .

    => c n0 , n0 n f(n) cg(n) . , n g(n) f(n) . f(n) g(n) ..

    ) f(n) = 25n, g(n) = n2

    if let c = 1,

    25n n2 for all n 25 (n0 =25) f1(n) = 25n c n0 n

    f(n) cg(n)

    f(n) O(g(n)) .

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

    ( O-)

    3.3

    n f(n) = 25n g(n) = n2

    1 25 1

    2 50 4

    25 625 625

    26 650 676

  • < C >28

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

    n, n n0 g(n) (upper bound). .

    n = O(n2), n = O(n2.5)

    n = O(n3), n = O(2n)

    g(n) f(n) .

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

    ) if f(n) = amnm + ... + a1n + a0, then f(n) = O(n

    m)

    )

    f(n) |ak|nk + |ak-1|nk-1 +...+ |a1|n + |a0|= {|ak| + |ak-1|/n +...+ |a1|/n

    k-1+ |a0|/nk}nk

    {|ak| + |ak-1| +...+ |a1| + |a0|}nk= cnk (c = |ak|+|ak-1|+...+|a1|+|a0|)= O(nk)

    ( O-)

    3.3

  • < C >29

    [] O-

    1) f(n) = 3n + 4

    3n + 4

  • < C >30

    [] .

    Omega : (lower bound) .

    ) f(n) = (g(n)) c n0 , n0 n f(n) cg(n)

    . , n , g(n) f(n) . f(n) g(n) .

    Theta :

    ) f(n) = (g(n)) c1, c2 n0 , n0 n c1g(n)

    f(n) c2g(n). , n g(n) f(n) . O . .

    - more precise than both the big oh and omega notations- g(n) is both an upper and lower bound on f(n)

    ( O-)

    3.3

  • < C >31

    class of time complexities

    O(1): constant

    O(log2n): logarithmic

    O(n): linear

    O(nlog2n): log-linear O(n2): quadratic

    O(n3): cubic

    O(2n): exponential

    O(n!): factorial

    (polynomial time)

    (exponential time)

    ( O(f(n)) )

    3.3

  • < C >32

    ( O(f(n)) )

    n 1 2 4 8 16 32

    1log n

    nn log n

    n2

    n3

    constantlogarithmiclinearlog linearquadraticcubic

    101011

    112248

    1248

    1664

    138

    2464

    512

    14

    1664

    2564096

    15

    32160

    102432768

    2nn!

    exponentialfactorial

    21

    42

    1624

    25640326

    6553620922789888000

    429496729626313X 10 33

    .

    3.3

  • Q/A

    1. (O- ) O- .

    1) 5n4 + 6n + 3

    2) nlogn + 2n + 7

    3) 10

    4) nlogn + 1000n

    2. (O- ) - O- Order .

    n nlogn n2logn n! 2n n2 n1/2 logn

    < C >33

  • < C >34

    Review

    .

    :

    : , ,

    .

    n .

    O- .

    O(f(n)), O(g(n)) f(n) > g(n) O(g(n)) .

    O(f(n)) f(n) factorial .