Download presentation
Presentation is loading. Please wait.
Published byNelson Stephens Modified over 9 years ago
1
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds
2
Prof. Amr Goneid, AUC2 Complexity Bounds
3
Prof. Amr Goneid, AUC3 Complexity Bounds Algorithms The 4 Questions about Algorithms Space & Time Complexities Bounds
4
Prof. Amr Goneid, AUC4 1. Algorithms The word Algorithm comes from the name of Abu Ja’afar Mohamed ibn Musa Al Khowarizmi (c. 825 A.D.) An Algorithm is a procedure to do a certain task An Algorithm is supposed to solve a general, well- specified problem
5
Prof. Amr Goneid, AUC5 Algorithms Example: Euclide’s Algorithm for the GCD ALGORITHM Euclid(m, n) //Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n != 0 do r ←m mod n m←n n←r return m
6
Prof. Amr Goneid, AUC6 Algorithms Euclide’s Algorithm for the GCD (Recursive Version) function gcd(m, n) if n = 0 return m else return gcd (n, m mod n) "The Euclidean algorithm is the granddaddy of all algorithms, because it is the oldest nontrivial algorithm that has survived to the present day”. Donald Knuth, The Art of Computer Programming, Vol. 2
7
Prof. Amr Goneid, AUC7 Algorithms Example: The Sieve of Eratosthenes is a method used to compute all primes less than N. It was invented by Eratosthenes of Cyrene (276 BC - 194 BC).Eratosthenes of Cyrene The algorithm to do this uses an array of size N+1: a 0, a 1, a 2,...a i,....., a N and the objective is to set a i = 1 if i is a prime, and to zero if it is not.
8
Prof. Amr Goneid, AUC8 Algorithms The Algorithm:
9
Prof. Amr Goneid, AUC9 Algorithms Example: Sorting Problem: Input: A sequence of keys {a 1, a 2, …, a n } output: A permutation (re-ordering) of the input, {a’ 1, a’ 2, …, a’ n } such that a’ 1 ≤ a’ 2 ≤ …≤ a’ n An instance of the problem might be sorting an array of names or sorting an array of integers. An algorithm is supposed to solve all instances of the problem
10
Prof. Amr Goneid, AUC10 Example: Selection Sort Algorithm Solution: “From those elements that are currently unsorted, find the smallest and place it next in the sorted list” Algorithm: for each i = 0.. n-2 find smallest element in sub-array a[i] to a[n-1] swap that element with that at the start of the sub- array
11
Prof. Amr Goneid, AUC11 Algorithms should be: Transparent Correct Complete Writeable Maintainable Easy to use Efficient
12
Prof. Amr Goneid, AUC12 2. The 4 Questions about Algorithms How to Design Algorithms ? How to Validate Algorithms ? How to Analyze Algorithms ? How to Test Algorithms ?
13
Prof. Amr Goneid, AUC13 The main goal is to determine the cost of running an algorithm and how to reduce that cost. Cost is expressed as Complexity Time Complexity Space Complexity 3. Analysis of Algorithms
14
Prof. Amr Goneid, AUC14 Time Complexity Depends on: - Machine Speed - Size of Data and Number of Operations needed (n) Space Complexity Depends on: - Size of Data - Size of Program Space & Time Complexities
15
Prof. Amr Goneid, AUC15 Expressed as T(n) = number of operations required. (n) is the Problem Size: n could be the number of specific operations, or the size of data (e.g. an array) or both. Time Complexity
16
Prof. Amr Goneid, AUC16 Number of Operations Each "simple" operation (+, -, =, =) is one operation. Loops and subroutine calls are not simple operations, but depend upon the size of the data and the contents of a subroutine. We do not want ``sort'' to be a single step operation. Each memory access is one operation. We measure T(n) of an algorithm by counting the number of operations.
17
Prof. Amr Goneid, AUC17 Example (1): Factorial Function factorial (n) { f = 1; if ( n > 0 ) for (i = 1 to n) f = f * i ; return f ; } Let T(n) = Number of multiplications. For a given n, then T(n) = n (always) Number of Operations T(n)
18
Prof. Amr Goneid, AUC18 Complexity of the Factorial Algorithm Because T(n) = n always, then T(n) = (n) T(n) n (n)
19
Prof. Amr Goneid, AUC19 Number of Operations T(n) Example (2): Linear Search in an array a[ ] linSearch (a, target, n) { for (i = 0 to n-1) if (a i == target) return i; return -1; } T(n) = number of array element comparisons. Best case: T(n) = 1 Worst case:T(n) = n
20
Prof. Amr Goneid, AUC20 Complexity of the Linear Search Algorithm T(n) = 1 in the best case. T(n) = n in the worst case We write that as: T(n) = (1) and T(n) = O(n) T(n) n O(n) (1)
21
Prof. Amr Goneid, AUC21 4. Bounds Bounds describe the limiting behavior of algorithm complexity at large (n). They are: Worst Case: Upper Bound (Big O complexity) Best Case: Lower Bound (Big complexity) Exact (Big complexity)
22
Prof. Amr Goneid, AUC22 Bounds An Average Case complexity can also be obtained if the probabilities of the different data sets are known. Algorithms are usually compared on the basis of the upper bound (Big O complexity)
23
Prof. Amr Goneid, AUC23 Bounds T(n) is Time Complexity T(n) nn UB LB Tav UB1 UB2 n0n0
24
Prof. Amr Goneid, AUC24 Bounds We say T(n) = (f(n)), or Big iff there exist positive constants c 1, c 2, n 0 such that c 1 f(n) ≤ T(n) ≤ c 2 f(n) for all n ≥ n 0 c 1 f(n) T(n) n c 2 f(n)
25
Prof. Amr Goneid, AUC25 Bounds It follows that, if an algorithm always costs T(n) = c f(n) for the same (n n0 ) independent of the data, it is an Exact algorithm. In this case we say T(n) = (f(n)), or Big . An example is the factorial function with a cost of T(n) = n for n 0 (always). Hence T(n) = (n) is
26
Prof. Amr Goneid, AUC26 Exact Algorithms, Another Example T(n) is independent of data values Example: position of maximum in an array // elements in array locations 0…n-1 m = 0; for (i = 0 to n-1) if (a i > a m ) m = i; T(n) = the number of comparisons of array elements T(n) = n for n ≥ 1 hence T(n) = (n) comparison
27
Prof. Amr Goneid, AUC27 Bounds If the cost T(n) of an algorithm for a given size (n) changes with the data values, it is not an exact algorithm. In this case, we find the Best Case (Lower Bound) and the Worst Case (Upper Bound)
28
Prof. Amr Goneid, AUC28 Bounds Upper Bound (Big O): T(n) is O(f(n)) if T(n) grows at most as fast as f(n), i.e. T(n) = O(f(n)) iff T(n) c f(n) for n n 0 c and n 0 are positive constants.
29
Prof. Amr Goneid, AUC29 Bounds T(n) n n n0n0 f(n) n0n0 T(n) = O(f(n)) T(n) = (f(n))
30
Prof. Amr Goneid, AUC30 Bounds Lower Bound (Big ): T(n) is (f(n)) if T(n) grows at least as fast as f(n), i.e. T(n) = (f(n)) iff T(n) ≥ c f(n) for n n 0 c and n 0 are positive constants. The linear search function is an example where T(n) = (1) and T(n) = O(n)
31
Prof. Amr Goneid, AUC31 Constants do not matter If T(n) = constant * function of (n), i.e. T(n) = c f(n) we still say that T(n) is O(f(n)) or (f(n)) or (f(n)). Examples: T(n) = 4 (best case) then T(n) = (1) T(n) = 6 n 2 (worst case) then T(n) = O(n 2 ) T(n) = 3 n (always) then T(n) = (n)
32
Prof. Amr Goneid, AUC32 Constants do not matter T(n) = 4 (best case) then T(n) = (1) T(n) = 6 n 2 (worst case) then T(n) = O(n 2 ) T(n) = 3 n (always) then T(n) = (n) Number of Operations is of Complexity
33
Prof. Amr Goneid, AUC33 More Examples T(n) ≤ 5.5 n 3 for n ≥ 2 hence T(n) = O(n 3 ) T(n) ≥ 30 n for n ≥ 1 hence T(n) = (n) T(n) = 6 log 2 n for n ≥ 2 hence T(n) = (lg n) (constants do not matter)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.