Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices.

Slides:



Advertisements
Similar presentations
Chapter 2 Combinatorial Analysis 主講人 : 虞台文. Content Basic Procedure for Probability Calculation Counting – Ordered Samples with Replacement – Ordered.
Advertisements

663 Chapter 14 Integral Transform Method Integral transform 可以表示成如下的積分式的 transform  kernel Laplace transform is one of the integral transform 本章討論的 integral.
Course 1 演算法: 效率、分析與量級 Algorithms: Efficiency, Analysis, and Order
基本概論 Basic concepts.
Introduction 基本概念 授課老師:蕭志明
第一章 資料結構導論 1-1 資料結構簡介 1-2 認識程式設計 1-3 演算法效能分析 1-4 物件導向程式設計與Java.
Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices
TQC+ 物件導向程式認證-JAVA.
Performance Evaluation
The discipline of algorithms
XI. Hilbert Huang Transform (HHT)
Minimum Spanning Trees
3-3 Modeling with Systems of DEs
Operators and Expressions
Euler’s method of construction of the Exponential function
RSA-256bit Digital Circuit Lab TA: Po-Chen Wu.
IV. Implementation IV-A Method 1: Direct Implementation 以 STFT 為例
期末考的範圍遠遠多於期中考,要了解的定理和觀念也非常多
Population proportion and sample proportion
模式识别 Pattern Recognition
计算机问题求解 – 论题 算法的效率 2018年03月14日.
Differential Equations (DE)
二、現代的加解密法:RSA 非對稱式密碼系統的一種。
Chapter 4 歸納(Induction)與遞迴(Recursion)
主讲人: 吕敏 { } Spring 2012 ,USTC 算法基础 主讲人: 吕敏 { } Spring 2012 ,USTC.
微積分網路教學課程 應用統計學系 周 章.
計算方法設計與分析 Design and Analysis of Algorithms 唐傳義
樹狀結構 陳怡芬 2018/11/16 北一女中資訊專題研究.
非線性規劃 Nonlinear Programming
SAT and max-sat Qi-Zhi Cai.
On Some Fuzzy Optimization Problems
張智星 清大資工系 補充內容:方煒 台大生機系 小幅修改:吳俊仲 長庚機械系
Course 4 搜尋 Search.
第 1 章 演算法分析.
Properties of Continuous probability distributions
The Greedy Method.
Sampling Theory and Some Important Sampling Distributions
Course 9 NP Theory序論 An Introduction to the Theory of NP
Chapter 13 數論基礎.
主讲人: 吕敏 { } Spring 2016,USTC 算法基础 主讲人: 吕敏 { } Spring 2016,USTC.
本章大綱 2.1 The Limit of a Function函數的極限 2.2 Limit Laws極限的性質
Randomized Algorithms
主讲人: 吕敏 } Spring 2012 ,USTC 算法基础 主讲人: 吕敏 } Spring 2012 ,USTC.
第四章 插值 /* Interpolation */
Interval Estimation區間估計
子博弈完美Nash均衡 我们知道,一个博弈可以有多于一个的Nash均衡。在某些情况下,我们可以按照“子博弈完美”的要求,把不符合这个要求的均衡去掉。 扩展型博弈G的一部分g叫做一个子博弈,如果g包含某个节点和它所有的后继点,并且一个G的信息集或者和g不相交,或者整个含于g。 一个Nash均衡称为子博弈完美的,如果它在每.
重點 資料結構之選定會影響演算法 選擇對的資料結構讓您上天堂 程式.
4-5 数论基础.
樹 2 Michael Tsai 2013/3/26.
Chapter 5 Recursion.
鄧姚文 資料結構 第一章:基本概念 鄧姚文
每周三交作业,作业成绩占总成绩的15%; 平时不定期的进行小测验,占总成绩的 15%;
3.5 Region Filling Region Filling is a process of “coloring in” a definite image area or region. 2019/4/19.
第九章 數論基礎.
中央社新聞— <LTTC:台灣學生英語聽說提升 讀寫相對下降>
公钥密码学与RSA.
软件测试技术-白盒测试 张志强 2006.
计算机问题求解 – 论题2-1 - 算法的正确性 2018年3月7日.
Course 10 削減與搜尋 Prune and Search
演算法時間複雜度 (The Complexity of Algorithms)
演算法分析 (Analyzing Algorithms)
Chapter 7 Relations (關係)
問題解決與流程圖 高慧君 台北市立南港高中 2006年12月22日.
赵才荣 同济大学,电子与信息工程学院,智信馆410室
怎樣把同一評估 給與在不同班級的學生 How to administer the Same assessment to students from Different classes and groups.
補充 數值方法 數值方法.
二项式的分解因式 Factoring binomials
Gaussian Process Ruohua Shi Meeting
Presentation transcript:

Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices.

2.1 Algorithms Def 1. An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. Example 1. Describe an algorithm for finding the maximum value in a finite sequence of integers.(假設給定的sequence是a1,a2,…,an)

Solution : ( English language) Set the temporary maximum equal to the first integer in the sequence. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. Repeat the previous step if there are more integers in the sequence. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.

Solution : (pseudo-code) Algorithm 1. Finding the Maximum Element procedure max(a1, a2, …, an : integers) max := a1 for i := 2 to n if max < ai then max := ai { max is the largest element}

※ There are several properties that algorithms generally share : Input Output Definiteness : The steps of an algorithm must be defined precisely. Correctness : produce correct output values Finiteness : produce the desired output after a finite number of step. Effectiveness Generality : The procedure should be applicable for all problems of the desired form, not just for a particular set of input values.

※ Searching Algorithms Problem : Locate an element x in a list of distinct elements a1,a2,…,an, or determine that it is not in the list. 做法 : linear search, binary search. Algorithm 2. The linear search algorithm procedure linear_search( x : integer, a1,a2,…,an: distinct integers) i := 1 While ( i ≤ n and x≠ai ) i := i + 1 if i ≤ n then location := i else location := 0 { location = j if x = aj; location = 0 if x≠ai, ∀i }

兩種search方式的概念 : Linear Search : 從 a1 開始,逐一比對 x 是否等於 ai,若找到則 location = i , 若到 an 比完後還找不到,則 location = 0。 Binary Search : (必須具備 a1 < a2 < … < an 的性質才能用) (1) 每次將 list 切成兩半,( ai , … , am ),(am+1 , … , aj ) 若 x > am 表示 x 應在右半,否則在左半。 (2) 重覆上一步驟至 list 只剩一個元素 ai, 若 x = ai 則 location = i,否則 location = 0。

Example 3. Search 19 from a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 12 13 15 16 18 19 20 22 18 19 20 22 18 19 19 1. ( 切兩半 ) ( 因 19 > 10,取右半 ) 2. ( 再切二半 ) ( 因 19 > 16,取右半 ) 3. ( 再切二半 ) ( 因 19 ≦19,取左半 ) 4. ( 再切二半 ) ( 因 19 > 18,取右半 ) 5 此時只剩一個元素 a14 = 19 因 19 = 19,故 location =14 Note : ai, ai+1, …, aj 數列的切法 : 令 m = 則 am 即切開紅線左邊那點。

Algorithm 3. The Binary Search Algorithm procedure binary_search( x : integer, a1,a2,…,an : increasing integers) i :=1 { i is left endpoint of search interval } j := n { j is right endpoint of search interval } while i < j begin m := if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 { location = i if x = ai , location = 0 if x≠ai , ∀i }

※ Sorting Algorithms Problem : Suppose that we have a list of elements, a sorting is putting these elements into a list in which the elements are in increasing order. eg. 7, 2, 1, 4, 5, 9 => 1, 2, 4, 5, 7, 9 d, t, c, a, f => a, c, d, f, t 解法有很多,此處僅介紹 : bubble sort (氣泡排序法),及 insertion sort (插入排序法)。 Bubble Sort 概念 : 設原 list 為 a1,…,an。 從a1,a2開始,向後兩兩比較,若ai > ai+1 則交換,當檢查完 an 時,an 必定是最大數。 再從 a1,a2 開始向後比較,若ai > ai+1 則交換,此時只需檢查到 an-1 即可。 依此類推。

Example 4. Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order. Sol : First pass (i=1) : Second pass (i=2) : 3 2 4 1 5 2 3 4 1 5 2 3 4 1 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 1 3 4 5 2 1 3 4 5 Third pass (i=3) : Fourth pass (i=4) : 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

Algorithm 4 The Bubble Sort procedure bubble_sort (a1,…,an ) for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 { a1,a2,…,an is in increasing order }

Insertion Sort 的概念 : 從 j = 2 開始,將 aj 插入已排序好的 a1,…,aj-1間的位置,使得 a1,…,aj 都由小 → 大排好。 j 逐次遞增,重複上一步驟至做完。

Example 5. Use insertion sort to sort 3, 2, 4, 1, 5 Sol : a1 a2 a3 a4 a5 Example 5. Use insertion sort to sort 3, 2, 4, 1, 5 Sol : (j=2時,a1=3可看成已經排序好的數列,此時要插入a2) : 3 < 2  2, 3 交換  2, 3, 4, 1, 5 (j=3時,a1,a2已經排序好,此時要插入a3) : 4 > 2, 4 > 3  4的位置不變  2, 3, 4, 1, 5 (j=4時,a1,a2 ,a3已經排序好,此時要插入a4) : 1 < 2  將 1 插在最前面  1, 2, 3, 4, 5 (j=5時,a1,a2 ,a3 ,a4已經排序好,此時要插入a5) : 5 > 1, 5 > 2, 5 > 3, 5 > 4  5不變  1, 2, 3, 4, 5

Algorithm 5 The Insertion Sort procedure insertion_sort ( a1,…,an : real numbers with n ≥ 2 ) for j := 2 to n begin i := 1 while aj > ai i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1 ai := m end { a1,a2,…,an are sorted } 找出 aj 應插入的位置 最後ai-1 < aj <= ai 將 ai, ai+1, …, aj-1 全部往右移一格 ( Exercise : 9, 13, 23, 35, 39 )

2.2 The Growth of Functions To analyze the practicality of the program, we need to understand how quickly the function (number of operations used by this algorithm) grows as n (number of input elements) grows. eg. sort n objects Alg. 1 : n2次計算 Alg. 2 : 8n次計算 n 1 2 3 … 8 9 10 # of op. Alg.1 4 64 81 100 Alg.2 16 24 72 80 better!

Def 1. ( Big-O notation ) Let f and g be functions from the set of integers to the set of real numbers. We say that f (x) is O(g(x)) if there are constants C and k such that | f (x) | ≤ C | g(x) | whenever x > k . ( read as “f (x) is big-oh of g(x)” )

Example 1. Show that f (x) = x2+2x+1 is O(x2) Sol : Since x2+2x+1 ≤ x2+2x2+x2 = 4x2 whenever x > 1 , it follows that f (x) is O(x2) (take C = 4 and k =1 ) 另法: If x > 2, we see that x2+2x+1 ≤ x2+x2+x2 = 3x2 ( take C = 3 and k = 2 )

Figure 2. The function f (x) is O(g(x)) Example 1(補充). Show that f (n)= n 2+2n +2 is O(n3) Sol : Since n2+2n+2 ≤ n3+n3+n3 = 3n3 whenever n > 1, we see that f (n) is O(n3) ( take C = 3 and k = 1 ) k Cg(x) f (x) g(x) f (x) < C g(x) for x > k Note. The function g is chosen to be as small as possible.

Theorem 1. Let f (x) = anxn+an-1xn-1+…+a1x+a0 where a0, a1, …, an are real numbers. Then f (x) is O(xn). Example 4. How can big-O notation be used to estimate the sum of the first n positive integers? ( i.e.,  ) Sol : 1 + 2 + 3 + … + n ≤ n + n + … + n = n2 ∴ is O(n2), taking C =1 and k =1.

Example 5. Give big-O estimates for f (n) = n! Sol : n! = 12  3  …  n ≤ n  n  …  n = nn ∴ n! is O(nn) , taking C =1 and k =1. Example 6. (see Figure 3) 常見function的成長速度由小至大排列: 1 < log n < n < n log n < n2 < 2n < n! Theorem 2,3 Suppose that f1(x) is O(g1(x)) and f2(x) is O(g2(x)), then (f1+f2)(x) is O(max(|g1(x)|, |g2(x)|)), (f1 f2)(x) is O(g1(x) g2(x)).

Exercise 7,11,19 Exercise 19(c) : f (n) = (n!+2n)(n3+log(n2+1))  (n!+n!)(n3+n3) = 4n3n! ∴ f (n) is O(n3n!) 取 C = 4, k = 3

2.3 Complexity of Algorithms Q : How can the efficiency of an algorithm be analyzed ? Ans : (1) time (2) memory Def : Time complexity : an analysis of the time required to solve a problem of a particular size. (評量方式 : 計算 # of operations,如 “comparison”次數, “加法” 或 “乘法” 次數等) Space complexity : an analysis of the computer memory required to solve a problem of a particular size. (通常是資料結構探討的範圍)

Example 1. Describe the time complexity of Algorithm 1. Sol : (計算 # of comparisons) i 值一開始 = 2 逐次加一,並比較是否>n. 當 i 變成 n+1 時 因比 n 大,故結束 for 迴圈。 ∴ 共有 n 次 comparison 共有 n-1 次 comparison 故整個演算法共做 2n-1 次 comparison 其 time complexity 為 O(n). Algorithm 1. ( Find Max ) procedure max(a1,…,an : integers) max := a1 for i := 2 to n if max < ai then max := ai { max is the largest element }

Example 2. Describe the time complexity of the linear search algorithm. Algorithm 2 ( Linear Search ) procedure ls ( x : integer , a1,…,an : distinct integers ) i := 1 While ( i  n and x ≠ai ) i := i +1 if i  n then location := i else location := 0 location = i  x = ai = 0  x  ai i Sol : ( 計算 # of comparisons ) (Case 1) 當 x = ai for some i  n 時 此行只執行 i 次,故此行共2i次比較 加上if,共計 2i +1次 comparisons. (Case 2) 當 x ≠ ai for all i 時 此行執行 n 次後 第 n + 1 次時 i = n + 1 > n 即跳出 ∴共計 2n+2 次 comparisons 由(1)、(2)取 worst-case 演算法的 time complexity為 O(n)

∴average-case的time complexity為O(n) Example 4. Describe the average-case performance of the linear search algorithm, assuming that x is in the list. Sol : ( 計算 “平均比較次數” ) 已知當 x = ai 時,共需 2i + 1 次比較. ( by Example 2 ) x = a1,a2, …, 或 an 的機率都是 1/n. ∴平均比較次數 (即期望值) = ( x = a1 的比較次數 ) × ( x = a1 的機率 ) + ( x = a2 的比較次數 ) × ( x = a2 的機率 ) + … + ( x = an 的比較次數 ) × ( x = an 的機率 ) = 3 × 1/n + 5 × 1/n + … + ( 2n+1) × 1/n = ( 3+5+…+(2n+1)) / n = / n = n + 2 Alg. 2 ( Linear Search ) procedure ls ( x,a1,…,an) i := 1 While ( i  n and x ≠ai ) i := i +1 if i  n then location := i else location := 0 ∴average-case的time complexity為O(n)

Example 3. Describe the time complexity of the binary search algorithm. Alg. 3 ( Binary Search ) procedure bs ( x : integer, a1,…,an : increasing integers ) i := 1 { left endpoint } j := n { right endpoint } while i < j /* ( k+1 次) begin m :=  ( i + j ) / 2  if x > am then i := m+1 /* ( k次 ) else j := m end if x = ai then location := i /* ( 1次 ) else location := 0 Sol : 設 n = 2k 以簡化計算 (若 n < 2k,其比較次數必小於等 於 n = 2k 的情況) 因while迴圈每次執行後 整個 list 會切成兩半 故最多只能切 k 次 就會因 i = j 而跳出迴圈 ∴共比較 2k+2 次 time complexity 為 O(k) = O(log n)

Example 5. What is the worst-case complexity of the bubble sort in terms of the number of comparisons made ? Sol : 共 n-1 個 pass 第 i 個 pass 需 n – i 次比較 ∴共計 (n-1)+(n-2)+…+1 = 次比較 ∴ O(n2) procedure bubble_sort ( a1,…,an ) for i := 1 to n -1 for j := 1 to n – i if aj > aj+1 then interchange aj and ai+1 { a1,…,an is in increasing order } Note 1. 不管何種 case 都需做 次比較。 Note 2. For 迴圈所需比較次數通常會省略,因此Example 5,6 不再考慮。

Example 6. What is the worst-case complexity of the insertion sort in terms of the number of comparisons made ? procedure insertion_sort ( a1,…,an ) for j := 2 to n begin i := 1 while aj > ai i := i +1 m := aj for k := 0 to j - i -1 aj-k := aj-k-1 ai := m end { a1,…,an are sorted } Sol : 做最多次比較的情況如下: 在考慮 aj 時 a1 < a2 < … < aj-1 < aj 此時共做 j 次比較 故共計 2+3+…+n = -1 次比較  O(n2) (即 worst case 是 a1 < a2 < … < an)

Table 1. Commonly Used Terminology Complexity Terminology O(1) constant complexity O(log n) Logarithmic complexity O(n) Linear complexity O(n log n) n log n complexity O(nb) Polynomial complexity O(bn) , b >1 Exponential complexity O(n!) Factorial complexity Exercise : 7,8,13

2.4 The integers and division ※探討一些 Number Theory 的基本觀念 Def 1. a,b : integers, a≠0. a divides b (denote a | b) if cZ , b=ac . (a : a factor of b, b : a multiple of a) (a b if a does not divide b) Corollary 1. If a,b,c Z and a | b , a | c. then a | mb+nc whenever m,nZ Def 2. pZ+ \ {1} is called prime (質數) if a p,1<a< p, aZ+. p is called composite (合成數) otherwise. Thm 2. (The fundamental theorem of arithmetic) 所有大於1的正整數,都是質數或可分解為質數 的乘積 (分解方式唯一)。

Thm 3. If n is a composite integer, then n has a prime divisor less than or equal to . Thm 4. There are infinitely many primes. Pf. 假設質數只有n個:p1, p2, …, pn,Let Q = p1p2…pn+1. 證明 p1, …, pn 都不能整除Q. ※目前為止所知最大的質數是2p -1的形式, where p is prime. 稱為 Mersenne primes (梅森質數). Example 7. 22-1=3, 23-1=7, 25-1=31 are primes, but 211-1=2047=2389 is not a prime. Def 3. If a=dq+r, where a,q,rZ, dZ+, 0 r<d then r = a mod d. Def 4. gcd ( greatest common divisor ) Def 7. lcm ( least common multiple ) Def 5. relatively prime (互質)

a is congruent to b modulo m if m|(a-b). (denote a≡b (mod m)). Def 8. If a,bZ, mZ+, then a is congruent to b modulo m if m|(a-b). (denote a≡b (mod m)). Thm 9. Let mZ+, a,bZ. a≡b (mod m) iff kZ, a=b+km. Exercise 14. How many zeros are there at the end of 100! ? Sol : 計算12  3  …  100=10k m, where 10 m ∵ 10=2  5,又2的次數必定比5多 ∴ 計算1  2  3  …  100=5k  n, where 5 n ∵ 5,10,15,20,…,100才有因數5, 而25,50,75,100有因數25 ∴ k=24  共有24個0 Homework : 試寫一alg. 求出  n 的所有質數

2.5 Integers and Algorithms ※The Euclidean Algorithm (輾轉相除法求 gcd ) Example : Find gcd(91,287) Sol: 287 = 91 3 + 14 91 = 14  6 + 7 14 = 7  2 ∴ gcd(91,287) = 7 Example 12. Find gcd(416,662) (做做看!) Ans : 2 if x|91 & x|287  x|14 ∴gcd (91,287) = gcd(91,14) gcd (91,14) = gcd (14,7) gcd (14,7) = 7

eg. 求 gcd (6,12) x = 6 y = 12 while y≠0 r = 6 mod 12 =6 x = 12 y = 6 while y≠0 r = 12 mod 6 = 0 y = 0 while y = 0 , end. ∴ gcd (6,12) = 6 Algorithm 6. ( The Euclidean Algorithm) procedure gcd ( a, b : positive integers) x := a y := b while y≠0 begin r := x mod y ( if y > x then r = x) x := y y := r end { gcd (a, b) = x } Exercise : 21,23

2.6 Applications of Number Theory ※介紹中國餘數定理 Example 5. 孫子算經 :「某物不知其數,三三數之餘二,五五數之餘三,七七數之餘二,問物幾何 ?」 i.e. x ≡ 2 (mod 3) x ≡ 3 (mod 5) x = ? x ≡ 2 (mod 7) Theorem 4. (The Chinese Remainder Theorem) Let m1,m2,…,mn be pairwise relatively prime positive integers. The system x ≡ a1 (mod m1) x ≡ a2 (mod m2) : x ≡ an (mod mn) has a unique solution modulo m = m1m2…mn. (即有一解 x, where 0 x < m , 且所有其他解 mod m都等於 x)

Proof of Thm 4: Let Mk = m / mk  1 k  n ∵ m1, m2,…, mn are pairwise relatively prime ∴ gcd (Mk , mk) = 1  integer yk s.t. Mk yk≡1 (mod mk) ( by Thm. 3, 此處不証)  ak Mk yk≡ak (mod mk) ,  1  k  n Let x = a1M1y1+a2M2y2+…+anMnyn ∵ mi | Mj , i≠j ∴ x≡ak Mk yk≡ak (mod mk)  1 k  n x 即為一解

Example 6. (承 Example 5 題目敘述,求解) Let m = 357 = 105 M1 = m / 3 = 35 ∵ 35≡2 (mod 3)  35  2 ≡ 1 (mod 3) 21≡1 (mod 5)  21  1 ≡ 1 (mod 5) 15≡1 (mod 7)  15  1 ≡ 1 (mod 7) ∴ x = a1M1y1 + a2M2y2 + a3M3y3 = 2  35  2 + 3  21  1 + 2  15  1 = 233 ≡ 23 (mod 105) ∴ 最小的解為23,其餘解都等於 23+105t for some tZ+ Exercise : 19 M1 y1 M2 y2 M3 y3

Exercise 18. Find all solutions to the system of congruences x≡2 (mod 3) a1=2 , a2=1 , a3=3, m1=3 , m2=4 , m3=5 m=345=60 M1=20 , M2=15 , M3=12 20≡2 (mod 3)  202≡1 (mod 3) 15≡3 (mod 4)  153≡1 (mod 4) 12≡2 (mod 5)  123≡1 (mod 5) ∴ x = 2202+1153+3123 = 80+45+108=233≡53 (mod 60)

2.7 Matrices Algorithm 1. Matrix multiplication procedure matrix_multiplication(A : mk matrix, B : kn matrix ) for i := 1 to m for j :=1 to n begin cij := 0 for q := 1 to k cij := cij + aiqbqj end { c =[cij] = AB } Exercise : 23 , 25