BASICS 1

42  Download (0)

Full text

(1)

Prof. Michael Tsai

2012/02/21

(2)

Variables

• 變數是什麼?

• 𝑥2 + 2𝑦 − 2 = 1

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

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

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

(4)

Data Types’ Operations

• Operations

+, -, *, /, %, ==

=, +=, -=

? :

sizeof, - (negative)

giligulu(int a, int b)

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

01000001 1 byte of memory:

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

(7)

Abstract Data Type

• “Abstract Data Type” (ADT):

• Separate the specifications from the representation and the implementation

Representation and Implementation Specification (Interface)

User

(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

(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

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

(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

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

(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 𝑛 ≥ 1 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

(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

(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

(16)

Task 1

min=i;

for(j=i;j<n;++j)

if (list[j]<list[min]) min=j;

Task 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 𝑙𝑖𝑠𝑡 0 ≤ 𝑙𝑖𝑠𝑡 1 ≤ ⋯ ≤ 𝑙𝑖𝑠𝑡,𝑛 − 1-.

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 𝑙𝑖𝑠𝑡 0 ≤ 𝑙𝑖𝑠𝑡 1 ≤ ⋯ ≤ 𝑙𝑖𝑠𝑡,𝑛 − 1-.

(18)

Example: Binary Search

• Input:

searchnum: the number to be found

list: sorted array, size n, and 𝑙𝑖𝑠𝑡 0 ≤ 𝑙𝑖𝑠𝑡 1 ≤ ⋯ ≤ 𝑙𝑖𝑠𝑡,𝑛 − 1-

• Output:

-1 if searchnum is not found in list

the index of searchnum in list if searchnum is found

(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;

(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 middle right

middle=(left+right)/2;

left=middle+1;

10 11

(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

(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好數字的array

searchnum: 要找的數字

left, right: 正在找的範圍左邊和右邊邊界

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

(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

(25)

怎麼評估一個程式寫得好不好?

6. 程式是否有效地使用主要及次要的儲存?

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

(26)

空間及時間複雜度

• 程式的空間複雜度:

程式執行完畢所需使用的所有空間(記憶體)

• 程式的時間複雜度:

程式執行完畢所需使用的(執行)時間

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

• 什麼是input size?

• 問題給的input的”元素數量”, 如:

Array大小

多項式最高項的次方

矩陣的長寬

二進位數的位元數目

(27)

空間複雜度

• 程式所需空間:

1. 固定的空間

• 和input/output的大小及內容無關

2. 變動的空間

• 和待解問題P的某個input instance I(某一個input)有 關

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

• 𝑆 𝑃 = 𝑐 + 𝑆 𝑃 (𝐼)

(28)

時間複雜度

一個程式P所需使用的時間:

Compile所需時間

執行時間 (execution time or run time)

• Compile時間: 固定的. (例外?)

C (and other compiled programming languages)

 One Compilation  Multiple Executions

• Run time: 𝑇𝑃

和input instance的特性有關!

(29)

如何得到 𝑇 𝑃 ?

1. 總執行時間

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

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

(30)

如何得到 𝑇 𝑃 ?

• 方法 1:

數出整個程式每個operation各總共花了多少時間.

Add Subtract Load Store

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

𝑐

𝑎

𝑐

𝑠

𝑐

𝑙

𝑐

𝑠𝑡

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

Is it good to use?

(方法1)

(31)

如何得到 𝑇 𝑃 ?

• 方法2:

把程式分解為一個一個步驟

每個步驟的執行時間變成跟input的特性無關了

然後數數看總共有幾個步驟

• 但是…

• 不同的程式步驟可能有不同的執行時間

a=2;

a=2*b+3*c/d-e+f/g/a/b/c;

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

(32)

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;

}

(33)

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)

(34)

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!

(35)

比較數學的方法—Asymptotic analysis

• “預測”當input的性質改變(通常是input size改變)時, 執行時 間的成長速度(growth of run time)

• 比較兩個做相同事情的程式的時間複雜度

• “程式步驟”不是那麼精確:

• “3n+3” 比“3n+5” 快?

• 通常 “3n+3”, “7n+2”, or “2n+15” 執行時間都相差不遠.

• 我們將使用一個比較不準確的方式來描述執行時間…

(36)

Example

• Program P and Q

𝑇𝑃 𝑛 = 𝑐1𝑛2 + 𝑐2𝑛

𝑇𝑄 𝑛 = 𝑐3𝑛

• N很大的時候, Q 會比 P 快, 不管𝑐1, 𝑐2, 𝑐3的數值是什麼.

• Example:

𝑐1 = 1, 𝑐2 = 2, 𝑐3 = 100 , then 𝑐1𝑛2 + 𝑐2𝑛2 > 𝑐3𝑛 for 𝑛 > 98.

𝑐1 = 1, 𝑐2 = 2, 𝑐3 = 1000 , then 𝑐1𝑛2 + 𝑐2𝑛2 > 𝑐3𝑛 for 𝑛 > 998.

• 需要知道 𝑐1, 𝑐2, 𝑐3 的數值嗎? Break even point constants are

close).

(37)

Asymptotic Notation – Big OH

Definition *Big “oh”+:

𝑂 𝑔 𝑛 =

*𝑓 𝑛 : there exist positive constants 𝑐 and 𝑛0 such that 0 ≤ 𝑓 𝑛 ≤ 𝑐𝑔 𝑛 for all 𝑛 ≥ 𝑛0+

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

“=“ is “is” not “equal”

Ο 𝑔 𝑛 = 𝑓(𝑛)

可以想成是Upper Bound

(王先生打了太多全壘打, 所以是upper bound)

(38)

Example

• 3𝑛 + 2 = Ο(𝑛) ?

• Yes, since 3𝑛 + 2 ≤ 4𝑛 for all 𝑛 ≥ 2.

• 3𝑛 + 3 = Ο(𝑛) ?

• Yes, since 3𝑛 + 3 ≤ 4𝑛 for all 𝑛 ≥ 3.

• 100𝑛 + 6 = Ο(𝑛) ?

• Yes, since 100𝑛 + 6 ≤ 101𝑛 for all 𝑛 ≥ 10.

• 10𝑛2 + 4𝑛 + 2 = Ο(𝑛2) ?

• Yes, since 10n2 + 4n + 2 ≤ 11𝑛2 for all 𝑛 ≥ 5.

𝑓 𝑛 ≤ 𝑐𝑔(𝑛) for all 𝑛, 𝑛 ≥ 𝑛

0

(39)

Example

• 1000𝑛2 + 100𝑛 − 6 = Ο 𝑛2 ?

• Yes, since 1000𝑛2 + 100𝑛 − 6 ≤ 1001𝑛2 for all 𝑛 ≥ 100.

• 6 ∗ 2𝑛 + 𝑛2 = Ο(2𝑛) ?

• Yes, since 6 ∗ 2𝑛 + 𝑛2 ≤ 7 ∗ 2n for all 𝑛 ≥ 4.

• 3𝑛 + 3 = Ο(𝑛2) ?

• Yes, since 3𝑛 + 3 ≤ 3𝑛2 for all 𝑛 ≥ 2.

• 10𝑛2 + 4𝑛 + 2 = Ο(𝑛4) ?

• Yes, since 10𝑛2 + 4𝑛 + 2 ≤ 10𝑛4 for all 𝑛 ≥ 2.

• 3𝑛 + 2 = Ο 1 ?

• No. Cannot find c and n0.

𝑓 𝑛 ≤ 𝑐𝑔(𝑛) for all 𝑛, 𝑛 ≥ 𝑛

0

(40)

The World of Big Oh

• Ο 1 constant

• Ο 𝑛 linear

• Ο 𝑛2 quadratic

• Ο 𝑛3 cubic

• Ο 2𝑛 exponential

• Ο 1 , Ο log 𝑛 , Ο 𝑛 , Ο 𝑛log 𝑛 , Ο 𝑛2 , Ο 𝑛3 , Ο 2𝑛

Faster Slower

(41)

Big Oh 是 Upper Bound

• 但是沒有說它是多好的upper bound

• 𝑛 = Ο(𝑛)

• 𝑛 = Ο(𝑛2)

• 𝑛 = Ο(𝑛2.5)

• 𝑛 = Ο(2𝑛)

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

• 3𝑛 + 3 = Ο(𝑛2)

• 3𝑛 + 3 = Ο 𝑛

(42)

A Useful Theorem

• Theorem: If 𝑓 𝑛 = 𝒂𝒎𝒏𝒎 + ⋯ + 𝑎1𝑛 + 𝑎0, then 𝑓 𝑛 = Ο(𝒏𝒎).

• Proof: (自己閱讀)

𝑓 𝑛 ≤ 𝑎𝑖 𝑛𝑖

𝑚

𝑖=0

= 𝑛𝑚 𝑎𝑖 𝑛𝑖−𝑚

𝑚

𝑖=0

≤ 𝑛𝑚 𝑎𝑖

𝑚

𝑖=0

, for all 𝑛 ≥ 1. So, 𝑓 𝑛 = Ο 𝑛𝑚 .

𝑛𝑖−𝑚 ≤ 1

Figure

Updating...

References

Related subjects :