M 277 60 h Discrete Mathematics Logic Bibliography
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 (Mat. Lab, 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(a 1, a 2, …an: integers) max = a 1 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 a 1, a 2, …an, or determine that it is not in the list. • First algorithm: Linear search or sequential search. Procedure linear search(x: integer, a 1, a 2, …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, a 1, a 2, …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 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(a 1, a 2, …, an: integers) sum = a 1 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 = a 1 a 2…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…a 1 a 0)2, b = (bnbn-1…b 1 b 0)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
Example
Complixity
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 end {C is the product of A and b} Complexity: O(n 3)
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 + 2 K-1 avec N 1 = 0 ; N 2 = 3 ; • Si K = 50 Nk = 1064 opérations • G 4 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 = 2 n for n= 0, 1, 2, 3, … However, this sequence can be defined by the first term of the sequence, namely, a 0 = 1, and a rule finding a term of the sequence from the previous one, namely, an+1 =2 an 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
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) = 2 f(0) + 3 = 2. 3 + 3 = 9 f(2) = 2 f(1) + 3 = 2. 9 + 3 = 21 f(3) = 2 f(2) + 3 = 2. 21 + 3 = 45 f(4) = 2 f(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 a 0 is specify a 0 = 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.
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, f 0, f 1, f 2, …, are defined by the equations f 0 = 0, f 1 = 1 and fn = fn-1 + fn-2 for n = 2, 3, 4, … f 2 = f 3 = f 4 = f 5 = f 6 = f 1 f 2 f 3 f 4 f 5 + + + f 0 f 1 f 2 f 3 f 4 =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 { 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(nb) O(n!) Constant complexity logarithmic complexity linear complexity n log(n) complexity Polynomial complexity Factorial complexity
- Slides: 36