Prof. Michael Tsai
2012/02/21
Variables
• 變數是什麼 ?
• 變數 : Names(x 和 y) 對應到某個 data
• 每個變數可能有不同的資料型態 (data type)
•
Data Type
• What is a data type?
• A data type in a programming language is a set of data with value s having pre-defined characteristics.
• A data type is a collection of objects and a set of operations that act on those objects.
• 每種 data type 有所占的記憶體大小及可表示的資料數值範圍
• Data types in C
• char, int, float, long, double (unsigned, signed, …)
• Array
• Structure
(User-defined)
struct { int a;
int b;
char str[16];
int * iptr;
} blah;
int iarray[16];
Data Types’ Operations
• Operations
• +, -, *, /, %, ==
• =, +=, -=
• ? :
• sizeof, - (negative)
• giligulu(int a, int b)
Data Type
• Representation of the objects of the data type
• Example: char
• char blah=‘A’; (‘A’: ASCII code is 65(dec), or 0x41 (hex))
Q: The maximum number which can be represented with a char variable?
A: 255.
• How about char, int, long, float?
01000001 01000001 1 byte of memory:
Data type
• Q: Do we need to know about the representation of a data typ e?
• A: It depends.
• Algorithms could be made more efficient.
• But, If the representation of the data type is changed, the program needs to be verified, revised, or completely
re- written
.囧
• Porting to a different platform (x86, ARM, embedded system, …)
• Changing the specification of a program or a library (ex. 16-bit int 3 2-bit long)
Abstract Data Type
• “Abstract Data Type” (ADT):
• Separate the specifications from the representation an d the implementation
Representation and Implementation Specification (Interface)
User
Abstract Data Type
• Specifications:
• Operations:
• Name of the function and the description of what the function does
• The type of the argument(s)
• The type of the result(s) (return value)
• Data (usually hidden)
• Function categories:
• Creator/constructor
• Transformers
• Observer/reporter
What is a Data Structure?
•
An organization of information, usually in memory, for better algorith m efficiency. ( 演算法跑起來比較快 )
•
Or, a way to store and organize data in order to facilitate access and m odifications. ( 比較好存取和更改 )
•
例子 : 存多項式 :
•
又可分為 :
• Linear data structure: 必須循序地存取 ( 如 linked list, stack, queue)
• Non-linear data structure: 可以不循序的存取 ( 如 tree, graph)
0 1 2 3 4 5 6
27 11 -3 0 0 2 0
2 �
5− 3 �
2+11 �+27
What is an algorithm?
• “The step-by-step instructions to solve a given problem”
• 解決某一個問題的詳細、一步一步地指令
• “A computable set of steps to achieve a desired result.”
• All algorithm must satisfy the following criteria:
• Input: 外部給的資訊 ( 可以是零個或多個 )
• Output: 產生的結果 ( 至少一個 )
• Definiteness: 每一個指令都是清楚而不模糊的
• Finiteness: 所有的狀況下 ( 所有的 input), 演算法會在有限步驟之後 結束
• Effectiveness: 每一個指令都必須是簡單可以直接執行的 ( 必須可以 執行 )
例子
• Statement 1: “Is n=2 the largest value of n for whic h there exist positive integers x, y, and z such th at has a solution?”
• Statement 2: “Store 5 divided by zero into x and go to statement ㄆ .”
• Which criterion do they violate?
• Input
• Output
• Definiteness
• Finiteness
• Effectiveness
•
How do we describe an algorithm?
• Human language (English, Chinese, …)
• Programming language
• A mix of the above
1. 拿平底鍋 2. 拿沙拉油
1. 我們有油嗎 ?
1. 有的話 , 倒一茶匙的沙拉油到鍋子裡 2. 沒有的話 , 我們想要買油嗎 ?
1. 是的話 , 就去全聯買一罐沙拉油 2. 如果不想的話 , 只好先不煮了 . 3. 打開火爐 , …
Example: Selection Sort
•
Integers are stored in an array, list. The i-th integer is stored in list [i], 0<i<n.
•
Goal: Devise a program to sort a set of integers
•
Solution: From those integers that are currently unsorted, find the smallest and place it next in the sorted list.
•
ㄅ ㄆ 1
1 ㄆ 2 ㄅ
1 2 ㄆ ㄅ
Example: Selection Sort
• First attempt:
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];
}
Task 1
Task 2
Task 2
void swap(int *x, int *y) { int temp = *x;
*x=*y;
*y=temp;
}
Or
#define SWAP(x,y,t) ((t)=(x), (x)=(y), (y)=(t)) Task 2
Task 1
min=i;
for(j=i;j<n;++j)
if (list[j]<list[min]) min=j;
Task 1
How do we prove that it is correct?
• ( 這一頁回家自己看 )
• [Theorem] Function sort(list,n) correctly sorts a set of n>=1 integers. The res ult remains in list[0], …, list[n-1] such that .
• Proof: When the outer for loop completes its iteration for i=q, we have . Fur ther, on subsequent iterations, i>q and list[0] through list[q] are unchanged.
Hence following the last iteration of the outer for loop (i.e., i=n-2), we have .
•
Example: Binary Search
• Input:
• searchnum: the number to be found
• list: sorted array, size n, and
• Output:
• -1 if searchnum is not found in list
• the index of searchnum in list if searchnum is found
•
Example:
1 3 4 4 6 7 11 13 13 13 18 19
0 1 2 3 4 5 6 7 8 9 10 11
searchnum=13;
Example:
1 3 4 4 6 7 11 13 13 13 18 19
0 1 2 3 4 5 6 7 8 9
searchnum=13;
lef middle right
middle=(left+right)/2;
left=middle+1;
10 11
Example:
1 3 4 4 6 7 11 13 13 13 18 19
0 1 2 3 4 5 6 7 8 9
searchnum=5;
return -1;
10 11
int binsearch(int list[], int searchnum, 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;
}
list: 存 sort 好數字的 array searchnum: 要找的數字
lef, right: 正在找的範圍左邊和右邊邊界
怎麼評估一個程式寫得好不好 ?
1.Does the program meet the original specifications of the task?
2.Does it work correctly?
3.Does the program contain documentation that shows how to u se it and how it works?
4.Does the program effectively use functions to create logical uni
ts?
5.Is the program’s code readable?
怎麼評估一個程式寫得好不好 ?
6.Does the program efficiently use primary and secondary stor age?
Primary storage: memory?
Secondary storage: Hard drive, flash disk, etc.
7.Is the program’s running time acceptable for the task?
Example: Network intrusion detection system
(1) 99.8% detection rate, 50 minutes to finish analysis of a minute of traffic
(2) 85% detection rate, 20 seconds to finish analysis of a minute of traffic
怎麼評估一個程式寫得好不好 ?
6. 程式是否有效地使用主要及次要的儲存 ?
7.程式的執行時間是否適合所需解決的工作內容 ?
Time complexity
Space complexity
空間及時間複雜度
• 程式的空間複雜度 :
• 程式執行完畢所需使用的所有空間 ( 記憶體 )
• 程式的時間複雜度 :
• 程式執行完畢所需使用的 ( 執行 ) 時間
• Goal: 找出執行時間 / 使用空間”如何”隨著 input size 變長 ( 成長的有多快 )
• 什麼是 input size?
• 問題給的 input 的”元素數量” , 如 :
• Array 大小
• 多項式最高項的次方
• 矩陣的長寬
• 二進位數的位元數目
空間複雜度
• 程式所需空間 :
1. 固定的空間
• 和 input/output 的大小及內容無關
2. 變動的空間
• 和待解問題 P 的某個 input instance I( 某一個 input) 有關
• 跟 recursive function 會使用到的額外空間有關
•
時間複雜度
• 一個程式 P 所需使用的時間 :
• Compile 所需時間
• 執行時間 (execution time or run time)
• Compile 時間 : 固定的 . ( 例外 ?)
• C (and other compiled programming languages)
One Compilation Multiple Executions
• Run time:
• 和 input instance 的特性有關 !
•
如何得到 ?
1. 總執行時間
2. 所執行的程式指令的數目
3. 比較數學的方法 ( 使用 function 來代表程式執行的時間 )
如何得到 ?
• 方法 1:
• 數出整個程式每個 operation 各總共花了多少時間 .
Add Subtract Load Store
ADD(n) SUB(n) LDA(n) STA(n)
Add Subtract Load Store
ADD(n) SUB(n) LDA(n) STA(n)
�
�( �)=�
���� (�)+�
���� (�)+�
����(�)+�
�����(�)
Is it good to use?
( 方法 1)
Is it good to use?
( 方法 1)
如何得到 ?
• 方法 2:
• 把程式分解為一個一個步驟
• 每個步驟的執行時間變成跟 input 的特性無關了
• 然後數數看總共有幾個步驟
• 但是…
• 不同的程式步驟可能有不同的執行時間
• a=2;
• a=2*b+3*c/d-e+f/g/a/b/c;
• 要數出所有步驟的數目需要解某一個 input instance
Example
float sum(float list[], int n) {
float tempsum = 0; count++; //assignment int i;
for (i=0;i<n;i++) {
count++; // for loop
tempsum+=list[i]; count++; //assignment
}
count++; //last iteration of for count++; return tempsum;
}
Example
float sum(float list[], int n) {
float tempsum = 0;
int i;
for (i=0;i<n;i++) {
count+=2;
}
count+=3;
return tempsum;
}
count = 2n+3 (steps)
Example 2
float rsum(float list[], int n) {
count++; // if if (n) {
count++; //return
return rsum(list,n-1)+list[n-1];
}
count++; //return return list[0];
}
count = 2n+2 (steps)
2n+2 < 2n+3.
Does this mean rsum is faster than sum ?
No!
比較數學的方法— Asymptotic analysis
• “ 預測”當 input 的性質改變 ( 通常是 input size 改變 ) 時 , 執行時間的成長速度 (growth of run time)
• 比較兩個做相同事情的程式的時間複雜度
• “ 程式步驟”不是那麼精確 :
• “3n+3” 比“ 3n+5” 快 ?
• 通常 “ 3n+3”, “7n+2”, or “2n+15” 執行時間都相差不遠 .
• 我們將使用一個比較不準確的方式來描述執行時間…
Example
• Program P and Q
• N 很大的時候 , Q 會比 P 快 , 不管的數值是什麼 .
• Example:
• , then for .
• , then for .
• 需要知道 的數值嗎 ?
•
Break even point constants are
close).
constants are close).
Asymptotic Notation – Big OH
• Definition [Big “oh”]:
•
• “f of n is big oh of g of n” ( 是集合的成員 )
• “=“ is “is” not “equal”
•
• 可以想成是 Upper Bound
( 王先生打了太多全壘打 , 所以是 upper bo und)
•
Example
• ?
• Yes, since for all .
• ?
• Yes, since for all .
• ?
• Yes, since for all .
• ?
• Yes, since for all .
•
for all
for all
Example
• ?
• Yes, since for all .
• ?
• Yes, since for all .
• ?
• Yes, since for all .
• ?
• Yes, since for all .
• ?
• No. Cannot find
•
for all
for all
The World of Big Oh
• constant
• linear
• quadratic
• cubic
• exponential
• , , , ,
•
Faster Slower
Big Oh 是 Upper Bound
• 但是沒有說它是多好的 upper bound
• 通常我們會把它取得越小 ( 緊 ) 越好 .
•
A Useful Theorem
• Theorem: If , then .
• Proof: ( 自己閱讀 )
, for all . So,
•