수치해석 (Numerical Methods)

Download Report

Transcript 수치해석 (Numerical Methods)

Fundamentals of Data Structures
in C
By Horowitz,Sahni & Freed
Sookmyung Women’s University
Yukyong Kim, Ph.D.
Basic Concepts
1. Overview
2. Algorithm Specification
3. Data Abstraction
4. Performance Analysis
5. Practical Complexities
6. Performance Measurement
2
1. Overview
• Data Structures : The Objectives
– 대규모 컴퓨터 시스템을 설계,구현하는 tools과
techniques습득
– Prerequisites : 비교적 작은 문제를 해결하는
structured programming 기술
3
1. Overview (cont.)
• Development Process: the System Life Cycle
– Requirements
• system 이 다루는 problem 의 input/output을 정의하는 단계
– Analysis
• problem 을 다루기에 적합한 sub-problems로 분리하는 단계
– Design
• problem에서 data objects 와 operations을 찾아내는 단계
– Refinement and coding
• data objects에 대한 representation을 결정하고,operations에
대한 알고리즘을 작성하는 단계
– Verification
• correctness 증명,
• testing for input data,
• error 제거
4
1. Overview (cont.)
• Program Life Cycle
5
2. Algorithm Specification
• Definition of Algorithm : 어떤 문제를 해결하는
instructions의 유한집합으로써 다음 사항을
만족해야 한다.
– Input : 외부에서 알고리즘으로 공급되는 0 혹은
하나 이상의 quantities가 존재함
– Output : 알고리즘 수행 후 적어도 하나의 quantity가
생성됨
– Definiteness : 각 instruction이 clear하고,
unambiguous함.
– Finiteness : 알고리즘의 모든 cases에서 유한
steps후에 종료함.
– Effectiveness : 각 instruction이 basic함.
6
2. Algorithm Specification (cont.)
• 주의
– Computational Theory에서는 알고리즘을
프로그램과 구분하나 여기서는 끝이 있는
프로그램만 다루므로 알고리즘과 프로그램을
혼용한다.
– 알고리즘 기술 방법에는 natural language, flow
chart 등으로 기술이 가능하지만 여기서는 C
언어와 natural language를 혼용한다.
7
2. Algorithm Specification (cont.)
• Example : 알고리즘 Selection Sort
a)문제 - Natural language
• From those integers that are currently unsorted, find the
• smallest and place it next in the sorted list.
b)문제의 설명 : Graphical representation
8
2. Algorithm Specification (cont.)
c)알고리즘 (C-like natural language로 기술함)
for(i = 0 ; i < n; i++) {
Examine list[i] to list[n-1] and suppose that the smallest
integer is at list[min];
Interchange list[i] and list[min];
}
9
2. Algorithm Specification (cont.)
d) A real C program
#define SWAP(x,y,t) ((t) = (x), (x) = (y), (y) = (t))
for(i=0; i < n–1 ; i++) {
min = i;
for (j = i+1; j < n; j++) {
if (list[j] < list[min]) min = j;
SWAP(list[i], list[min], tem p);
}
}
void swap(int *x, int *y) {
int tem p = *x;
*x = *y;
*y = tem p;
}
10
Example : A Binary Sort Algorithm
a)문제 : 서로 다른 n개의 정렬된 수가 list에
저장되어 있을 때, 특정한 값을 찾는다.
b) Binary Search의 설명
– 다음 리스트 A[10]에서 searchnum = 4를 찾는 문제
11
Example : A Binary Sort Algorithm (cont.)
c)알고리즘 (C-like)
while (there are more integers to check) {
middle = (left + right) / 2;
if (searchnum < list[middle])
right = middle - 1;
else if (searchnum == list[middle])
return middle;
else left = middle + 1;
}
12
Example : A Binary Sort Algorithm (cont.)
d) 알고리즘 (A real C)
#define COM PARE(x,y) ( ( (x) < (y) ) ? -1 : ( (x) == (y) ) ? 0 : 1 )
int binsearch (int list[ ], int searchnum , int left, int right) {
int middle;
while (left <= right) {
middle = (right + left) / 2;
switch (COM PARE(list[middle], searchnum ) ) {
case -1: left = middle + 1;
break;
case 0 : return middle;
case 1 : right = middle - 1;
break;
}
}
return - 1;
}
13
2.1. Recursive Algorithms
• Recursion(재귀) :the technique of defining a process
in terms of itself
Example : n!
f(n) = 1, if n = 0
n * f(n – 1), otherwise
• Recursive Algorithm : Algorithm의 몸체(Body)에서
자신을 부르는 Algorithm
– 문제에 대한 data structure가 recursive하게 정의되는 경우에
간단한 해결 기법을 제공
– 2 가지 유형의 Recursive Algorithm
• direct recursion : algorithm A 가 A를 직접 Call
• indirect recursion : algorithm A가 B를 Call하고,B가 A를 Call
14
Example : Computing Factorial
a) R ecursive Call:
int fact(int n) {
if(n = = 0) return(1);
else
return (n * fact(n-1));
}
b) Iteration:
int fact(int n) {
int i, f = 1;
for (i=n; i>0; i--)
f = f * i;
return(f);
}
15
Example: Recursive Binary Search
Algorithm
• 리스트 A[0,n–1]에서 v를 찾는 문제
– A[0,middle–1]과 A[middle+1,n–1]에서 v 를 찾는 문제로 나뉨.
int binsearch (int list[], int searchnum , int left, int right) {
int middle;
if (left <= right) {
middle = (right + left) / 2
switch (com pare(list[middle], searchnum ) {
case -1 : return /* list[middle] < searchnum */
binsearch(list,searchnum , middle + 1, right) ;
case 0 : return middle;
case 1 : return
binsearch(list,searchnum , left, middle - 1)
}
return - 1;
}
}
16
3. Data Abstraction
• Data Type
– Data:가공되지 않은 정보,현실세계로부터
관찰이나 측정을 통해 얻은 단순한 facts or values
– a collection of objects and a set of operations that
act on those objects
• predefined data type과 user defined data
type으로 구분됨
– predefined data type : int, char, float,… (system
dependent)
– user defined data type : struct, array, class(in
C++), ...
17
3. Data Abstraction (cont.)
• Example: integer data type
– data objects :
{INT_M IN, … , -2, -1, 0, 1, 2, … , INT_M AX}
– operations :
integer plus,
integer minus,
integer multiplication,
integer division
18
3. Data Abstraction (cont.)
• Abstract Data Type (ADT)
– data objects 와 operations의 specifications과
implementation이 분리된 data type.
Ex: int x = 10;
10은 어떻게 표현되는가 ?
변수 x는 어떻게 구현되는가 ?
치환 연산은 어떻게 처리되는가 ?
– Alternative definition : operations들로 구성된
인터페이스를 통해서만 data를 access할 수 있는
data type
19
3. Data Abstraction (cont.)
• data objects와 operations의 specifications이란
– function name, arguments (name, type), return type,
– a function description without implementation details
•
data objects와 operations의 implementation 이란
– data structures(list, array, … )
– code를 의미함
• ADT Operations의 분류
– Creator/constructor : ADT의 new instance 생성
– Transformers : ADT의 기존 instances를 사용하여 new
instance를 생성
– Observers/Reporters : ADT의 instances에 대한 정보를 제공
– Destructor
20
4. Performance Analysis
• Program 에 대하여 machine과 독립적인 time,
space를 분석하는 것
•
Program 의 일반적인 평가기준
– 문제의 Specifications을 만족하는가?
– Program 이 correct한가?
– Program 의 documentation이 충분한가?
– 문제의 logical units을 생성하는데 functions을 효율적으로
사용했는가?
– Program 이 readable한가?
– Program 이 storage (main, disk)를 효율적으로 사용하는가?
– Program 의 수행시간이 늦지 않은가?
21
4. Performance Analysis (cont.)
• Definition : program P의 time, space,
complexity
– Space complexity : P의 수행 완료에 필요한
memory량
– time complexity : P의 수행 완료에 필요한
computer time
22
4. Performance Analysis (cont.)
• Space Complexity(S(p)) : fixed space 와 variable
space로 구성됨
– fixed space : program의 input/output size에 무관한 space(C)
• instruction space (code 저장 space)
• space for simple variables
• fixed size structured variables (struct등)
• constants
– variable space: program 의 input/output size에 종속된
space(Sp(I))
• 문제해결과정에서 instance I에 따라 크기가 정해지는 structured
variables의 space
• recursion에 필요한 추가 space
• is a function of some characteristics of the instance I.
– 프로그램 P의 전체 space요구량 : S(P) = C + Sp(I)
23
4. Performance Analysis (cont.)
• Time Complexity T(P)
T(P) = compile time(Tc) + run time(Tp)
– 한번 compile해서 n번 수행하므로 running time(Tp)이 중요함
Example : n개의 수를 더하고 빼는 program 의 run time complexity
Tp(n) = Ca ADD(n) + Cs SUB(n) + Cl LDA(n) + Cst STA(n)
– n : instance characteristics (n개의 수를 더하고,뺀다)
– Ca (Cs,Cl,Cst) : 각 Addition (Subtraction, Load, Store) 연산의
시간
– ADD (SUB, LDA, STA) : 각 연산의 회수를 나타내는 함수
24
4. Performance Analysis (cont.)
• Run time (execution time)의 추정에서
– 정확한 계산 : 시스템 clock수로 계산 가능 하지만,
machine dependent하며,큰 의미가 없다.
– 실용적인 대안 : program 이 수행하는 operation의
수로 추정하며 machine independent하게 추정한다.
⇒program step사용
25
4. Performance Analysis (cont.)
• Definition of program step :
Syntactically or semantically meaningful
program segments whose execution time is
independent of the instance characteristics.
• Example: assignment
a = 2 (one step)
a = 2 * b + 3 * c / d – e + f / g (one step)
26
4. Performance Analysis (cont.)
Example : program steps for iterative summing of a list
of numbers
float sum (float list[], int n) {
float tem psum = 0; count++; /* for assignment */
int i;
for ( i = 0; i < n; i++) {
count++; /* for the for loop */
tempsum += list[i]; count++; /* for assignment */
}
count++; /* last execution of for */
count++; /* for return */
return tempsum ;
}
27
4. Performance Analysis (cont.)
• Example : program steps for iterative
summing of a list of numbers
28
4. Performance Analysis (cont.)
• Example : program steps for recursive
summing of a list of numbers
29
4. Performance Analysis (cont.)
• Asymptotic Notation (Ο,Ω,Θ)
– program 의 step을 counting하는 목적
• 두 programs의 time complexity 분석/비교
• instance characteristics이 변할 때 run time의 증가 치의
분석
– 정확한 step count는 일반적으로 어려우며,위의
목적에 유용하지 않음
⇒ program 의 time과 space complexity를 잘
나타내는 새로운 개념이 필요함.
30