Algorithms and Complexity

Similar documents
0 0 = 1 0 = 0 1 = = 1 1 = 0 0 = 1

= lim(x + 1) lim x 1 x 1 (x 2 + 1) 2 (for the latter let y = x2 + 1) lim

Chapter 22 Lecture. Essential University Physics Richard Wolfson 2 nd Edition. Electric Potential 電位 Pearson Education, Inc.

生物統計教育訓練 - 課程. Introduction to equivalence, superior, inferior studies in RCT 謝宗成副教授慈濟大學醫學科學研究所. TEL: ext 2015

Chapter 6. Series-Parallel Circuits ISU EE. C.Y. Lee

GSAS 安裝使用簡介 楊仲準中原大學物理系. Department of Physics, Chung Yuan Christian University

tan θ(t) = 5 [3 points] And, we are given that d [1 points] Therefore, the velocity of the plane is dx [4 points] (km/min.) [2 points] (The other way)

EXPERMENT 9. To determination of Quinine by fluorescence spectroscopy. Introduction

Chapter 1 Linear Regression with One Predictor Variable

Differential Equations (DE)

Chapter 20 Cell Division Summary

Linear Regression. Applied Linear Regression Models (Kutner, Nachtsheim, Neter, Li) hsuhl (NUK) SDA Regression 1 / 34

邏輯設計 Hw#6 請於 6/13( 五 ) 下課前繳交

Advanced Engineering Mathematics 長榮大學科工系 105 級

雷射原理. The Principle of Laser. 授課教授 : 林彥勝博士 Contents

在雲層閃光放電之前就開始提前釋放出離子是非常重要的因素 所有 FOREND 放電式避雷針都有離子加速裝置支援離子產生器 在產品設計時, 為增加電場更大範圍, 使用電極支援大氣離子化,

Digital Integrated Circuits Lecture 5: Logical Effort

基因演算法 學習速成 南台科技大學電機系趙春棠講解

pseudo-code-2012.docx 2013/5/9

1 dx (5%) andˆ x dx converges. x2 +1 a

Ch.9 Liquids and Solids

Multiple sequence alignment (MSA)

Chapter 8 Lecture. Essential University Physics Richard Wolfson 2 nd Edition. Gravity 重力 Pearson Education, Inc. Slide 8-1

Frequency Response (Bode Plot) with MATLAB

KWUN TONG GOVERNMENT SECONDARY SCHOOL 觀塘官立中學 (Office) Shun Lee Estate Kwun Tong, Kowloon 上學期測驗

Permutation Tests for Difference between Two Multivariate Allometric Patterns

Digital Image Processing

相關分析. Scatter Diagram. Ch 13 線性迴歸與相關分析. Correlation Analysis. Correlation Analysis. Linear Regression And Correlation Analysis

壓差式迴路式均熱片之研製 Fabrication of Pressure-Difference Loop Heat Spreader

國立中正大學八十一學年度應用數學研究所 碩士班研究生招生考試試題

CH 5 More on the analysis of consumer behavior

2019 年第 51 屆國際化學奧林匹亞競賽 國內初選筆試 - 選擇題答案卷

14-A Orthogonal and Dual Orthogonal Y = A X

Chapter 1 Physics and Measurement

CHAPTER 2. Energy Bands and Carrier Concentration in Thermal Equilibrium

Chapter 1 Linear Regression with One Predictor Variable

國立成功大學 航空太空工程學系 碩士論文 研究生 : 柯宗良 指導教授 : 楊憲東

台灣大學開放式課程 有機化學乙 蔡蘊明教授 本著作除另有註明, 作者皆為蔡蘊明教授, 所有內容皆採用創用 CC 姓名標示 - 非商業使用 - 相同方式分享 3.0 台灣授權條款釋出

Lecture Notes on Propensity Score Matching

Candidates Performance in Paper I (Q1-4, )

FUNDAMENTALS OF FLUID MECHANICS. Chapter 8 Pipe Flow. Jyh-Cherng. Shieh Department of Bio-Industrial

Algorithms and Their Complexity

統計學 Spring 2011 授課教師 : 統計系余清祥日期 :2011 年 3 月 22 日第十三章 : 變異數分析與實驗設計

ApTutorGroup. SAT II Chemistry Guides: Test Basics Scoring, Timing, Number of Questions Points Minutes Questions (Multiple Choice)

CSCE 222 Discrete Structures for Computing

Numbers and Fundamental Arithmetic

期中考前回顧 助教 : 王珊彗. Copyright 2009 Cengage Learning

5.5 Using Entropy to Calculate the Natural Direction of a Process in an Isolated System

Elementary Number Theory An Algebraic Apporach

Candidates Performance in Paper I (Q1-4, )

Regression Analysis. Institute of Statistics, National Tsing Hua University, Taiwan

Ch2. Atoms, Molecules and Ions

Discrete Mathematics CS October 17, 2006

Statistics and Econometrics I

FUNDAMENTALS OF FLUID MECHANICS Chapter 3 Fluids in Motion - The Bernoulli Equation

HKDSE Chemistry Paper 2 Q.1 & Q.3

Work Energy And Power 功, 能量及功率

適應控制與反覆控制應用在壓電致動器之研究 Adaptive and Repetitive Control of Piezoelectric Actuators

授課大綱 課號課程名稱選別開課系級學分 結果預視

論文與專利寫作暨學術 倫理期末報告 班級 : 碩化一甲學號 :MA 姓名 : 林郡澤老師 : 黃常寧

原子模型 Atomic Model 有了正確的原子模型, 才會發明了雷射

命名, 構象分析及合成簡介 (Nomenclature, Conformational Analysis, and an Introduction to Synthesis)

Statistical Intervals and the Applications. Hsiuying Wang Institute of Statistics National Chiao Tung University Hsinchu, Taiwan

4 內流場之熱對流 (Internal Flow Heat Convection)

REAXYS NEW REAXYS. RAEXYS 教育訓練 PPT HOW YOU THINK HOW YOU WORK

2001 HG2, 2006 HI6, 2010 HI1

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

Chapter 13. Enzyme Kinetics ( 動力學 ) and Specificity ( 特異性 專一性 ) Biochemistry by. Reginald Garrett and Charles Grisham

ECOM Discrete Mathematics

Ch2 Linear Transformations and Matrices

Chapter 9 Time-Weighted Control Charts. Statistical Quality Control (D. C. Montgomery)

2( 2 r 2 2r) rdrdθ. 4. Your result fits the correct answer: get 2 pts, if you make a slight mistake, get 1 pt. 0 r 1

GRE 精确 完整 数学预测机经 发布适用 2015 年 10 月考试

d) There is a Web page that includes links to both Web page A and Web page B.

Sparse Learning Under Regularization Framework

在破裂多孔介質中的情形 底下是我們考慮的抛物線微分方程式. is a domain and = f. in f. . Denote absolute permeability by. P in. k in. p in. and. and. , and external source by

Algorithms 2/6/2018. Algorithms. Enough Mathematical Appetizers! Algorithm Examples. Algorithms. Algorithm Examples. Algorithm Examples

Chapter 13 Thin-layer chromatography. Shin-Hun Juang, Ph.D.

Boundary Influence On The Entropy Of A Lozi-Type Map. Cellular Neural Networks : Defect Patterns And Stability

Ch. 6 Electronic Structure and The Periodic Table

Chapter 7. The Quantum- Mechanical Model of the Atom. Chapter 7 Lecture Lecture Presentation. Sherril Soman Grand Valley State University

( 选出不同类别的单词 ) ( 照样子完成填空 ) e.g. one three

On the Quark model based on virtual spacetime and the origin of fractional charge

Chapter 2 the z-transform. 2.1 definition 2.2 properties of ROC 2.3 the inverse z-transform 2.4 z-transform properties

Ch 13 Acids and Bases

A Direct Simulation Method for Continuous Variable Transmission with Component-wise Design Specifications

Announcements. CompSci 230 Discrete Math for Computer Science. The Growth of Functions. Section 3.2

Finite Interval( 有限區間 ) open interval ( a, closed interval [ ab, ] = { xa x b} half open( or half closed) interval. Infinite Interval( 無限區間 )

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Algebraic Algorithms in Combinatorial Optimization

With Question/Answer Animations

Hong Kong s temperature record: Is it in support of global warming? 香港的溫度記錄 : 全球暖化的證據?

A proof of the 3x +1 conjecture

2. Suppose that a consumer has the utility function

大原利明 算法点竄指南 点竄術 算 額 絵馬堂

行政院國家科學委員會補助專題研究計畫 成果報告 期中進度報告 ( 計畫名稱 )

Chemistry advance in molecular imaging March 8, 2005 ABSTRACT Zeitsan Tsai

The dynamic N1-methyladenosine methylome in eukaryotic messenger RNA 报告人 : 沈胤

Examples (Chapter )

2012 AP Calculus BC 模拟试卷

Transcription:

Algorithms and Complexity

2.1 ALGORITHMS( 演算法 ) Def: An algorithm is a finite set of precise instructions for performing a computation or for solving a problem The word algorithm algorithm comes from the name of a Persian author, Abu Ja far Mohammed ibn Musa al Khowarizmi (c. 825 A.D.), who wrote a textbook on mathematics. Example: A computer program is an algorithm, more precisely, program = algorithm + data structure Remark: From a mathematical perspective, an algorithm represents a function. The British mathematician Alan Turing proved that some functions cannot be represented by an algorithm.

Properties of algorithms 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. Generality : The procedure should be applicable for all problems of the desired form, not just for a particular set of input values Effectiveness

Pseudo-code Terminology: A good pseudocoding of an algorithm provides a clear prose representation of the algorithm and also is transformable into one or more target programming languages.

Find Maximum Find maximum from a sequence of integers: 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.

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

Remark If the array were pre-sorted into ascending (or descending) order, then faster algorithms could be used.

Binary Search Example example

Remark Sorting algorithms Bubble sort Insertion sort Quick sort Merge sort etc

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. example 7, 2, 1, 4, 5, 9 1, 2, 4, 5, 7, 9

Bubble Sort( 氣泡排序法 ) 設原 sequence 為 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 即可 依此類推

Example Put 3, 2, 4, 1, 5 in increasing order using the bubble sort Sol : 3 2 4 1 5 First pass (i=1) : 2 3 4 1 5 2 3 4 1 5 2 3 1 4 5 Second pass (i=2) : Third pass (i=3) : Fourth pass (i=4) : 1 2 3 4 5 2 3 1 4 5 2 3 1 4 5 2 1 3 4 5 2 1 3 4 5 2 3 1 4 5 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

Bubble Sort( 氣泡排序法 ) Algorithm 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 }

Insertion Sort 設原 sequence 為 a 1,,a n 從 j = 2 開始, 將 a j 插入已排序好 a 1,,a j-1 間的位置, 使得 a 1,,a j 都由小到大排好 j 逐次遞增, 重複上一步驟至做完

Example Use insertion sort to put 3, 2, 4, 1, 5 in increasing order Solution (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

Insertion Sort( 插入排序法 ) Algorithm The Insertion Sort procedure insertion_sort ( a 1,,a n : real numbers with n 2 ) for j := 2 to n begin i := 1 找出 a j 應插入的位置 while a j > a 最後 a i i 1 < a j <= a i i := i + 1 m := a j 將 a for k := 0 to j i 1 i, a i+1,, a j 1 全部往右移一格 a j k := a j k 1 a i := m end { a 1,a 2,,a n are sorted }

Summary Search Find maximum Binary search Sort Bubble sort Insertion sort

Remark Strategies of designing good algorithms: Greedy Divide & conquer Prune & search Dynamic programming Branch and bound Approximation Heuristics

Exercises Section 2.1. 13(b), 34, 38

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.

Asymptotical Upper Bound

BIG OH CLASSES

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

Figure The function f (x) is O(g(x)) Cg(x) f (x) g(x) k f (x) < C g(x) for x > k

Witness

Example Example 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 f (x) is O(x 2 ) (take C = 4 and K =1 ) Method 2: If x > 2, we see that x 2 +2x+1 x 2 +x 2 +x 2 = 3x 2 ( take C = 3 and K = 2 )

Example Example 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 ) Note. The function g in O(g) is chosen to be as small as possible.

Exercise Show that f (n)= n 2 +2n +2 is O(n 2 )

Remark Big O = asymptotic upper bound Big Ω = asymptotic lower bound Big Θ = exact order

Theorem 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 ).

Example 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. Example (see Figure 3 in textbook) 常見 function 的成長速度由小至大排列 : 1 < log n < n < n log n < n 2 < 2 n < n!

Theorem 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)).

Exercises Section 2.2 3,18

2.3 Complexity of Algorithms Question : How can the efficiency of an algorithm be analyzed? Answer : (1) time (2) memory

Complexity 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. ( 是 data structure 探討範圍 )

Example Describe the time complexity of Algorithm Find Max Algorithm ( 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).

Example Describe the time complexity of the binary search algorithm. Algorithm ( 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 Sol : 設 n = 2 k 以簡化計算 ( 若 n < 2 k, 其比較次數必小於等於 n = 2 k 的情況 ) 因 while 迴圈每次執行後整個 list 會切成兩半故最多只能切 k 次就會因 i = j 而跳出迴圈 共比較 2k+2 次 time complexity 為 O(k) = O(log n)

Table Commonly Used Terminology Complexity O(1) O(log n) O(n) O(n log n) O(n b ) O(b n ), b >1 O(n!) Terminology constant complexity Logarithmic complexity Linear complexity n log n complexity Polynomial complexity Exponential complexity Factorial complexity

COMPLEXITY JARGON def: A problem is solvable if it can be solved by an algorithm. Example: Alan Turing defined the halting problem to be that of deciding whether a computational procedure (e.g., a program) halts for all possible input. He proved that the halting problem is unsolvable.

COMPLEXITY JARGON def: A problem is in class P if it is solvable by an algorithm that runs in polynomial time. def: A problem is tractable if it is in class P. def: A problem is in class NP if an algorithm can decide in polynomial time whether a putative solution is really a solution.

Exercises Is P = NP?