Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Slides:



Advertisements
Similar presentations
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
Advertisements

布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
Instructor: Ching-Chi Lin 林清池 助理教授
1.1 線性方程式系統簡介 1.2 高斯消去法與高斯-喬登消去法 1.3 線性方程式系統的應用(-Skip-)
亂數產生器安全性評估 之統計測試 SEC HW7 姓名:翁玉芬 學號:
Lecture 8 Median and Order Statistics. Median and Order Statistics2 Order Statistics 問題敘述 在 n 個元素中,找出其中第 i 小的元素。 i = 1 ,即為找最小值。 i = n ,即為找最大值。 i = 或 ,即為找中位數。
Review of Chapter 3 - 已學過的 rules( 回顧 )- 朝陽科技大學 資訊管理系 李麗華 教授.
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
Chapter 2 聯立線性方程式與矩陣 緒言 線性方程式組 (systems of linear equations) 出現 在多數線性模式 (linear model) 中。根據以往解 題的經驗,讀者們也許已發現方程式的解僅與 該方程式的係數有關,求解的過程也僅與係數 的運算有關,只要係數間的相關位置不改變,
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
: The Playboy Chimp ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10611: The Playboy Chimp 解題者:蔡昇宇 解題日期: 2010 年 2 月 28 日 題意:給一已排序的數列 S( 升冪.
Analyzing Algorithms Based on: 1. 物件導向資料結構 — 使用 Java 語言, 江振 瑞 著, 松崗圖書公司, Introduction to the Design and Analysis of Algorithms -- A strategic.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
第一章 演算法:效率、分析與量級 1.1演算法 1.2發展有效率演算法的重要性 1.3演算法的分析 1.4量級(Order)
2009fallStat_samplec.i.1 Chap10 Sampling distribution (review) 樣本必須是隨機樣本 (random sample) ,才能代表母體 Sample mean 是一隨機變數,隨著每一次抽出來的 樣本值不同,它的值也不同,但會有規律性 為了要知道估計的精確性,必需要知道樣本平均數.
:Problem D: Bit-wise Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10232: Problem D: Bit-wise Sequence 解題者:李濟宇 解題日期: 2006 年 4 月 16.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
計算機概論 - 排序 1 排序 (Sorting) 李明山 編撰 ※手動換頁.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
3.1 矩陣的行列式 3.2 使用基本運算求行列式 3.3 行列式的性質 3.4 特徵值介紹 3.5 行列式的應用
Fourier Series. Jean Baptiste Joseph Fourier (French)(1763~1830)
Chapter 2 Getting Started Insertion Sort: 能有效率地排序小數字的演算法 範例 :
: Playing War ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11061: Playing War 解題者:陳盈村 解題日期: 2008 年 3 月 14 日 題意:在此遊戲中,有一類玩家一旦開始攻擊, 就會不停攻擊同一對手,直到全滅對方或無法再.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
Fugacity Coefficient and Fugacity
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
資料結構實習-一 參數傳遞.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
觀測量的權 權的觀念與計算.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
-Antidifferentiation- Chapter 6 朝陽科技大學 資訊管理系 李麗華 教授.
: Flip Sort ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10327: Flip Sort 解題者:歐子揚 解題日期: 2010 年 2 月 26 日 題意:在這個問題中使用一種排序方式 (Flip) ,意思就是 只能交換相鄰的.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 2. Recurrence Relations (遞迴關係)
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 7 貪婪法則 Greedy Approach.
: Help My Brother ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11033: Help My Brother 解題者: 呂明璁 解題日期: 2007 年 5 月 14 日.
2005/7 Linear system-1 The Linear Equation System and Eliminations.
連續隨機變數 連續變數:時間、分數、重量、……
: Searching for Nessy ★☆☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 11044: Searching for Nessy 解題者:王嘉偉 解題日期: 2007 年 5 月 22 日 題意: 給定 case 數量.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
:Count the Trees ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10007:Count the Trees 解題者:楊家豪 解題日期: 2006 年 3 月 題意: 給 n 個點, 每一個點有自己的 Label,
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
1 Knapsack Cryptosystems 2 ◎ Merkle-Hellman Knapsack Cryptosystem 觀察: (1) 0/1 knapsack problem (i.e. sum of subset) 例:已知 C = 14, A = (1, 10, 5, 22, 3)
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Discrete Mathematics Chapter 3 Mathematical Reasoning, Induction, and Recursion 感謝 大葉大學 資訊工程系 黃鈴玲老師 提供.
Discrete Mathematics Chapter 7 Advanced Counting Techniques 大葉大學 資訊工程系 黃鈴玲.
Discrete Mathematics Chapter 2 Basic Structures : Sets, Functions, Sequences, and Sums 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Discrete Mathematics Section 3.7 Applications of Number Theory 大葉大學 資訊工程系 黃鈴玲.
Discrete Mathematics Chapter 6 Advanced Counting Techniques.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Presentation transcript:

Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch 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 是 a 1, a 2,…, a n )

Ch3-3 Solution : ( English language) 1.Set the temporary maximum equal to the first integer in the sequence. 2.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. 3.Repeat the previous step if there are more integers in the sequence. 4.Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.

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

Ch3-5 ※ 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.

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

Ch3-7 兩種 search 方式的概念 : Linear Search : 從 a 1 開始,逐一比對 x 是否等於 a i ,若找到 則 location = i, 若到 a n 比完後還找不到,則 location = 0 。 Binary Search : ( 必須具備 a 1 a m 表示 x 應在右半,否則在左半。 (2) 重覆上一步驟至 list 只剩一個元素 a i , 若 x = a i 則 location = i ,否則 location = 0 。

Ch3-8 Example 3. Search 19 from a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a 11 a 12 a 13 a 14 a 15 a ( 切兩半 ) ( 因 19 > 10 ,取右半 ) 2. ( 再切二半 ) ( 因 19 > 16 ,取右半 ) 3. ( 再切二半 ) ( 因 19 ≦ 19 ,取左半 ) 4. ( 再切二半 ) ( 因 19 > 18 ,取右半 ) 5 此時只剩一個元素 a 14 = 19 因 19 = 19 ,故 location =14 Note : a i, a i +1, …, a j 數列的切法 : 令 m = 則 a m 即切開紅線左邊那點。

Ch3-9 Algorithm 3. The Binary Search Algorithm procedure binary_search( x : integer, a 1,a 2,…,a n : 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 > a m then i := m+1 else j := m end if x = a i then location := i else location := 0 { location = i if x = a i, location = 0 if x≠a i, ∀ i }

Ch3-10 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 為 a 1,…,a n 。  從 a 1,a 2 開始,向後兩兩比較,若 a i > a i+1 則交換,當檢查 完 a n 時, a n 必定是最大數。  再從 a 1,a 2 開始向後比較,若 a i > a i+1 則交換,此時只需檢 查到 a n- 1 即可。  依此類推。 ※ Sorting Algorithms

Ch3-11 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 ) : Third pass ( i=3 ) :Fourth pass ( i=4 ) :

Ch3-12 Algorithm 4 The Bubble Sort procedure bubble_sort (a 1,…,a n ) for i := 1 to n  1 for j := 1 to n  i if a j > a j+1 then interchange a j and a j+1 { a 1,a 2,…,a n is in increasing order }

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

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

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

Ch 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 : n 2 次計算  Alg. 2 : 8n 次計算 n 123…8910 # of op. Alg.1 149… Alg … better!

Ch3-17 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 )” )

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

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

Ch3-20 Example 5. How can big- O notation be used to estimate the sum of the first n positive integers? ( i.e.,  ) Sol : … + n ≤ n + n + … + n = n 2 ∴ is O(n 2 ), taking C =1 and k =1. Theorem 1. Let f (x) = a n x n +a n  1 x n  1 +…+a 1 x+a 0 where a 0, a 1, …, a n are real numbers. Then f (x) is O(x n ).

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

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

Ch 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. ( 通常是資 料結構探討的範圍 )

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

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

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

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

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

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

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

Ch 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. In the quality a = dq + r with 0  r < d, d is called the divisor ( 除數 ), a is called the dividend ( 被除數 ), q is called the quotient ( 商數 ), and r is called the remainder ( 餘數 ).  q = a div d, r = a mod d

Ch3-32 Def 3. If a, b  Z, m  Z +, then a is congruent ( 同餘 ) to b modulo m if m | (a  b). (denote a ≡ b (mod m) ). Thm 4. Let m  Z +, a,b  Z. a≡b (mod m) iff  k  Z, s.t. a=b+km. Thm 5. Let m  Z +, a,b  Z. If a≡b (mod m) and c≡d (mod m), then a+c≡b+d (mod m) and ac≡bd (mod m).

Ch Primes and Greatest Common Divisors Def 1. p  Z +  {1} is called prime ( 質數 ) if a p,  1<a< p, a  Z +. p is called composite ( 合成數 ) otherwise. Thm 1. (The fundamental theorem of arithmetic) Every positive integer greater than 1 can be written uniquely as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing size. Example 2. The prime factorization ( 因數分解 ) of 100 is

Ch3-34 Thm 3. There are infinitely many primes. Pf. 假設質數只有 n 個: p 1, p 2, …, 及 p n , 令 Q = p 1 p 2 …p n +1, 因 p 1, …, p n 都不整除 Q ,得證。 ※目前為止所知最大的質數是 2 p  1 的形式, where p is prime. 稱為 Mersenne primes ( 梅森質數 ). 梅森質數 Example  1=3, 2 3  1=7, 2 5  1=31 are primes, but 2 11  1=2047=23  89 is not a prime. Def 2. gcd ( greatest common divisor ) Def 3. relatively prime ( 互質 ) Def 5. lcm ( least common multiple ) Thm 2. If n is a composite integer, then n has a prime divisor less than or equal to.

Ch3-35 Exercise 14. How many zeros are there at the end of 100! ? Sol : 計算 1  2  3  …  100=10 k  m, where 10 m ∵ 10=2  5 ,又 2 的次數必定比 5 多 ∴ 計算 1  2  3  …  100=5 k  n, where 5 n ∵ 5,10,15,20,…,100 才有因數 5, 而 25,50,75,100 有因數 25 ∴ k=24  共有 24 個 0 Homework : 試寫一 alg. 求出  n 的所有質數

Ch Integers and Algorithms ※ The Euclidean Algorithm ( 輾轉相除法求 gcd ) Example : Find gcd(91,287) Sol: 287 = 91  = 14  = 7  2 ∴ gcd(91,287) = 7 Lemma 1 Let a = bq + r, where a, b, q, and r  Z. Then gcd(a, b) = gcd (b, r). if x |91 & x |287  x |14 ∴ gcd (91,287) = gcd(91,14) gcd (91,14) = gcd (14,7) gcd (14,7) = 7

Ch3-37 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 } 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 x = 6 y = 0 while y = 0, end. ∴ gcd (6,12) = 6 Exercise : 23,25

Ch Applications of Number Theory Theorem 1. If a and b are positive integers, then there exist integers s and t such that gcd(a,b) = sa+tb. gcd(a,b) can be expressed as a linear combination with integer coefficients of a and b. ※將 gcd(a,b) 寫成 a 跟 b 的線性組合: The extended Euclidean Algorithm

Ch3-39 Example 1 Express gcd(252, 198) =18 as a linear combination of 252 and 198. Sol: 252 = 1  = 3  = 1  = 2  18 ∴ gcd(252, 198) = = 54 – 1  =198 – 3  =252 – 1  = 54 – 1  36  = 54 – 1  (198 – 3  54 ) = 4  54 – 1  198= 4  (252 – 1  198) – 1  198 = 4  252 – 5  198 Exercise : 1(g)

Ch3-40 Lemma 1. If a, b and c are positive integers such that gcd(a,b) = 1 and a | bc, then a | c. Lemma 2. If p is a prime and p | a 1 a 2 …a n, where each a i is an integer, then p | a i for some i. Example 2 14  8 (mod 6), 但  的左右兩邊同除以 2 後不成立 because 14/2=7, 8/2=4, but 7  4(mod 6). Q: 何時可以讓  的左右同除以一數後還成立呢? 另, 14  8 (mod 3), 同除以 2 後, 7  4 (mod 3) 成立

Ch3-41 Theorem 2. Let m be a positive integer and let a, b, and c be integers. If ac  bc (mod m) and gcd(c, m) = 1, then a  b (mod m). ※ Linear Congruences A congruence ( 同餘式 ) of the form ax  b (mod m), where m is a positive integer, a and b are integers, and x is a variable, is called a linear congruence. How can we solve the linear congruence ax  b (mod m) ? Def: If ax  1 (mod m), and let a be an answer of x, a is called an inverse ( 反元素 ) of a modulo m

Ch3-42 Theorem 3. If a and m are relatively prime integers and m>1, then an inverse of a modulo m exists. Furthermore, this inverse is unique modulo m. Proof. (existence) (unique 的部分是 exercise) By Thm 1, because gcd(a, m) = 1, there exist integers s and t such that sa + tm =1.  sa + tm  1 (mod m). Because tm  0 (mod m), sa  1 (mod m),  s is an inverse of a modulo m.

Ch3-43 Example 3 Find an inverse of 3 modulo 7. Sol. Because gcd(3, 7) = 1, find s, t such that 3s + 7t =1.   2 is an inverse of 3 modulo 7.  7 = 2   1 =  2   7 (Note that every integer congruent to  2 modulo 7 is also an inverse of 3, such as 5,  9, 12, and so on. ) Exercise : 5

Ch3-44 Example 4 What are the solutions of the linear congruence 3x  4 (mod 7) ? Sol. By Example 3   2 is an inverse of 3 modulo 7  If x is a solution, then x   8  6 (mod 7).   2  3x   2  4 (mod 7) Because  6  1 (mod 7), and  8  6 (mod 7), We need to determine whether every x with x  6 (mod 7) is a solution. Assume x  6 (mod 7), then 3x   6 = 18  4 (mod 7). Therefore every such x is a solution: x = 6, 13, 20, …, and  1,  8,  15, …. Exercise : 11 將 3x  4 (mod 7) 左右同乘  2   6x   (mod 7)  3  (  2)  1 (mod 7)

Ch3-45 Example 5. 孫子算經 : 「某物不知其數,三三數之餘二,五五數之 餘三,七七數之餘二,問物幾何 ? 」 ( 又稱為「韓信點兵」問題 ) i.e. x ≡ 2 (mod 3) x ≡ 3 (mod 5) x = ? x ≡ 2 (mod 7) Theorem 4. (The Chinese Remainder Theorem) Let m 1,m 2,…,m n be pairwise relatively prime positive integers and a 1, a 2, …, a n arbitrary integers. Then the system x ≡ a 1 (mod m 1 ) x ≡ a 2 (mod m 2 ) : x ≡ a n (mod m n ) has a unique solution modulo m = m 1 m 2 …m n. ( 即有一解 x, where 0  x < m, 且所有其他解 mod m 都等於 x ) The Chinese Remainder Theorem ( 中國餘數定理 )

Ch3-46 Proof of Thm 4: Let M k = m / m k  1  k  n ∵ m 1, m 2,…, m n are pairwise relatively prime ∴ gcd ( M k, m k ) = 1  integer y k s.t. M k y k ≡ 1 (mod m k ) ( by Thm. 3)  a k M k y k ≡ a k (mod m k ),  1  k  n Let x = a 1 M 1 y 1 +a 2 M 2 y 2 +…+a n M n y n ∵ m i | M j,  i ≠ j ∴ x ≡ a k M k y k ≡ a k (mod m k )  1  k  n x is a solution. All other solution y satisfies y ≡ x (mod m k ). x ≡ a 1 (mod m 1 ) x ≡ a 2 (mod m 2 ) : x ≡ a n (mod m n ) m = m 1 m 2 …m n

Ch3-47 Example 6. (Solve the system in Example 5) Let m = m 1 m 2 m 3 = 3  5  7 = 105 M 1 = m / m 1 = 105 / 3 = 35 ( 也就是 m 2 m 3 ) M 2 = m / m 2 = 105 / 5 = 21 M 3 = m / m 3 = 105 / 7 = ≡ 2 (mod 3)  35  2 ≡ 2  2 ≡ 1 (mod 3) 21 ≡ 1 (mod 5)  21  1 ≡ 1 (mod 5) 15 ≡ 1 (mod 7)  15  1 ≡ 1 (mod 7) ∴ x = a 1 M 1 y 1 + a 2 M 2 y 2 + a 3 M 3 y 3 = 2  35   21   15  1 = 233 ≡ 23 (mod 105) ∴ 最小的解為 23 ,其餘解都等於 t for some t  Z + M1M1 y1y1 M2M2 y2y2 M3M3 y3y3 x ≡ 2 (mod 3) x ≡ 3 (mod 5) x = ? x ≡ 2 (mod 7) 找 y 1 使得 M 1 y 1 = 1 (mod 3)

Ch3-48 Exercise 18. Find all solutions to the system of congruences x ≡ 2 (mod 3) x ≡ 1 (mod 4) x ≡ 3 (mod 5) Sol : a 1 =2, a 2 =1, a 3 =3, m 1 =3, m 2 =4, m 3 =5 m=3  4  5=60 M 1 =20, M 2 =15, M 3 =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 = =233≡53 (mod 60)

Ch3-49 ※ 補充: (when m i is not prime) Ex 20. Find all solutions, if any, to the system of congruences. x≡5 (mod 6) x≡3 (mod 10) x≡8 (mod 15) Sol. Rewrite the system as the following: x ≡ 1 (mod 2)x≡2 (mod 3) i.e., x≡1 (mod 2) x≡2 (mod 3) … x≡3 (mod 5) Exercise : 做完此題 x ≡ 1 (mod 2) x ≡ 2 (mod 3) x≡3 (mod 5)

Ch3-50 ※ 補充: (when m i is a prime power) Ex 21. Find all solutions, if any, to the system of congruences. x≡7 (mod 9) x≡4 (mod 12) x≡16 (mod 21) Sol. Rewrite the system as the following: x≡7 (mod 9) ( 不能拆! ) x≡0 (mod 4) i.e., x≡7 (mod 9) ( 此式取代 x≡1 (mod 3) 式子 ) x≡0 (mod 4) … x≡2 (mod 7) x≡1 (mod 3) x≡2 (mod 7)

Ch3-51 Computer Arithmetic with Large Integers Suppose that m 1,m 2,…,m n be pairwise relatively prime integers greater than or equal to 2 and let m = m 1 m 2 … m n. By the Chinese Remainder Theorem, we can show that an integer a with 0  a < m can be uniquely represented by the n -tuple ( a mod m 1, a mod m 2, …, a mod m n ). Example 7 What are the pairs used to represent the nonnegative integers x<12 when they are represented by the order pair ( x mod 3, x mod 4 )? Sol 0=(0, 0), 1=(1, 1), 2=(2, 2), 3=(0, 3), 4=(1, 0), 5=(2, 1), 6=(0, 2), 7=(1, 3), 8=(2, 0), 9=(0, 1), 10=(1, 2), 11=(2, 3). Exercise : 37

Ch3-52 To perform arithmetic with larger integers, we select moduli (modulus 的複數 ) m 1,m 2,…,m n, where each m i is an integer greater than 2, gcd(m i, m j )=1 whenever i  j, and m=m 1 m 2 …m n is greater than the result of the arithmetic operations we want to carry out.

Ch3-53 Example 8 Suppose that performing arithmetic with integers less than 100 on a certain processor is much quicker than doing arithmetic with larger integers. We can restrict almost all our computations to integers less than 100 if we represent integers using their remainders modulo pairwise relatively prime integers less than 100. For example, 99, 98, 97, and 95 are pairwise relatively prime. every nonnegative integer less than 99  98  97  95 = can be represented uniquely by its remainders when divided by these four moduli. E.g., = (33, 8, 9, 89), and = (32, 92, 42, 16) = (33, 8, 9, 89) + (32, 92, 42, 16) = (65 mod 99, 100 mod 98, 51 mod 97, 105 mod 95) = (65, 2, 51, 10) Use Chinese Remainder Thm to find this sum 

Ch3-54 Theorem 5 (Fermat’s Little Theorem) If p is prime and a is an integer not divisible by p, then a p  1  1 (mod p) Furthermore, for every integer a we have a p  a (mod p) Exercise 27(a) Show that  1 (mod 11) by Fermat’s Little Theorem and noting that = (2 10 ) 34. Proof 11 is prime and 2 is an integer not divisible by 11.  2 10  1 (mod 11)   1 (mod 11) by Thm 5 of Sec. 3.4 (a  b (mod m) and c  d (mod m)  ac  bd (mod m) ) Exercise : Compute (mod 7)