Basics 1

42
BASICS 1 Prof. Michael Tsai 2012/02/21

description

Basics 1. 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 values having pre-defined characteristics . - PowerPoint PPT Presentation

Transcript of Basics 1

Page 1: Basics 1

BASICS 1Prof. Michael Tsai2012/02/21

Page 2: Basics 1

2

Variables

• 變數是什麼 ?

• 變數 : Names(x 和 y) 對應到某個 data

• 每個變數可能有不同的資料型態 (data type)

Page 3: Basics 1

3

Data Type• What is a data type?• A data type in a programming language is a set of data with

values 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];

Page 4: Basics 1

4

Data Types’ Operations• Operations• +, -, *, /, %, ==• =, +=, -=• ? :• sizeof, - (negative)• giligulu(int a, int b)

Page 5: Basics 1

5

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?

010000011 byte of memory:

Page 6: Basics 1

6

Data type• Q: Do we need to know about the representation of a data

type?• 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

32-bit long)

Page 7: Basics 1

7

Abstract Data Type• “Abstract Data Type” (ADT):• Separate the specifications from the representation

and the implementation

Representation and Implementation

Specification (Interface)

User

Page 8: Basics 1

8

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

Page 9: Basics 1

9

What is a Data Structure?• An organization of information, usually in memory, for better

algorithm efficiency. ( 演算法跑起來比較快 )

• Or, a way to store and organize data in order to facilitate access and modifications. ( 比較好存取和更改 )

• 例子 : 存多項式 :

• 又可分為 :• 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

Page 10: Basics 1

10

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: 每一個指令都必須是簡單可以直接執行的 ( 必須可以

執行 )

Page 11: Basics 1

11

例子• Statement 1: “Is n=2 the largest value of n for which there exist positive integers x, y, and z such that 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

Page 12: Basics 1

12

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. 打開火爐 , …

Page 13: Basics 1

13

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

Page 14: Basics 1

14

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

Page 15: Basics 1

15

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

Page 16: Basics 1

16

Task 1

min=i;for(j=i;j<n;++j) if (list[j]<list[min]) min=j;

Task 1

Page 17: Basics 1

17

How do we prove that it is correct?• ( 這一頁回家自己看 )

• [Theorem] Function sort(list,n) correctly sorts a set of n>=1 integers. The result remains in list[0], …, list[n-1] such that .

• Proof: When the outer for loop completes its iteration for i=q, we have . Further, 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 .

Page 18: Basics 1

18

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

Page 19: Basics 1

19

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;

Page 20: Basics 1

20

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;

left rightmiddle

middle=(left+right)/2;

left=middle+1;

10 11

Page 21: Basics 1

21

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

Page 22: Basics 1

22

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 好數字的 arraysearchnum: 要找的數字left, right: 正在找的範圍左邊和右邊邊界

Page 23: Basics 1

23

怎麼評估一個程式寫得好不好 ?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

use it and how it works?4.Does the program effectively use functions to create logical

units?5.Is the program’s code readable?

Page 24: Basics 1

24

怎麼評估一個程式寫得好不好 ?6.Does the program efficiently use primary and secondary

storage?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

Page 25: Basics 1

25

怎麼評估一個程式寫得好不好 ?6. 程式是否有效地使用主要及次要的儲存 ?

7.程式的執行時間是否適合所需解決的工作內容 ?

Time complexity

Space complexity

Page 26: Basics 1

26

空間及時間複雜度• 程式的空間複雜度 :• 程式執行完畢所需使用的所有空間 ( 記憶體 )

• 程式的時間複雜度 :• 程式執行完畢所需使用的 ( 執行 ) 時間

• Goal: 找出執行時間 / 使用空間”如何”隨著 input size 變長 ( 成長的有多快 )

• 什麼是 input size? • 問題給的 input 的”元素數量” , 如 :• Array 大小• 多項式最高項的次方• 矩陣的長寬• 二進位數的位元數目

Page 27: Basics 1

27

空間複雜度•程式所需空間 :

1. 固定的空間• 和 input/output 的大小及內容無關

2. 變動的空間• 和待解問題 P 的某個 input instance I( 某一個 input)

有關• 跟 recursive function 會使用到的額外空間有關

Page 28: Basics 1

28

時間複雜度• 一個程式 P 所需使用的時間 :• Compile 所需時間• 執行時間 (execution time or run time)

• Compile 時間 : 固定的 . ( 例外 ?)• C (and other compiled programming languages)

One Compilation Multiple Executions

• Run time: • 和 input instance 的特性有關 !

Page 29: Basics 1

29

如何得到 ?1. 總執行時間

2. 所執行的程式指令的數目

3. 比較數學的方法 ( 使用 function 來代表程式執行的時間 )

Page 30: Basics 1

30

如何得到 ?• 方法 1:• 數出整個程式每個 operation 各總共花了多少時間 .

Add Subtract Load Store

ADD(n) SUB(n) LDA(n) STA(n)

𝑇 𝑃 (𝑛)=𝑐𝑎𝐴𝐷𝐷 (𝑛 )+𝑐𝑠𝑆𝑈𝐵 (𝑛 )+𝑐 𝑙𝐿𝐷𝐴 (𝑛 )+𝑐𝑠𝑡𝑆𝑇𝐴 (𝑛)

Is it good to use?( 方法 1)

Page 31: Basics 1

31

如何得到 ?• 方法 2:• 把程式分解為一個一個步驟• 每個步驟的執行時間變成跟 input 的特性無關了• 然後數數看總共有幾個步驟

• 但是…• 不同的程式步驟可能有不同的執行時間• a=2;• a=2*b+3*c/d-e+f/g/a/b/c;

• 要數出所有步驟的數目需要解某一個 input instance

Page 32: Basics 1

32

Examplefloat sum(float list[], int n) {

float tempsum = 0; count++; //assignmentint i;for (i=0;i<n;i++) {

count++; // for looptempsum+=list[i]; count++; //assignment

}count++; //last iteration of forcount++; return tempsum;

}

Page 33: Basics 1

33

Examplefloat 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)

Page 34: Basics 1

34

Example 2float rsum(float list[], int n) {

count++; // ifif (n) {

count++; //returnreturn rsum(list,n-1)+list[n-1];

}count++; //returnreturn list[0];

}

count = 2n+2 (steps)

2n+2 < 2n+3.

Does this mean rsum is faster than sum ?

No!

Page 35: Basics 1

35

比較數學的方法— Asymptotic analysis• “ 預測”當 input 的性質改變 ( 通常是 input size 改變 ) 時 ,

執行時間的成長速度 (growth of run time)• 比較兩個做相同事情的程式的時間複雜度

• “ 程式步驟”不是那麼精確 :• “3n+3” 比“ 3n+5” 快 ? • 通常 “ 3n+3”, “7n+2”, or “2n+15” 執行時間都相差不遠 .• 我們將使用一個比較不準確的方式來描述執行時間…

Page 36: Basics 1

36

Example• Program P and Q

• N 很大的時候 , Q 會比 P 快 , 不管的數值是什麼 .• Example: • , then for .• , then for .

• 需要知道 的數值嗎 ?Break even point

Usually no (if the constants are

close).

Page 37: Basics 1

37

Asymptotic Notation – Big OH• Definition [Big “oh”]:•

• “f of n is big oh of g of n” ( 是集合的成員 )• “=“ is “is” not “equal”•

• 可以想成是 Upper Bound(王先生打了太多全壘打 , 所以是 upper bound)

Page 38: Basics 1

38

Example• ?• Yes, since for all .• ?• Yes, since for all .• ?• Yes, since for all .• ?• Yes, since for all .

for all

Page 39: Basics 1

39

Example• ?• Yes, since for all .• ?• Yes, since for all .• ?• Yes, since for all .• ?• Yes, since for all .• ?• No. Cannot find

for all

Page 40: Basics 1

40

The World of Big Oh• constant• linear• quadratic• cubic• exponential

• , , , ,

Faster Slower

Page 41: Basics 1

41

Big Oh 是 Upper Bound• 但是沒有說它是多好的 upper bound

• 通常我們會把它取得越小 (緊 )越好 .

Page 42: Basics 1

42

A Useful Theorem• Theorem: If , then .• Proof: ( 自己閱讀 )

, for all . So,

𝑛𝑖−𝑚≤1