590 likes | 792 Views
Discrete Mathematics. Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲. 3.1 Algorithms ( 演算法 ). Def 1. An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem.
E N D
Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲
3.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.(假設給定的整數序列是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 { maxis the largest element} 演算法名稱 輸入變數名稱 procedure表示開始一個副程式 :=用來表示「指定」 max:=a1表示「指定max變數的值等於a1」 大括號{}用來存放註解,此處標示「輸出變數」是max 輸入變數型別
※ 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.
Exercise 3.1 參考找最大值的演算法做修改: procedure max(a1, a2, …, an : integers) max:=a1 for i:=2 to n if max<ai then max:=ai { maxis the largest element} procedure sum (a1, a2, …, an: integers) sum := for i:= to { sumis the largest element} 3. 設計一個演算法,找出某個整數集合中所有數目的總和。
※ 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 = jif x = aj; location = 0 if x is not found.}
Exercise 3.1 參考: 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 = jif x = aj; location = 0 if x is not found.} 13(a). 列出「線性搜尋」用來從序列 1, 3, 4, 5, 6, 8, 9, 11中尋找 9 的步驟。
兩種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 a1a2a3a4a5a6a7a8a9a10a11a12a13a14a15a16 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 Note : ai, ai+1, …, aj數列的切法 : 令 m= 則 am即切開紅線左邊那點。 正在搜尋 x 的數列為 ai,ai+1, …, aj x 一開始 i=1,j=16 1. 切兩半, m=8 因 x=19 > a8=10,取右半,i=9 2. 再切二半, m=12 因 x=19 > a12=16,取右半, i=13 3. 再切二半, m=14 因 x=19 a14=19,取左半, j=14 4. 再切二半, m=13 因 x=19 > a13=18,取右半, i=14 5 此時 i = j =14, 數列只剩一個元素 a14 = 19 因 x= 19 = a14,故 location =14
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 }
Exercise 3.1 參考: 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 } 13(b). 列出「二元搜尋」用來從序列 1, 3, 4, 5, 6, 8, 9, 11中尋找 9 的步驟。
參考: 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 } 13(補充). 列出「二元搜尋」用來從序列 2, 4, 6, 8, 10, 12, 14 中尋找 3 的步驟。
(跳過) ※ 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 即可。 • 依此類推。
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 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 (跳過) • 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 Third pass (i=3) : Fourth pass (i=4) : 1 2 3 4 5
(跳過) Algorithm 4 The Bubble Sortprocedure bubble_sort (a1,…,an ) fori := 1 ton-1 forj := 1 ton-i ifaj > aj+1then interchange aj and aj+1 { a1,a2,…,an is in increasing order }
(跳過) • Insertion Sort 的概念 : • 從 j= 2開始,將 aj插入已排序好的 a1,…,aj-1間的位置,使得 a1,…,aj都由小 → 大排好。 • j 逐次遞增,重複上一步驟至做完。
(跳過) a1a2a3a4a5 • 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 Sortprocedure insertion_sort ( a1,…,an : real numbers with n≥ 2 ) forj := 2 ton begin i := 1 whileaj > ai i := i + 1 m := aj fork := 0 toj – 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 : 13, 23, 35, 39 )
3.2 The Growth of Functions (函數的成長) • 為了分析演算法的實用性,我們需要瞭解函數 (用來代表一個演算法中的運算次數)隨著n (input變數的個數) 的成長,會增加多快. • 例. 排序n物件 假設Algorithm 1 需要n2次計算 Algorithm 2 需要8n次計算,哪一個演算法比較好? 1 … 4 9 64 81 100 80 24 8 16 … 64 72 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)” ) • 我們說函數 f (x)是O(g(x)) 代表在 x夠大時,| f (x)| ≤ |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 )
Cg(x) f (x) g(x) f (x) < Cg(x) for x > k k Figure 2. The function f (x) is O(g(x)) Example 1(補充).Show thatf (x)= x2 +2x +2isO(x3) Sol :Since x2+2x+2 ≤ x3+x3+x3 = 3x3 whenever x> 1, we see that f (x) is O(x3) ( take C = 3 and k = 1 ) Note. The function g is chosen to be as small as possible.大O符號裡放的函數 要越小越好
Example 5. 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. (跳過) 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 6. Give big-O estimates for f (n) = n! Sol : n! = 12 3 … n≤ nn… n = nn ∴ n! is O(nn) , taking C =1 and k =1. Example 7. (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) = 4n3n! ∴ f (n) is O(n3n!)取 C = 4, k = 3
(複雜度) 3.3 Complexity of Algorithms Q :如何分析演算法的執行效能? Ans : (1) time (2) memory Def : • Time complexity: an analysis of the time required to solve a problem of a particular size. (評量方式 : 計算「運算次數」,如 “做比較”的次數,“加法” 或 “乘法”次數等) • Space complexity:分析解問題時所需的電腦記憶體容量 (通常是資料結構探討的範圍)
Sol : (計算 「做比較」的次數) i值一開始 = 2 逐次加一,並比較是否>n. 當 i變成 n+1時 因比 n大,故結束for迴圈。 ∴ 共有 n次比較 共有 n-1次比較 故整個演算法共做 2n-1次比較 其時間複雜度為 O(n). Example 1.描述 Algorithm 1 (Find Max) 的時間複雜度. Algorithm 1. ( Find Max ) procedure max(a1,…,an: integers) max:= a1 for i:= 2 ton if max < ai then max := ai { max is the largest element }
Algorithm 2 ( Linear Search ) procedure ls ( x: integer , a1,…,an : distinct integers ) i := 1 While ( in and x ≠ai ) i := i +1 if i n then location := i else location := 0 { location = iifx = ai; location = 0 if x ai i} Example 2. Describe the time complexity of the linear search algorithm. Sol : ( 計算「比較」次數) (Case 1) 當 x = 某個ai時 此行只執行 i次,故此行共2i次比較 加上if,共計 2i +1次比較. (Case 2) 當 x ≠ 任何ai時 此行執行 n 次後 第 n + 1次時i = n + 1 > n即跳出 ∴共計 2n+2次比較 由(1)、(2)取 worst-case (最糟的情形)故演算法的 time complexity為 O(n)
Exercise 3.3 procedure polynomial (c, a0, a1,…,an : real numbers) power := 1 {變數power用來表示乘冪}y := a0 {變數y用來表示目前已算出的多項式值} fori := 1 ton begin power := power * cy := y + ai * power end{ y = anc n+an-1c n-1 +… + a1c +a0} 執行上述演算法的每一步驟,計算 3x2 +x +1 在 x =2 時的值 為求出n階多項式在x = c時的值,所需乘法與加法次數為何? 7. 對多項式 an xn+an-1xn-1 +… + a1x +a0,求出 x = c時的值,傳統演算法做法如下:
Exercise 3.3 procedure Horner (c, a0, a1,…,an : real numbers) y := an fori := 1 ton y := y * c + an-i { y = anc n+an-1c n-1 +… + a1c +a0} 執行上述演算法的每一步驟,計算 3x2 +x +1 在 x =2 時的值 為求出n階多項式在x = c時的值,所需乘法與加法次數為何? 8. 對多項式 an xn+an-1xn-1 +… + a1x +a0,求出 x = c時的值,更有效率的Horner演算法做法如下:
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 ( in and x ≠ai ) i := i +1 if i n then location := i else location := 0 Exercise : 13 ∴average-case的time complexity為O(n)
Alg. 3 ( Binary Search ) procedurebs ( x : integer, a1,…,an : increasing integers ) i := 1 { left endpoint } j := n { right endpoint } whilei < j/* ( k+1 次) begin m := ( i + j ) / 2 ifx > am theni := m+1 /* ( k次) elsej := m end ifx = aithenlocation := i /* ( 1次) elselocation := 0 Example 3. Describe the time complexity of the binary search algorithm. 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)
3.4 The integers and division (除法) ※探討一些 Number Theory 的基本觀念 Def 1.a,b : integers, a≠0. a divides b (denote a | b) if cZ, b=ac. a : a factor (因數) of b, b : a multiple (倍數) of a (ab if a does not divide b) Theorem 1. a,b,cZ, (i) If a | b and a | c then a | (b+c). (ii) If a | b then a | bc for any integer c. (iii) If a | b and b | c then a | c. Corollary 1. If a,b,cZ and a | b , a | c. then a | (mb+nc) whenever m,nZ.
Def 2.In the equality a = dq + rwith0r <d,d is called the divisor (除數), a is called the dividend (被除數), q is called the quotient (商數), and ris called the remainder (餘數). q=a div d, r = a mod d Example 3. 101 div 11= ? (商數) 101 mod 11 =? (餘數) Sol: 101 = 11 9 + 2 故 101 div 11 = 9, 101 mod 11 = 2 Example 4.-11 div 3= ? (商數)-11 mod 3 =? (餘數) Sol: -11 = 3 (-4) + 1 故 -11 div 3 = -4, -11 mod 3 = 1
Exercise 3.4 10. (b) 777 div 21 = 777 mod 21 = (c) -123 div 19 =-123 mod 19
※ 模算術 Def 3.If a,bZ, mZ+, then a is congruent (同餘) to b modulo m if m | (a-b). (表示為a b(mod m), 即整數a、b對模m同餘). Thm 3. Let mZ+, a,bZ. a ≡ b (mod m) iff a mod m =b mod m. Example 5.判斷在模6時,17是否同餘於5? 24是否同餘於14? Sol: 17mod 6= 5, 5mod 6=5, 故17同餘於5 24mod 6= 0, 14mod 6=2, 故24不同餘於14
Thm 4. Let mZ+, a,bZ. a ≡ b (mod m) iff kZ, 使得a=b + km. Thm 5. Let mZ+, a,bZ. If a ≡ b (mod m) andc ≡ d (mod m), then a+c ≡ b+d (mod m) andac ≡ bd (mod m). Example 6.7≡ 2 (mod 5), 11≡ 1 (mod 5), 故18≡ 3 (mod 5), 77≡ 2 (mod 5).
Exercise 3.4 補充:請計算 (1717 9 + 31) mod5 =? 19. 判斷下列何者在模數 17 時,與 5 同餘。(a) 80 (b) 103 (c) -29 (d) -122
※同餘的應用-密碼學 Example 9.根據凱撒大帝的密碼製作方式,下面的訊息會被加密成什麼? “MEET YOU IN THE PARK” Sol.“PHHW BRX LQ WKH SDUN” 凱撒大帝的加密法:將訊息中每個字母向後移動三位,最後三個字母則以最前面三個字母取代,如 B 換成 E,X 換成 A。 以數學方式表示:將A~Z以0~25編碼,將編號 p 的字母以 f(p) 取代,其中 f(p)=(p+3) mod26。
Example 10.若使用 f(p)=(7p + 3) mod26來加密,則將會用哪一個字母來取代 K? Sol.K 的編碼是 10, (710+3) mod 26 = 21 故用V取代K 改進加密法:將A~Z以0~25編碼,將編號 p 的字母以 f(p) 取代,其中 f(p)=(ap + b) mod26,所選取的整數 a, b 只要能使 f(p) 成為對射函數即可。
Exercise 3.4 31. 利用給定的函數 f(p)=(7p + 3) mod26,將訊息 “PASS THROUGH” 加密,寫出加密的訊息。 32. 將下列經凱撒密碼加密之訊息解密: “EOXH MHDQV”
3.5 Primes and Greatest Common Divisors Def 1.pZ+-{1} is called prime(質數)if ap, 1<a< p, aZ+. p is called composite (合成數) otherwise. Thm 1. (算數基本定理) 每個大於1的正整數,都能唯一表示為 一個質數 或是 兩個以上質數的乘積(數字由小排到大)。 Example 2. (prime factorization) 100 的質因數分解是 2255= 2252. 641 的質因數分解是 641. 999 的質因數分解是 33337= 3337. 1024 的質因數分解是 210.
Thm 3. 質數有無限多個。Pf. 假設質數只有n個:p1, p2, …, 及 pn, 令Q = p1p2…pn+1, 因 p1, …, pn都不整除Q,得證。 ※目前為止所知最大的質數是2p-1的形式, where p is prime. 稱為 Mersenne primes (梅森質數). Example 5.22-1=3, 23-1=7, 25-1=31 are primes, but 211-1=2047=2389 is not a prime. Def 2.gcd (greatest common divisor, 最大公因數) Thm 2.若n為合成數,則n有一個質因數 . Example 10. gcd(24, 36) = ? Example 11. gcd(17, 22) = ?
Def 3.relatively prime (互質) Def 5.lcm (least common multiple, 最小公倍數)
6.數字100!後面有幾個0? Sol :計算12 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 Exercise 3.5 5. 寫出10!的質因數分解。
3.6 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 (原因,若x為最大公因數) x|91 & x|287 x|14 ∴gcd (91,287) = gcd(91,14) gcd (91,14) = gcd (14,7) gcd (14,7) = 7 Lemma 1 Let a = bq + r, where a, b, q, and rZ.Then gcd(a, b) = gcd (b, r).