KAIST- CS206 1
CS206A
DATA STRUCTURE
Spring, 2003
KAIST- CS206 2
Instructor: 김태환
tkim@cs.kaist.ac.kr x3542
조교: webpage (http://vlsisyn.kaist.ac.kr/course/cs206) 참조
Text: Fundamentals of Data Structures in C++
E. Horowitz, et al.
강의는 text 전 내용을 거의 모두 cover함.
매주 1 chapter 씩 진도 나가는 것으로 보면 됨.
Grading: Homework/Quiz/Participation 30%,
Mid-exam: 30%, Final: 40%
주의: 대리출석, 숙제copy, 시험부정행위는 무조건 F 학점
KAIST- CS206 3
Chapter 1 Basic Concepts
KAIST- CS206 4
1.1 System Life Cycle
(1) Requirements
∙ A set of specifications that define the project
(2) Analysis
∙ Break the problem down into manageable pieces
∙ bottom-up vs top-down
(3) Design
∙ Description of how to achieve the specification
∙ Data objects abstract data types
∙ Operations algorithms
∙ Programming language independent
KAIST- CS206 5
1.1 System Life Cycle (cont)
(4) Refinement and coding
∙ Representation of data objects and algorithms
∙ Program language dependent
(5) Verification
∙ Correctness proofs, testing, error removal
KAIST- CS206 6
1.2 Algorithm Specification
1.2.1 Introduction
Algorithm: input, output, definiteness, finiteness,
effectiveness
∙ Representation of data objects and algorithms
∙ Program language dependent
Example 1.1 [Selection sort]
for (i=0; i<n; i++) {
interchange list[i] and list[min];
}
Example 1.2 [Binary search]
KAIST- CS206 7
1.2 Algorithm Specification (cont)
1.2.2 Recursive Algorithm
Example 1.3 [Binary search]
int binsearch(int list[], int x, int left, int right) {
int mid;
if (left < right) {
mid = (left + right) / 2;
switch (COMPARE(list[mid], searchnum)) {
case -1: return binsearch(list, x, mid+1, right);
case 0 : return mid;
case 1 : return binsearch(list, x, left, mid-1);
}
}
return -1;
}
Example 1.4 [Permutations]
KAIST- CS206 8
1.3 Data Abstraction
C data types
- fundamental data types
∙ char, int, float, double
∙ modifiers: short, long, signed, unsigned
∙ pointer
- grouping mechanisms
∙ array, struct
- user-defined data types
Definition: A data type is a collection of objects and a
set of operations that acts on those objects
Definition: An abstract data type (ADT) is a data type
that is organized in a way that the specification and
implementation are separated.
KAIST- CS206 9
1.3 Data Abstraction (cont)
Abstract data type
Creator/constructor, transformer, observers/reporters
Example 1.5 [ADT: natural_number]
ADT Natural_Number is
functions :
for all x, y ∈ Natural_Number, TRUE, FALSE ∈ Boolean
+, -, <, ==, = are the usual integer operations
Nat_No Zero() ::= 0
Boolean IsZero(x) ::= if (x == 0) IsZero = TRUE
else IsZero = FALSE
Nat_No Add(x,y) ::= if (x+y <= MAXINT) Add = x+y
else Add = MAXINT
Boolean Equal(x,y) ::= if (x == y) Equal = TRUE
else Equal = FALSE
end Natura_lNumber
KAIST- CS206 10
1.6 Performance Analysis
Estimating time and space that are machine
independent performance analysis
Complexity theory
- space complexity
- time complexity
Measuring machine dependent running time
Performance measurement
KAIST- CS206 11
1.6.1.1 Space Complexity
(1) Fixed space requirements
- do not depend on the size the program’s inputs and outputs
- example: instruction space, fixed-size structs, constants
(2) Variable space requirements
S(P) = c + Sp(I)
Example 1.6 (Program 1.12): Sabc(I) = 0
Example 1.7 (program 1.13): Ssum(n) = 0
line float sum(float *a, cost int n)
{
float s = 0;
for (int i=0; i<n; i++)
s += a[i];
return s;
}
Example 1.8 (program 1.14): Srsum(n) = 4(n+1)
KAIST- CS206 12
1.6.1.2 Time Complexity
Tp(n) = ca ADD(n) + csSUB(n) + clLDA(n) + cstSTA(n) + . . .
Definition: A program step is program segment whose execution
time is independent of the instance characteristics
Example 1.9 (counting program steps)
float sum(float *a, cost int n) {
float s = 0;
count++; // global variable
for (int i=0; i<n; i++) {
count++; // for
s += a[i];
count++; // assignment
}
count++; // last for
count++; // return
return s;
}
2n+3
KAIST- CS206 13
1.6.1.2 Time Complexity (count)
Example 1.9 (using step table) s/e freq. total steps
1 float sum(float *a, cost int n) { 0
2 float s = 0; 1 1 1
3 for (int i=0; i<n; i++) 1 n+1 n+1
4 s += a[i]; 1 n n
5 return s; 1 1 1
6 } 0 1 0
-----------
2n+3
KAIST- CS206 14
1.6.2 Time Complexity (cont)
Worst case complexity usually easy to check
Best case complexity usually easy to check
example: sequential search, binary search
Average case complexity usually hard to check
example: binary search
KAIST- CS206 15
1.6.1.3 Asymptotic Notation
Definition: [Big ‘’oh’’] f(n) = O(g(n))
Definition: [Omega] f(n) = (g(n))
Definition: [Theta] f(n) = (g(n))
Example 1.16 [Generating permutation]
O(n!) for worst, best, average cases
Example 1.17 [Binary search]
O(n log n) for worst case
KAIST- CS206 16
1.6.1.4 and 1.6.2 Practical Complexity
1
log n
n
n log n
n2
n3
2n
n!
Check Tables 1.7, 1.8 and Figure 1.3
Run time checking
- clock( );
- time(NULL);
Check example programs in textbook

Chapter 1 Basic Concepts

  • 1.
    KAIST- CS206 1 CS206A DATASTRUCTURE Spring, 2003
  • 2.
    KAIST- CS206 2 Instructor:김태환 tkim@cs.kaist.ac.kr x3542 조교: webpage (http://vlsisyn.kaist.ac.kr/course/cs206) 참조 Text: Fundamentals of Data Structures in C++ E. Horowitz, et al. 강의는 text 전 내용을 거의 모두 cover함. 매주 1 chapter 씩 진도 나가는 것으로 보면 됨. Grading: Homework/Quiz/Participation 30%, Mid-exam: 30%, Final: 40% 주의: 대리출석, 숙제copy, 시험부정행위는 무조건 F 학점
  • 3.
    KAIST- CS206 3 Chapter1 Basic Concepts
  • 4.
    KAIST- CS206 4 1.1System Life Cycle (1) Requirements ∙ A set of specifications that define the project (2) Analysis ∙ Break the problem down into manageable pieces ∙ bottom-up vs top-down (3) Design ∙ Description of how to achieve the specification ∙ Data objects abstract data types ∙ Operations algorithms ∙ Programming language independent
  • 5.
    KAIST- CS206 5 1.1System Life Cycle (cont) (4) Refinement and coding ∙ Representation of data objects and algorithms ∙ Program language dependent (5) Verification ∙ Correctness proofs, testing, error removal
  • 6.
    KAIST- CS206 6 1.2Algorithm Specification 1.2.1 Introduction Algorithm: input, output, definiteness, finiteness, effectiveness ∙ Representation of data objects and algorithms ∙ Program language dependent Example 1.1 [Selection sort] for (i=0; i<n; i++) { interchange list[i] and list[min]; } Example 1.2 [Binary search]
  • 7.
    KAIST- CS206 7 1.2Algorithm Specification (cont) 1.2.2 Recursive Algorithm Example 1.3 [Binary search] int binsearch(int list[], int x, int left, int right) { int mid; if (left < right) { mid = (left + right) / 2; switch (COMPARE(list[mid], searchnum)) { case -1: return binsearch(list, x, mid+1, right); case 0 : return mid; case 1 : return binsearch(list, x, left, mid-1); } } return -1; } Example 1.4 [Permutations]
  • 8.
    KAIST- CS206 8 1.3Data Abstraction C data types - fundamental data types ∙ char, int, float, double ∙ modifiers: short, long, signed, unsigned ∙ pointer - grouping mechanisms ∙ array, struct - user-defined data types Definition: A data type is a collection of objects and a set of operations that acts on those objects Definition: An abstract data type (ADT) is a data type that is organized in a way that the specification and implementation are separated.
  • 9.
    KAIST- CS206 9 1.3Data Abstraction (cont) Abstract data type Creator/constructor, transformer, observers/reporters Example 1.5 [ADT: natural_number] ADT Natural_Number is functions : for all x, y ∈ Natural_Number, TRUE, FALSE ∈ Boolean +, -, <, ==, = are the usual integer operations Nat_No Zero() ::= 0 Boolean IsZero(x) ::= if (x == 0) IsZero = TRUE else IsZero = FALSE Nat_No Add(x,y) ::= if (x+y <= MAXINT) Add = x+y else Add = MAXINT Boolean Equal(x,y) ::= if (x == y) Equal = TRUE else Equal = FALSE end Natura_lNumber
  • 10.
    KAIST- CS206 10 1.6Performance Analysis Estimating time and space that are machine independent performance analysis Complexity theory - space complexity - time complexity Measuring machine dependent running time Performance measurement
  • 11.
    KAIST- CS206 11 1.6.1.1Space Complexity (1) Fixed space requirements - do not depend on the size the program’s inputs and outputs - example: instruction space, fixed-size structs, constants (2) Variable space requirements S(P) = c + Sp(I) Example 1.6 (Program 1.12): Sabc(I) = 0 Example 1.7 (program 1.13): Ssum(n) = 0 line float sum(float *a, cost int n) { float s = 0; for (int i=0; i<n; i++) s += a[i]; return s; } Example 1.8 (program 1.14): Srsum(n) = 4(n+1)
  • 12.
    KAIST- CS206 12 1.6.1.2Time Complexity Tp(n) = ca ADD(n) + csSUB(n) + clLDA(n) + cstSTA(n) + . . . Definition: A program step is program segment whose execution time is independent of the instance characteristics Example 1.9 (counting program steps) float sum(float *a, cost int n) { float s = 0; count++; // global variable for (int i=0; i<n; i++) { count++; // for s += a[i]; count++; // assignment } count++; // last for count++; // return return s; } 2n+3
  • 13.
    KAIST- CS206 13 1.6.1.2Time Complexity (count) Example 1.9 (using step table) s/e freq. total steps 1 float sum(float *a, cost int n) { 0 2 float s = 0; 1 1 1 3 for (int i=0; i<n; i++) 1 n+1 n+1 4 s += a[i]; 1 n n 5 return s; 1 1 1 6 } 0 1 0 ----------- 2n+3
  • 14.
    KAIST- CS206 14 1.6.2Time Complexity (cont) Worst case complexity usually easy to check Best case complexity usually easy to check example: sequential search, binary search Average case complexity usually hard to check example: binary search
  • 15.
    KAIST- CS206 15 1.6.1.3Asymptotic Notation Definition: [Big ‘’oh’’] f(n) = O(g(n)) Definition: [Omega] f(n) = (g(n)) Definition: [Theta] f(n) = (g(n)) Example 1.16 [Generating permutation] O(n!) for worst, best, average cases Example 1.17 [Binary search] O(n log n) for worst case
  • 16.
    KAIST- CS206 16 1.6.1.4and 1.6.2 Practical Complexity 1 log n n n log n n2 n3 2n n! Check Tables 1.7, 1.8 and Figure 1.3 Run time checking - clock( ); - time(NULL); Check example programs in textbook