* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Introduction - ShareStudies.com
Survey
Document related concepts
Approximations of π wikipedia , lookup
Mathematics of radio engineering wikipedia , lookup
Big O notation wikipedia , lookup
Halting problem wikipedia , lookup
Elementary mathematics wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
Transcript
M 277 (60 h) Discrete Mathematics & Logic Bibliography Discrete Mathematics and its applications, Kenneth H. Rosen Numerical Analysis, Richard L. Burden, J. Douglas Faires, Albert C. Reynolds. Prindle, Weber & Schmidt Boston, Massachusetts. Applied Numerical Analysis, Curtis F. Gerald, Patrick O. Wheatley. Addison-wesley publishing company. Contents • Algorithms: Introduction, Complexity, Recursive definitions, Recursive algorithms, program correctness • Solve for the roots of a nonlinear equation. • Solve large systems of linear equations. • Interpolate to find intermediate values within a table of data. • Integrate any function even when known only as a table of values. • Solve ordinary differential equations when given initial values for the variables. • Cubic Spline interpolation Introduction • Numerical analysis is a way to higher mathematics problems on a computer, a technique widely used by scientists and engineers to solve their problems. • A major advantage for numerical analysis is that a numerical answer can be obtained even when a problem has no “analytical” solution. • Actually, evaluating an analytical result to get the numerical answer (approximation) for a specific application is subject to same errors. Using a computer to do numerical analysis • Numerical analysis is so important that extensive commercial software packages are available (MatLab, MATH / LIBRARY)). • Many such symbolic algebra programs are available (Maple, Mathematica,..) Four general steps 1) 2) 3) 4) State the problem clearly, including any simplifying assumptions. Develop a mathematical statement of the problem in a form that can be solved for a numerical answer. Solve the equation(s) that result from step 2. Interpret the numerical result to arrive at a decision. This will require experience and an understanding of the situation… Introduction The rules of logic give precise meaning to mathematical statements. these rules are used to distinguish between valid and invalid mathematical arguments. Algorithms • An algorithm is a definite procedure for solving a problem using a finite number of steps. • Example: Describe an algorithm for finding the largest element in a finite sequence of integers. Procedure max(a1,a2,…an: integers) max = a1 For i = 2 to n if max < ai then max = ai (max is the largest element) • Properties of algorithm: Input: an algorithm has input values from a specified set. Output: from each set of input values an algorithm produces out values from a specified set. The output values comprise the solution to the problem. Definiteness: The steps of an algorithm must be defined precisely. Finiteness: An algorithm should produce the desired output after a finite number of steps for any input in the set. Effectiveness: It must be possible to perform each step of an algorithm exactly and in a finite amount of time. Generality: The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. • Searching algorithms: Locate an element x in a list of distinct elements a1,a2,…an, or determine that it is not in the list. • First algorithm: Linear search or sequential search. 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 is the subscript of term that equals x, or is 0 if x is not found) • Second algorithm: Binary search algorithm. • This algorithm can be used when the list has terms occurring in order of increasing size (smallest to largest). Procedure binary search algorithm(x: integer, a1,a2,…an:distinct integers) i=1 j=n while ( i < j ) begin m = (i + j)/2 if x > am then i = m +1 else j = m end if x = ai then location = i else location = 0 (location is the subscript of term that equal to x or zero if x is not found) • The following example demonstrates how a binary search works. • To search for 19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 First split this list, which has 16 terms, into two smaller lists with eight terms each, namely 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 Then, compare 19 and the largest term in the first list. Since 10 < 19, the search for 19 can be restricted to the list containing the ninth through the sixteenth terms of the original list. 12 13 15 16 18 19 20 22 18 19 20 22 Since 19 is not greater than the largest term of the first of these two lists, which is also 19, the search is restricted to the first list,… • Devise an algorithm that finds the sum of all the integers in a list. • Solution procedure sum(a1,a2,…,an: integers) sum = a1 for i = 2 to n sum = sum + ai (sum has desired value) • Describe an algorithm that interchanges the values of the variables x and y. • Solution procedure interchange (x, y: real numbers) z=x x=y y=z (The minimum number of assignments needed is threes) • Describe an algorithm that will count the number of ones in a bit string by examining each bit of the string to determine whether it is a one bit. • Procedure ones(a: bit string, a = a1a2…an) ones = 0 for i = 1 to n begin if ai = 1 then ones = ones +1 end {ones is the number of ones in the bit strings a} • Describe an algorithm that, given the binary expansions of the integers a and b, determine whether a > b, a < b, or a = b. • Procedure compare(a, b: positive integers and a = (anan-1…a1a0)2, b = (bnbn-1…b1b0)2 ) k=n while ak = bk and k > 0 k = k -1 if ak= bk then print “ a equals b” if ak > bk then print “ a is greater than b” if ak < bk then print “ a is less than b” Complexity of Algorithms • Time complexity: The time required to solve a problem (number of operation). • Space complexity: The computer memory required to solve a problem. Speed Computer Algorithmes Calculs Instructions Operations Ips Mips Instructions per second Méga (Million) Instructions per second Ops operations per second Flops (floating operations per second) Mops Mflops Gops Gflops Mega and Giga operations per second Example year Name Gflops 1976 CRAY 1 0.166 1981 CYBER 205 de CDC 0.2 1985 CRAY 2 1-2 2001 G4 d'Apple 5.5 Complixity P ( x) a0 a1 x a2 x 2 ... an x n Complixity n(n 1) C p ( x ) O nadditions 1 2 3 ... n multiplications O(n 2 ) 2 Horner P ( x) a0 xa1 xa2 x... an ... nadditions et nmultiplications O(n) Matrix Multiplication • Procedure matrix multiplication (Anxn, Bnxn: matrices) for i = 1 to n begin for j = 1 to n begin cij = 0 for q = 1 to n cij cij aiq .bqj end end {C is the product of A and b} Complexity: O(n3) A and B are nxn matrices Read: O(n 2 ) n trace: tr( A) a O(n) ii i 1 Addition : A BC such that c a b O(n 2 ) ij ij ij n Multiplica tion: A*BC such that c a *b O(n3) ij ik kj k 1 a 00 a 01 Determinant (Cramer): a a a a a a 00 11 01 11 10 11 Temps du calcul • Nk est le nombre total d’opérations a effectuer pour calculer un déterminant d’ordre K: Nk = K.Nk-1 + 2K-1 avec N1 = 0 ; N2 = 3 ; • Si K = 50 Nk = 1064 opérations • G4 d'Apple (2001) 5.5 Gflops • 1064 op ≈ 1054 S ≈ 1049 Jours ≈ 1046 ans Recursive Definitions • Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursive. • We can use recursion to define sequences, functions, and sets. • The sequence of powers of 2 are given by an = 2n for n= 0, 1, 2, 3,… However, this sequence can be defined by the first term of the sequence, namely, a0 = 1, and a rule finding a term of the sequence from the previous one, namely, an+1 =2an for n = 0, 1, 2,… Recursively Defined functions • We can define a function the set of nonnegative integers as its domain by 1. Specifying the value of the function at zero, 2. Giving a rule for finding its value at an integer from its values at smaller integers. • Two conditions P(0) is true n N P(n) P(n 1) Then n N is true Recursively Defined functions • 1. 2. • We can define a function the set of nonnegative integers as its domain by Specifying the value of the function at zero, Giving a rule for finding its value at an integer from its values at smaller integers. Ex. Suppose that f is defined recursively by: f(0) = 3 f(n+1) = 2.f(n) + 3 Find f(1), f(2), f(3) and f(4) Solution: f(1) = 2f(0) + 3 = 2.3 + 3 = 9 f(2) = 2f(1) + 3 = 2.9 + 3 = 21 f(3) = 2f(2) + 3 = 2.21 + 3 = 45 f(4) = 2f(3) + 3 = 2.45 + 3 = 93 • Ex.: Give an inductive definition of the factorial function F(n) = n! With F(0) = 1 F(n+1) = (n+1).F(n) F(5) = 5.F(4) = 5.4.F(3) = 5.4.3.F(2) = 5.4.3.2.F(1) = 5.4.3.2.1.F(0) = 5.4.3.2.1.1 = 120 • Ex.: Give a recursive definition of an where a is a real number and n is a nonnegative integer. The recursive definition contains two parts. First a0 is specify a0 = 1. Then the rule for finding an+1 = a.an for n = 1, 2, 3,…is given. Recursive Algorithm • An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. • Give a recursive algorithm for computing an where a is a real number and n is a nonnegative integer. procedure power ( a : real number, n : nonnegative integer) if n 0 then power (a, n) 1 else power (a, n) a * power (a, n - 1) A Recurcive Procedure for Factorials. • Procedure factorial(n: positive integer) if n = 1 then factorial = 1 else factorial(n) = n* factorial(n-1) An Iterative Procedure for Factorials. • Procedure iterative factorial(n: positive integer) x=1 for i = 1 to n x = i* x {x is n!} Fibonacci Numbers • The Fibonacci numbers, f0, f1, f2,…, are defined by the equations f0 = 0, f1 = 1 and fn = fn-1 + fn-2 for n = 2, 3, 4, … f2 = f3 = f4 = f5 = f6 = f1 f2 f3 f4 f5 + + + + + f0 f1 f2 f3 f4 =1+0=1 =1+1=2 =2+1=3 =3+2=5 = 5 +3 = 8 A Recurcive Algorithm for Fibonacci Numbers • Procedure Fibonacci(n: nonnegative integer) if n = 0 then Fibonacci(0) = 0 else if n = 1 Fibonacci(1) = 1 else Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) An Iterative Algorithm for computing Fibonacci Numbers • Procedure iterative fibonacci(n: nonnegative integer) if n = 0 then y = 0 else begin x=0 y=1 for i = 1 to n-1 begin z=x+y x=y y=z end end { y is the nth Fibonacci number} A Recurcive Procedure for S = 1 + 2 + …+(n-1) + n • Procedure Sum(n: positive integer) if n = 1 then Sum = 1 else Sum(n) = n + Sum(n-1) An Iterative Algorithm for computing Sum = 1 + 2 + …+(n-1) + n • Procedure Sum(n: nonnegative integer) Sum = 0 for i = 1 to n Sum = Sum + i end { output : Sum } A Recurcive Procedure for S = 1 + 3 + 5…+ (n-2) + n n: odd number • Procedure Sum(n: odd integer number) if n = 1 then Sum = 1 else Sum(n) = n + Sum(n-2) Commonly used Terminology for the Complexity of algorithm Complexity Terminology O(1) O(log n) O(n) O(n log n) O(nb) O(n!) Constant complexity logarithmic complexity linear complexity n log(n) complexity Polynomial complexity Factorial complexity