data structure - 1st study

40

Upload: -

Post on 13-Aug-2015

2.727 views

Category:

Technology


0 download

TRANSCRIPT

3

4

5

6

7

9

10

11

float ABC(float a, float b, float c){

return a + b + b * c + (a + b - c) / (a + b) + 4.0;}

12

float Sum(float* a, const int n){

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

s += a[i];return s;

}

float Rsum(float* a, const int n){

if (n <= 0) return 0;else return (Rsum(a, n - 1) + a[n - 1]);

}

13

14

15

16

17

18

19

float Sum(float* a, const int n)1 {2 float s = 0;3 for (int i = 0; i < n; i++)4 s += a[i];5 return s;6 }

행 번호 s/e 빈도 단계 수

1 0 0 0

2 1 1 1

3 1 𝑛 + 1 𝑛 + 1

4 1 𝑛 𝑛

5 1 1 1

6 0 1 0

총 단계 수 2𝑛 + 3

s/e : 실행당 단계 수 (Step per Execution)

행 번호 s/e빈도 단계 수

𝑛 = 0 𝑛 > 0 𝑛 = 0 𝑛 > 0

1 0 1 1 0 0

2(a) 1 1 1 1 1

2(b) 1 1 0 1 0

3 1 + 𝑡𝑅𝑠𝑢𝑚(𝑛 − 1) 0 1 0 1 + 𝑡𝑅𝑠𝑢𝑚(𝑛 − 1)

4 0 1 1 0 0

총 단계 수 2 2 + 𝑡𝑅𝑠𝑢𝑚(𝑛 − 1)

20

float Rsum(float* a, const int n)1 {2 if (n <= 0) return 0;3 else return (Rsum(a, n - 1) + a[n - 1]);4 }

s/e : 실행당 단계 수 (Step per Execution)

21

22

void Add(int** a, int** b, int** c, int m, int n)1 {2 for (int i = 0; i < m; i++)3 for (int j = 0; j < n; j++)4 c[i][j] = a[i][j] + b[i][j];5 }

행 번호 s/e 빈도 단계 수

1 0 0 0

2 1 𝑚 + 1 𝑚 + 1

3 1 𝑚(𝑛 + 1) 𝑚𝑛 +𝑚

4 1 𝑚𝑛 𝑚𝑛

5 0 1 0

총 단계 수 2𝑚𝑛 + 2𝑚 + 1

s/e : 실행당 단계 수 (Step per Execution)

23

24

25

26

27

28

float Sum(float* a, const int n)1 {2 float s = 0;3 for (int i = 0; i < n; i++)4 s += a[i];5 return s;6 }

행 번호 s/e 빈도 단계 수

1 0 0 Θ(0)

2 1 1 Θ(1)

3 1 𝑛 + 1 Θ(𝑛)

4 1 𝑛 Θ(𝑛)

5 1 1 Θ(1)

6 0 1 Θ(0)

𝑡𝑆𝑢𝑚 𝑛 = Θ(max1≤𝑖≤6

{𝑔𝑖 𝑛 }) =Θ(𝑛)

s/e : 실행당 단계 수 (Step per Execution)

행 번호 s/e빈도 단계 수

𝑛 = 0 𝑛 > 0 𝑛 = 0 𝑛 > 0

1 0 1 1 0 Θ(0)

2(a) 1 1 1 1 Θ(1)

2(b) 1 1 0 1 Θ(0)

3 1 + 𝑡𝑅𝑠𝑢𝑚(𝑛 − 1) 0 1 0 Θ(1 + 𝑡𝑅𝑠𝑢𝑚 𝑛 − 1 )

4 0 1 1 0 Θ(0)

𝑡𝑅𝑠𝑢𝑚 𝑛 = 2 Θ(1 + 𝑡𝑅𝑠𝑢𝑚 𝑛 − 1 )

29

float Rsum(float* a, const int n)1 {2 if (n <= 0) return 0;3 else return (Rsum(a, n - 1) + a[n - 1]);4 }

s/e : 실행당 단계 수 (Step per Execution)

30

void Add(int** a, int** b, int** c, int m, int n)1 {2 for (int i = 0; i < m; i++)3 for (int j = 0; j < n; j++)4 c[i][j] = a[i][j] + b[i][j];5 }

행 번호 s/e 빈도 단계 수

1 0 0 Θ(0)

2 1 Θ(𝑚) Θ(𝑚)

3 1 Θ(𝑚𝑛) Θ(𝑚𝑛)

4 1 Θ(𝑚𝑛) Θ(𝑚𝑛)

5 0 1 Θ(0)

총 단계 수 Θ(𝑚𝑛)

s/e : 실행당 단계 수 (Step per Execution)

31

int BinarySearch(int* a, const int x, const int n){ // Search the sorted array a[0], ... , a[n-1] for x.

int left = 0, right = n - 1;while (left <= right){ // There are more elements

int middle = (left + right) / 2;if (x < a[middle]) right = middle - 1;else if (x > a[middle]) left = middle + 1;else return middle;

} // End of whilereturn -1; // Not found

}

값이 정렬된 배열이 있을 때, Best Case는찾는 값이 middle에 바로 있는 경우(9를 검색)

2 5 9 11 15 18

2 5 11 15 189

left

(0)

right

(5)

middle

(0+5)/2=2.5≈2

이 때, 시간 복잡도는 𝑂(1)!

x = 9, a[middle] = a[2] = 9이므로9가 있는 위치 2를 반환

32

int BinarySearch(int* a, const int x, const int n){ // Search the sorted array a[0], ... , a[n-1] for x.

int left = 0, right = n - 1;while (left <= right){ // There are more elements

int middle = (left + right) / 2;if (x < a[middle]) right = middle - 1;else if (x > a[middle]) left = middle + 1;else return middle;

} // End of whilereturn -1; // Not found

}

Worst Case는 배열에 없는 값을 검색할 때(24를 검색)

2 5 11 15 189

left

(0)

right

(5)

middle

(0+5)/2=2.5≈2

x = 24, a[middle] = a[2] = 9이므로left를 3으로 바꾼 뒤 다시 수행

33

int BinarySearch(int* a, const int x, const int n){ // Search the sorted array a[0], ... , a[n-1] for x.

int left = 0, right = n - 1;while (left <= right){ // There are more elements

int middle = (left + right) / 2;if (x < a[middle]) right = middle - 1;else if (x > a[middle]) left = middle + 1;else return middle;

} // End of whilereturn -1; // Not found

}

2 5 11 189 15

left

(3)

right

(5)

middle

(3+5)/2=4

x = 24, a[middle] = a[4] = 15이므로left를 5로 바꾼 뒤 다시 수행

34

int BinarySearch(int* a, const int x, const int n){ // Search the sorted array a[0], ... , a[n-1] for x.

int left = 0, right = n - 1;while (left <= right){ // There are more elements

int middle = (left + right) / 2;if (x < a[middle]) right = middle - 1;else if (x > a[middle]) left = middle + 1;else return middle;

} // End of whilereturn -1; // Not found

}

2 5 119 15 18

left = right

(5)

middle

(5+5)/2=5

x = 24, a[middle] = a[5] = 18이므로left를 6로 바꾼 뒤 다시 수행

하지만, while (left <= right) 문에서left = 6, right = 5이므로 while (false),while 문을 빠져나오게 되어 -1을 반환

이 때, 시간 복잡도는?

35

int BinarySearch(int* a, const int x, const int n){ // Search the sorted array a[0], ... , a[n-1] for x.

int left = 0, right = n – 1;while (left <= right){ // There are more elements

int middle = (left + right) / 2;if (x < a[middle]) right = middle – 1;else if (x > a[middle]) left = middle + 1;else return middle;

} // End of whilereturn -1; // Not found

}2 5 119 15 18

2 5 11 189 15

2 5 11 15 189

시간 복잡도 계산의 핵심은 “while 문이 얼마나 반복되었는가?”부분이 얼마나 많이 수행되었느냐가 관건부분은 상수 시간에 수행되므로 𝑂(1)

left right

𝑇 𝑛

left right

𝑇 𝑛/2

𝑇 𝑛/4

left = right

+𝑂(1)

+𝑂(1)

따라서, 시간 복잡도는

𝑇 𝑛 = 𝑇𝑛

2+ 𝑂 1 !

36

𝑇 𝑛 = 𝑇𝑛

2+ 𝑂 1

= 𝑇𝑛

22+ 2𝑂 1

= ⋯

= 𝑇𝑛

2𝑘+ 𝑘𝑂(1)

𝑛 = 2𝑘, 𝑇 1 = 𝑂(1)라면

∴ 𝑇 𝑛 = 𝑇 1 + 𝑘𝑂 1= 𝑘 + 1 𝑂(1)

𝑛 = 2𝑘이므로 𝑘 = log2 𝑛

∴ 𝑇 𝑛 = log2 𝑛 + 1 𝑂 1= 𝑂 log2 𝑛 = 𝑂(log 𝑛)

37

https://en.wikipedia.org/wiki/Big_O_notation

http://bigocheatsheet.com/

38

http://bigocheatsheet.com/

39

http://bigocheatsheet.com/

40

http://bigocheatsheet.com/