Presentation on theme: "Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices"— Presentation transcript:
1 Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices Algorithm: specifying a sequence of steps used to solve a problem.Computational complexity of an algorithm: what are the computer resources needed to use this algorithm to solve a problem of a specified size?Integers: properties of integers, division of integers, algorithms involving integers.Matrices: basic material about matrices, matrix arithmetic.
2 2.1 AlgorithmsDefinition 1. An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem.Example Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers.SolutionSet the temporary maximum equal to the first integer in the sequence.Compare the next integer in the sequence to the temporary maximum, and set the larger one to be temporary maximum.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 maximum in the sequence.Instead of using a particular computer language, we use a form of pseudocode.
3 The properties of algorithms: InputOutputDefinitenessCorrectnessFinitenessEffectivenessGenerality
4 Example 2 Describe an algorithm for finding an element x in a list of distinct elements
6 2.2 Complexity of Algorithms Assume that both algorithms A and B solve the problem P.Which one is better?Time complexity: the time required to solve a problem of a specified size.Space complexity: the computer memory required to solve a problem of a specified size.The time complexity is expressed in terms of the number of operations used by the algorithm.Worst case analysis: the largest number of operations needed to solve the given problem using this algorithm.Average case analysis: the average number of operations used to solve the problem over all inputs.
7 Example 3 Analyze the time complexities of Algorithm 1 and 2 Number of operations1n2(n-1)3n-112(n+1)n23n+5
8 Number of operations = 4 log n+4 12Number of operations = 4 log n+42
9 For finding x in a list of n elements, we have two algorithms Which useWhich one is better?A constant factor does not influence the running time so much.Therefore, 3n+5 grows faster than when n becomes larger. Usually, we say that 3n+5 is O(n) and say that is O( ).
10 Running time for a problem with size Time necessary operations Orders of GrowthRunning time for a problem with sizeRunningTimenecessaryoperationsOperationPer secondinstant1 second11.5 daysNever enddaysInstantUsing silicon computer, no matter how fast CPU will be you can never solve the problem whose running time is exponential !!!
11 Asymptotic Notations: O-notation Definition 2.1 A function f(n) is said to be O(g(n)) if there exist some constantN
12 Example 3 Prove 2n+1=O(n)Example 5List the following function in O-notation in increasing order:
13 2.2 The Integers and Division We discuss the properties of integers which belongs tothe branch of Mathematics called number theory.b is a multiple of aa is a factor of bExample 1 Determine whether 3|7 and whether 3|12.Solution: It follows that 3|7 and 3|12.
14 Basic properties of divisibility of integers Theorem 1. Let a, b, and c be integers. ThenIf a|b and a|c, then a|(b+c).If a|b, then a|bc for all integers c.If a|b and b|c, then a|c.ProofThere are s and t such that b=as and c=at. Therefore, b+c=a(s+t).There is s such that b=as. Therefore, bc=a(sc)There are s and t such that b=as and c=bt, therefore, c=a(st)
15 Definition 2. A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite.Example 2. Is 7 a prime?The primes less than 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97.Theorem 2. Every positive integer can be written uniquely as the uniquely as the product of primes in order of increasing size.
16 divisordividendquotientremainderExample 4 What are the quotient and remainder when 101 is divided by 11?Solution: We havequotientremainder
17 Definition 3. Let a and b be integers Definition 3. Let a and b be integers. The largest integer d such that d|a and d|b is called the greatest common divisor of a and b, denoted by gcd(a,b).Example 5 What is gcd(24,36) and gcd(17,22)?Definition 4. The integers a and b are relatively prime if gcd(a,b)=1.Definition 5. The least common multiple of the positive integers a and b is the smallest positive integer that is divisible by both a and b, denoted by lcm(a,b).
18 Definition 6. Let a be an integer and m be a positive integer Definition 6. Let a be an integer and m be a positive integer. We denoted by a mod m the remainder when a is divided by m.Example mod 5 = 2, mod 9=2, and 2001 mod 101 = 82.
19 2.4 Integers and Algorithms The Euclidean Algorithm: Find the greatest common divisor of two positive integersLemma 1 Let a=bq+r, where a,b,q, and r are integers. Then gcd(a,b)=gcd(b,r).
20 Example 1Find the greatest common divisor of 414 and 662 using the Euclidean Algorithm.Solution:Hence, gcd(414,662）=2, since 2 is the last nonzero remainder.