CJU Chapter 1: Basic Concept 1 CHAPTER 1 BASIC CONCEPT Ellis Horowitz, Sartaj Sahni, and Susan...
Transcript of CJU Chapter 1: Basic Concept 1 CHAPTER 1 BASIC CONCEPT Ellis Horowitz, Sartaj Sahni, and Susan...
Chapter 1: Basic Concept 1
CJU
CHAPTER 1BASIC CONCEPT
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,
Computer Science Press, 1992.
Chapter 1: Basic Concept 2
CJU
Data Structure
1. What is data structure?Data structure 探討一群相關資料的資料表示方
法與資料運作方法2. What is purpose of learning data structure?
使用最有效率的方式 , 對一群相關資料進行處理3. How to Analyze and Design Data Structure?
1. 找出對該資料的各種運算2. 考慮最適當的 Data Structure, 使得各種運算的效率最佳3. 設計一個完整的 Algorithm
Chapter 1: Basic Concept 3
CJU
1.1 System Life Cycle
Large-Scale program contains many complex interacting parts
Solid foundation of the methodologies in1. data abstraction 2. algorithm specification3. performance analysis and measurement
Chapter 1: Basic Concept 4
CJU
1.1 System Life Cycle (cont’d)
1. Requirements: What inputs (needs), outputs (results)2. Analysis: bottom-up vs. top-down, divide and conquer3. Design: data objects and operations4. Refinement and Coding: choice the representation fo
r data object and write algorithm5. Verification
Program Proving Testing Debugging
5 phases of system life cycle:
Chapter 1: Basic Concept 5
CJU
1.2 Algorithm Specification Definition
An algorithm is a finite set of instructions that accomplishes a particular task.
( 以有限的程序步驟完成所指定的工作即所謂的演算法 ) Criteria must be met
Input: zero or more quantities are externally supplied. Output: at least one quantity is produced. definiteness: clear and unambiguous finiteness: terminate after a finite number of steps effectiveness: instruction is basic enough to be carried out
Chapter 1: Basic Concept 6
CJU
1.2 Algorithm Specification (cont’d)
Difference between an algorithm and a program : the program does not have to satisfy the fourth condition (Finiteness).
Describing an algorithm: natural language such as English will do. However, natural language is wordy to make a statement definite. That is where the Code of a program language fit in.
Flowchart: work well only for algorithm small and simple.
Chapter 1: Basic Concept 7
CJU
Problem Devise a program that sorts a set of n integers, where
n>= 1, from smallest to largest. Solution I: looks good, but it is not an algorithm
From those integers that are currently unsorted, find the smallest and place it next in the sorted list.
Solution II: an algorithm, written in partially C and English for (i= 0; i< n; i++){ Examine list[i] to list[n-1] and suppose that the
smallest integer is list[min]; Interchange list[i] and list[min]; }
Example:Translating a Problem into an Algorithm
Chapter 1: Basic Concept 8
CJUExample:Translating a Problem into an Algorithm (cont’d)
Solution III#define swap(x,y,t) ((t)= (x), (x)= (y), (y)= (t))
void sort(int list[], int n){ int i, j, min, temp; 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], temp);}
上面 swap意思即類似void swap(int *x, int *y){ int temp= *x; *x= *y; *y= temp;}
Chapter 1: Basic Concept 9
CJU
Example:Translating a Problem into an Algorithm (cont’d) Theorem 1.1
Function sort(list, n) correctly sorts a set of n>=1 integers. The result remains in list[0],…,list[n-1] such that list[0]<=list[1]<=list[2]<=…<=list[n-1].
Proof: 1. When the outer for loop completes its iteration for i=q, we have list[q]<=list[r], where q<r<n.2. On subsequence iterations, i>q and list[0] through list[q] are unchanged. Hence following the last iteration of the outer for loop (n-2), we have list[0]<=list[1]<=list[2]<=…<=list[n-1].
Chapter 1: Basic Concept 10
CJU
1.2.2 Recursive Algorithms Direct recursion
Functions call themselves Indirect recursion
Functions call other functions that invoke the calling function again
Any function that we can write using assignment, if-else, and while statements can be written recursively.
Chapter 1: Basic Concept 11
CJU
1.2.2 Recursive Algorithms
How to determine that express an algorithm recursively ? The problem itself is defined recursively Statements: if-else and while can be written recursi
vely How to design recursive algorithm
1. 找出遞迴關係找出遞迴關係才能夠對問題進行切割
2. 找出終止條件找出終止條件避免無窮遞迴下去
非常重要 !!
Chapter 1: Basic Concept 12
CJU
遞迴和迴圈的比較 遞迴的優點 : 簡潔易懂
缺點 : 執行效率差因為執行時需要對遞 迴副程式進行呼叫 ,由於呼叫遞迴 副程式需要處理額外的工作。
迴圈的優點 : 執行效率佳缺點 :有的問題不易用迴圈來表示 ,即使寫
得出 來也低階難懂。如河內塔問題。
哪些工作 ?Trace一個
Chapter 1: Basic Concept 13
CJU
在已排序的資料中找值 18 Sequential Search
第 7次找到
Binary Search
第 3次找到
3
4
6
8
9
15
18
3
4
6
8
9
15
18
Chapter 1: Basic Concept 14
CJU
Binary Search
int binsearch(int list[], int searchnum, int left, int right){// search list[0]<= list[1]<=...<=list[n-1] for searchnum int middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1: left= middle+ 1;
break; case 0: return middle; case 1: right= middle- 1; } } return -1;}
int compare(int x, int y){ if (x< y) return -1; else if (x== y) return 0; else return 1;}
int compare(int x, int y){ if (x< y) return -1; else if (x== y) return 0; else return 1;}
Chapter 1: Basic Concept 15
CJU
Recursive Implementation of Binary Search
int binsearch(int list[], int searchnum, int left, int right){// search list[0]<= list[1]<=...<=list[n-1] for searchnumint middle; if (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1:return binsearch(list, searchnum, middle+1, right); case 0: return middle; case 1: return binsearch(list, searchnum, left, middle- 1); } } return -1;}
Chapter 1: Basic Concept 16
CJU
河內塔問題 有三根木樁 ,在其中一根木樁上放上 N個鐵盤 ,規則是 :
1. 一次移動一個鐵盤 .2. 小的鐵盤永遠在大鐵盤的上方 .
請問將 N個鐵盤全部移動到另一鐵盤需要多少次 ??? 設三個木樁為 x , y , z,數量為 n,函式取名為為 Hanoi (n , x , y ,
z)1. 當 N為 1時表示只要再移動一次就完成工作 .也就是只要把 一個鐵盤從 x移到 z就完成工作 .2. 當 N不為 1時作 3個動作 .ㄅ .執行 Hanoi(n-1,x,z,y); ㄆ .將一個鐵盤從 x移到 z; ㄇ .執行 Hanoi(n-1,y,x,z);
Chapter 1: Basic Concept 17
CJU
Chapter 1: Basic Concept 18
CJU
河內塔程式碼void Hanoi(int n, char x, char y, char z){
if (n > 1) {
Hanoi(n-1,x,z,y);printf("Move disk %d from %c to %c.\n",n,x,z);Hanoi(n-1,y,x,z);
} else{
printf("Move disk %d from %c to %c.\n",n,x,z); }
}
Chapter 1: Basic Concept 19
CJU
Hanoi (n , x , y , z) n=4 的結果執行 Hanoi(3,x,z,y). 執行 Hanoi(2,x,y,z). 執行 Hanoi(1,x,z,y). 列印 Move disk 1 from x to y. 列印 Move disk 2 from x to z. 執行 Hanoi(1,y,x,z). 列印 Move disk 1 from y to z. 列印 Move disk 3 from x to y. 執行 Hanoi(2,z,x,y). 執行 Hanoi(1,z,y,x). 列印 Move disk 1 from z to x. 列印 Move disk 2 from z to y.
Chapter 1: Basic Concept 20
CJU
執行 Hanoi(1,x,z,y). 列印 Move disk 1 from x to y. 列印 Move disk 4 from x to z. 執行 Hanoi(3,y,x,z). 執行 Hanoi(2,y,z,x). 執行 Hanoi(1,y,x,z). 列印 Move disk 1 from y to z. 列印 Move disk 2 from y to x. 執行 Hanoi(1,z,y,x) 列印 Move disk 1 from z to x. 列印 Move disk 3 from y to z. 執行 Hanoi(2,x,y,z). 執行 Hanoi(1,x,z,y). 列印 Move disk 1 from x to y. 列印 Move disk 2 from x to z. 執行 Hanoi(1,y,x,z). 列印 Move disk 1 from y to z.
Chapter 1: Basic Concept 21
CJU
1.3 Data Abstraction
Data TypeA data type is a collection of objects and a set of operations that act on those objects.
Example of "int" Objects: 0, +1, -1, ..., Int_Max, Int_Min Operations: arithmetic(+, -, *, /, and %), testing (equalit
y/inequality), assigns, functions
Chapter 1: Basic Concept 22
CJU
1.3 Data Abstraction (cont’d)
Data encapsulation and Data abstraction Data encapsulation or Information hiding 是把資料物件的
內部程式碼與變數內容隱藏不被外部的使用者知道目的 : 讓往後可能的修改能夠局限在此程式單元之中 讓程式單元不需隨著 ADT 內部的表示方式的改變而修改外部的使
用方式 可提高程式的可重用度 ,可讀性 , 方便除錯…
Data abstraction 是將一個資料物件的 specification 和 implementation 分離目的 :只描述主要特徵而隱藏大部分的次要特徵 ,可以將複雜的物件加以簡化
Chapter 1: Basic Concept 23
CJU
Abstract Data Type Abstract Data Type
An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations.
ADT Operations — only the operation name and its parameters are visible to the user — through interface
Why abstract data type ? implementation-independent
不管如何 (How)作,只管作了什麼 (What) Abstraction is …
Generalization of operations with unspecified implementation
Chapter 1: Basic Concept 24
CJU
Abstract data type model
Chapter 1: Basic Concept 25
CJU
Classifying the Functions of a ADT
Creator/constructor Create a new instance of the designated type
Transformers Also create an instance of the designated type
by using one or more other instances Observers/reporters
Provide information about an instance of the type, but they do not change the instance
Chapter 1: Basic Concept 26
CJU
*Structure 1.1:Abstract data type Natural_Number (p.17)structure Natural_Number is (denoted by Nat_No) objects: an ordered subrange of the integers starting at zero and ending at the maximum integer (INT_MAX) on the computer functions: for all x, y Nat_Number; TRUE, FALSE Boolean and where +, -, <, and == are the usual integer operations. Nat_No Zero ( ) ::= 0 Boolean Is_Zero(x) ::= if (x) return FALSE else return TRUE Nat_No Add(x, y) ::= if ((x+y) <= INT_MAX) return x+y else return INT_MAX Boolean Equal(x,y) ::= if (x== y) return TRUE else return FALSE Nat_No Successor(x) ::= if (x == INT_MAX) return x else return x+1 Nat_No Subtract(x,y) ::= if (x<y) return 0 else return x-y end Natural_Number
Creator
Observer
Transformer
Chapter 1: Basic Concept 27
CJU
Play with ADT Add(Zero(),y) ::= return y Add(Successor(x),y) ::=
return Successor(Add(x,y))
Equal(Zero(),Zero()) ::= return TRUE Equal(Successor(x),Zero()) ::=
return FALSE Equal(Successor(x), Successor(y)) ::=
return Equal(x, y)
Chapter 1: Basic Concept 28
CJU
Play with ADT (cont’d)
0Zero() 1Successor(Zero()) 2Successor(Successor(Zero())) 1 + 2 = ?
… 1 + 2 == 3 ?
Chapter 1: Basic Concept 29
CJU
1.4 Performance Analysis (machine independent)
Space and Time Does the program efficiently use primary and secondary
storage? Is the program's running time acceptable for the task?
Evaluate a program generally Does the program meet the original specifications of the
task? Does it work correctly? Does the program contain documentation that show
how to use it and how it works? Does the program effectively use functions to create
logical units? Is the program's code readable?
Chapter 1: Basic Concept 30
CJU
Performance Analysis(Cont.) Evaluate a program
Meet specifications, Work correctly, Good user-interface, Well-documentation,Readable, Effectively use functions, Running time acceptable, Efficiently use space/storage
How to achieve them? Good programming style, experience, and practice Discuss and think
Practice Practice Prac
tice
ThinkThinkThink
Chapter 1: Basic Concept 31
CJU
S(P)= c+ Sp(I)
The space needed is the sum of Fixed space and Variable space
Fixed space: c Includes the instructions, variables, and consta
nts Independent of the number and size of I/O
Variable space: Sp(I) Includes dynamic allocation, functions' recursio
n Total space of any program
S(P)= c+ Sp(Instance)
P: a programI: instance
(input, output)
Chapter 1: Basic Concept 32
CJU
*Program 1.9: Simple arithmetic function (p.19)
float abc(float a, float b, float c){ return a + b + b * c + (a + b - c) / (a + b) + 4.00; }
*Program 1.10: Iterative function for summing a list of numbers (p.20)
float sum(float list[ ], int n){ float tempsum = 0; int i; for (i = 0; i<n; i++) tempsum += list [i]; return tempsum;}
Sabc(I) = 0
Ssum(I) = 0
Recall: pass the address of thefirst element of the array &pass by value
Chapter 1: Basic Concept 33
CJU
*Program 1.11: Recursive function for summing a list of numbers (p.20)
float rsum(float list[ ], int n){ if (n) return rsum(list, n-1) + list[n-1]; return 0; }
*Figure 1.1: Space needed for one recursive call of Program 1.11 (p.21) (以 16bit x86 small/near為例 )
Type Name Number of bytes parameter: float parameter: integer return address:(used internally)
list [ ] n
2 2 2(unless a far address)
TOTAL per recursive call 6
Ssum(I)=Ssum(n)=6n
*
Chapter 1: Basic Concept 34
CJU
Time Complexity Total time
T(P)= compile time + run (or execution) time May run it many times without recompilation. Run ti
me How to evaluate?
+ - * / … (最細的估計 ) Use the system clock 直接用量的比較省事 Number of steps performed (中等的估計 )
machine-independent Instance及所費時間函數的趨勢 (粗略的估計 , p.39)
Definition of a program step A program step is a syntactically or semantically meaningful
program segment whose execution time is independent of the instance characteristics
Chapter 1: Basic Concept 35
CJU
Methods to compute the step count
Introduce variable count into programs Tabular method
Determine the total number of steps contributed by each statement steps_per_statement * frequency_for_that_statement s/e, steps/execution
add up the contribution of all statements
Chapter 1: Basic Concept 36
CJU
*Program 1.12: Program 1.10 with count statements (p.23)
float sum(float list[ ], int n){ float tempsum = 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; }
2n + 3 steps
Iterative summing of a list of numbers
Chapter 1: Basic Concept 37
CJU
*Program 1.13: Simplified version of Program 1.12 (p.23)
float sum(float list[ ], int n){ float tempsum = 0; int i; for (i = 0; i < n; i++) count += 2; count += 3; return 0;}
2n + 3 steps
Chapter 1: Basic Concept 38
CJU
*Program 1.14: Program 1.11 with count statements added (p.24)
float rsum(float list[ ], int n){
count++; /*for if conditional */if (n) {
count++; /* for return and rsum invocation */ return rsum(list, n-1) + list[n-1]; } count++; return list[0];}
2n+2 steps
Recursive summing of a list of numbers
Chapter 1: Basic Concept 39
CJU
*Program 1.15: Matrix addition (p.25)
void add( int a[ ] [MAX_SIZE], int b[ ] [MAX_SIZE], int c [ ] [MAX_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]; }
Matrix addition
Chapter 1: Basic Concept 40
CJU
*Program 1.16: Matrix addition with count statements (p.25)
void add(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE], int row, int cols ){ int i, j; for (i = 0; i < rows; i++){ count++; /* for i for loop */ for (j = 0; j < cols; j++) { count++; /* for j for loop */ c[i][j] = a[i][j] + b[i][j]; count++; /* for assignment statement */ } count++; /* last time of j for loop */ } count++; /* last time of i for loop */}
2rows * cols + 2 rows + 1
Chapter 1: Basic Concept 41
CJU
*Program 1.17: Simplification of Program 1.16 (p.26)
void add(int a[ ][MAX_SIZE], int b [ ][MAX_SIZE], int c[ ][MAX_SIZE], int rows, int cols){ int i, j; for( i = 0; i < rows; i++) { for (j = 0; j < cols; j++) count += 2; count += 2; } count++; }
2rows cols + 2rows +1
Suggestion: Interchange the loops when rows >> cols
Chapter 1: Basic Concept 42
CJU
*Figure 1.2: Step count table for Program 1.10 (p.26)
Statement s/e Frequency Total steps
float sum(float list[ ], int n){ float tempsum = 0; int i; for(i=0; i <n; i++)
tempsum += list[i]; return tempsum;}
0 0 00 0 01 1 10 0 01 n+1 n+11 n n1 1 10 0 0
Total 2n+3
Tabular Method
steps/execution
Iterative function to sum a list of numbers
Chapter 1: Basic Concept 43
CJU
*Figure 1.3: Step count table for recursive summing function (p.27)
Statement s/e Frequency Total steps
float rsum(float list[ ], int n){ if (n) return rsum(list, n-1)+list[n-1]; return list[0];}
0 0 00 0 01 n+1 n+11 n n1 1 10 0 0
Total 2n+2
Recursive Function to sum of a list of numbers
Chapter 1: Basic Concept 44
CJU
*Figure 1.4: Step count table for matrix addition (p.27)
Statement s/e Frequency Total steps
Void add (int a[ ][MAX_SIZE]‧ ‧ ‧ ){ int i, j; for (i = 0; i < row; i++) for (j=0; j< cols; j++) c[i][j] = a[i][j] + b[i][j];}
0 0 00 0 00 0 01 rows+1 rows+11 rows‧ (cols+1) rows‧ cols+rows1 rows‧ cols rows‧ cols0 0 0
Total 2rows‧ cols+2rows+1
Matrix Addition
Chapter 1: Basic Concept 45
CJU
Asymptotic Notation(O, , ) Exact step count
Compare the time complexity of two programs that computing the same function
Difficult task of most of programs Asymptotic notation
Big “oh” upper bound(current trend)
Omega lower bound
Theta upper and lower bound
Chapter 1: Basic Concept 46
CJU
Asymptotic Notation O Definition
f(n)= O(g(n)) iff there exist positive constants c and n0 such that f(n)<= cg(n) for all n, n>= n0
Examples 3n+ 2= O(n) as 3n+ 2<= 4n for all n>= 2 10n2+ 4n+ 2= O(n2) as 10n2+ 4n+ 2<= 11n2 for n>= 5 3n+2<> O(1), 10n2+ 4n+ 2<> O(n)
Remarks g(n) is upper bound, the least? (估 algorithm, 作答時… )
n=O(n2)=O(n2.5)= O(n3)= O(2n) O(1): constant, O(n): linear, O(n2): quadratic, O(n3): cubic,
and O(2n): exponential (各舉一例 )
Chapter 1: Basic Concept 47
CJU
Asymptotic Notation Definition
f(n)= (g(n)) iff there exist positive constants c and n
0 such that f(n)>= cg(n) for all n, n>= n0
Examples 3n+ 2= (n) as 3n+ 2>= 3n for n>= 1 10n2+ 4n+ 2= (n2) as 10n2+4n+ 2>= n2 for n>= 1 6*2n+ n2= (2n) as 6*2n+ n2 >= 2n for n>= 1
Remarks lower bound, the largest ? (used for problem)
3n+3= (1), 10n2+4n+2= (n); 6*2n+ n2= (n100) Theorem
If f(n)= amnm+ ...+ a1n+ a0 and am> 0, then f(n)= (nm)
Chapter 1: Basic Concept 48
CJU
Asymptotic Notation
Definition f(n)= (g(n)) iff there exist positive constants c1, c2, and n0 su
ch that c1g(n)<= f(n) <= c2g(n) for all n, n>= n0
Examples 3n+2=(n) as 3n+2>=3n for n>1 and 3n+2<=4n for all n>= 2 10n2+ 4n+ 2= (n2); 6*2n+n2= (2n)
Remarks Both an upper and lower bound 3n+2!=(1); 10n2+4n+ 2!= (n)
Theorem If f(n)= amnm+ ... +a1n+ a0 and am> 0, then f(n)= (nm)
Chapter 1: Basic Concept 49
CJU
Example of Time Complexity Analysis
Statement Asymptotic complexity
void add(int a[][Max.......) 0{ 0 int i, j; 0 for(i= 0; i< rows; i++) (rows) for(j=0; j< cols; j++) (rows*cols) c[i][j]= a[i][j]+ b[i][j]; (rows*cols)} 0
Total (rows*cols)
Chapter 1: Basic Concept 50
CJU
Example of Time Complexity Analysis(Cont.)
int binsearch(int list[], int left, int right){ int middle; while (left<= right){ middle= (left+ right)/2; switch(compare(list[middle], searchnum)){ case -1: left= middle+ 1;
break; case 0: return middle; case 1: right= middle- 1; } } return -1;}
worst case (log n)best case (1)
Chapter 1: Basic Concept 51
CJU
*Figure 1.9:Times on a 1 billion instruction per second computer(p.40)
Chapter 1: Basic Concept 52
CJU
*Figure 1.8:Plot of function values(p.39)
nlogn
n
logn
Chapter 1: Basic Concept 53
CJU
注意 : 一般常以 average case 和 worst case 來討論 (真的要怎麼作 ?)
Estimate average case : 用隨機的測試資料 ,取結果的平均時間 Estimate worst case : 用隨機的測試資料 ,取結果的最大時間
There are actually two different methods for timing events in C. Figure 1.10 shows the major differences between these two methods. Method 1 uses clock to time events. This function gives the amount of processor time t
hat has elapsed since the program began running. Method 2 uses time to time events. This function returns the time, measured in second
s, as the built-in type time_t. The exact syntax of the timing functions varies from computer to computer and also de
pends on the operating system and compiler in use.
Method 1 Method 2
Start timing start=clock(); start=time(NULL);
Stop timing stop=clock(); stop=time(NULL);
Type returned clock_t time_t
Result in seconds
duration=((double)(stop-start)/CLK_TCK;
duration=(double)difftime(stop, start);
Figure 1.10
Performance Measurement (Performance Measurement (machine machine dependent)dependent)
CLOCKS_PER_SEC