* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download ALGO-O
Large numbers wikipedia , lookup
Mathematics of radio engineering wikipedia , lookup
Elementary mathematics wikipedia , lookup
Halting problem wikipedia , lookup
Big O notation wikipedia , lookup
Algorithm characterizations wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
ALGORITHMS Dr. Yasir Ali College of E&ME, National University of Sciences and Technology, Islamabad, Pakistan. December 16, 2015 ALGORITHM An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. ALGORITHM An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. ALGORISM ABU JA’FAR MOHAMMED IBN MUSA AL-KHOWARIZMI (C. 780 - C. 850) ALGORITHM An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. ALGORISM ABU JA’FAR MOHAMMED IBN MUSA AL-KHOWARIZMI (C. 780 - C. 850) Algorism was used for the rules for performing arithmetic using decimal notation. ALGORITHM An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. ALGORISM ABU JA’FAR MOHAMMED IBN MUSA AL-KHOWARIZMI (C. 780 - C. 850) Algorism was used for the rules for performing arithmetic using decimal notation. Algorism evolved into the word algorithm by the eighteenth century. With the growing interest in computing machines, the concept of an algorithm was given a more general meaning, to include all definite procedures for solving problems, not just the procedures for performing arithmetic. Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 1. Set the temporary maximum equal to the first integer in the sequence. Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) 2. 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. Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) 2. 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. 3. Repeat the previous step if there are more integers in the sequence. Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) 2. 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. 3. Repeat the previous step if there are more integers in the sequence. 4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. Finding the Maximum Element in a Finite Sequence. procedure max(a1 , a2 , ..., an : integers) max := a1 for i := 2 to n if max < ai then max := ai return max{max is the largest element} PROPERTIES OF ALGORITHMS Input. An algorithm has input values from a specified set. Output. From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem. Definiteness. The steps of an algorithm must be defined precisely. Correctness. An algorithm should produce the correct output values for each set of input values. Finiteness. An algorithm should produce the desired output after a finite (but perhaps large) 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 The general searching problem can be described as follows: Searching Algorithms The general searching problem can be described as follows: Locate an element x in a list of distinct elements a1 , a2 , · · · , an , or determine that it is not in the list. Searching Algorithms The general searching problem can be described as follows: Locate an element x in a list of distinct elements a1 , a2 , · · · , an , or determine that it is not in the list. The solution to this search problem is Searching Algorithms The general searching problem can be described as follows: Locate an element x in a list of distinct elements a1 , a2 , · · · , an , or determine that it is not in the list. The solution to this search problem is the location of the term in the list that equals x (that is, i is the solution if x = ai ) and is 0 if x is not in the list. THE LINEAR SEARCH The linear search, or sequential search algorithm. The linear search algorithm begins by comparing x and a1 . When x = a1 , the solution is the location of a1 , namely, 1. THE LINEAR SEARCH The linear search, or sequential search algorithm. The linear search algorithm begins by comparing x and a1 . When x = a1 , the solution is the location of a1 , namely, 1. When x 6= a1 , compare x with a2 . THE LINEAR SEARCH The linear search, or sequential search algorithm. The linear search algorithm begins by comparing x and a1 . When x = a1 , the solution is the location of a1 , namely, 1. When x 6= a1 , compare x with a2 . If x = a2 , the solution is the location of a2 , namely, 2. THE LINEAR SEARCH The linear search, or sequential search algorithm. The linear search algorithm begins by comparing x and a1 . When x = a1 , the solution is the location of a1 , namely, 1. When x 6= a1 , compare x with a2 . If x = a2 , the solution is the location of a2 , namely, 2. When x 6= a2 , compare x with a3 .... THE LINEAR SEARCH The linear search, or sequential search algorithm. The linear search algorithm begins by comparing x and a1 . When x = a1 , the solution is the location of a1 , namely, 1. When x 6= a1 , compare x with a2 . If x = a2 , the solution is the location of a2 , namely, 2. When x 6= a2 , compare x with a3 .... Continue this process, comparing x successively with each term of the list until a match is found, where the solution is the location of that term, unless no match occurs. If the entire list has been searched without locating x, the solution is 0. THE LINEAR SEARCH The linear search, or sequential search algorithm. The linear search algorithm begins by comparing x and a1 . When x = a1 , the solution is the location of a1 , namely, 1. When x 6= a1 , compare x with a2 . If x = a2 , the solution is the location of a2 , namely, 2. When x 6= a2 , compare x with a3 .... Continue this process, comparing x successively with each term of the list until a match is found, where the solution is the location of that term, unless no match occurs. If the entire list has been searched without locating x, the solution is 0. The pseudo code for the linear search algorithm ? ALGORITHM - The Linear Search Algorithm. procedure linear search(x : integer , a1 , a2 , ..., an : distinctintegers) i := 1 while (i ≤ n and x 6= ai ) i := i + 1 if i ≤ n then location := i else location := 0 return location {location is the subscript of the term that equals x , or is 0 if x is not found} THE BINARY SEARCH This algorithm can be used when the list has terms occurring in order of increasing size It proceeds by comparing the element to be located to the middle term of the list. THE BINARY SEARCH This algorithm can be used when the list has terms occurring in order of increasing size It proceeds by comparing the element to be located to the middle term of the list. The list is then split into two smaller sublists of the same size, or where one of these smaller lists has one fewer term than the other. THE BINARY SEARCH This algorithm can be used when the list has terms occurring in order of increasing size It proceeds by comparing the element to be located to the middle term of the list. The list is then split into two smaller sublists of the same size, or where one of these smaller lists has one fewer term than the other. The search continues by restricting the search to the appropriate sublist based on the comparison of the element to be located and the middle term. THE BINARY SEARCH This algorithm can be used when the list has terms occurring in order of increasing size It proceeds by comparing the element to be located to the middle term of the list. The list is then split into two smaller sublists of the same size, or where one of these smaller lists has one fewer term than the other. The search continues by restricting the search to the appropriate sublist based on the comparison of the element to be located and the middle term. 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. 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. 10 < 19, Because 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12 13 15 16 Because 16 < 19. 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12 13 15 16 18 19 20 22. Because 16 < 19. The list 18 19 20 22 is split into two lists, namely, 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12 13 15 16 18 19 20 22. Because 16 < 19. The list 18 19 20 22 is split into two lists, namely, 18 19 20 22. Because 19 is not greater than the largest term of the first of these two lists, 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12 13 15 16 18 19 20 22. Because 16 < 19. The list 18 19 20 22 is split into two lists, namely, 18 19 20 22. Because 19 is not greater than the largest term of the first of these two lists, the search is restricted to the first list: 18 19, this list of two terms is split into two lists of one term each: 18 and 19. Because 18 < 19, 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. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12 13 15 16 18 19 20 22. Because 16 < 19. The list 18 19 20 22 is split into two lists, namely, 18 19 20 22. Because 19 is not greater than the largest term of the first of these two lists, the search is restricted to the first list: 18 19, this list of two terms is split into two lists of one term each: 18 and 19. Because 18 < 19, the search is restricted to the second list: which is 19. Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? We have following two measures for an algorithm when input values are of a specified size: Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? We have following two measures for an algorithm when input values are of a specified size: 1. the time used by a computer to solve a problem using the algorithm 2. the amount of computer memory required to implement the algorithm Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? We have following two measures for an algorithm when input values are of a specified size: 1. the time used by a computer to solve a problem using the algorithm 2. the amount of computer memory required to implement the algorithm I Computational Complexity Time Complexity An analysis of the time required to solve a problem of a particular size involves Space Complexity An analysis of the computer memory required involves the space complexity of the algorithm. The Growth of Functions How many number of operations used by algorithm? The Growth of Functions How many number of operations used by algorithm? The time required to solve a problem depends on the number of operations it uses. The Growth of Functions How many number of operations used by algorithm? The time required to solve a problem depends on the number of operations it uses. Using big-O notation, we can estimate the growth of a function. The Growth of Functions How many number of operations used by algorithm? The time required to solve a problem depends on the number of operations it uses. Using big-O notation, we can estimate the growth of a function. Big-O notation is used extensively to estimate the number of operations an algorithm uses as its input grows. With the help of this notation, we can determine whether it is practical to use a particular algorithm to solve a problem as the size of the input increases. The Growth of Functions How many number of operations used by algorithm? The time required to solve a problem depends on the number of operations it uses. Using big-O notation, we can estimate the growth of a function. Big-O notation is used extensively to estimate the number of operations an algorithm uses as its input grows. With the help of this notation, we can determine whether it is practical to use a particular algorithm to solve a problem as the size of the input increases. Furthermore, using big-O notation, we can compare two algorithms to determine which is more efficient as the size of the input grows. Graph of a Function Graph of Some Power Functions Graph of a Multiple of a Function Graph of a Multiple of a Function Increasing and Decreasing Functions Comparison of Functions Comparison of Functions Big-O Notation Let f and g be functions from the set of integers or the set of real numbers 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. [This is read as f (x) is big-oh of g (x).] Big-O Notation Let f and g be functions from the set of integers or the set of real numbers 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. [This is read as f (x) is big-oh of g (x).] Intuitively, the definition that f (x) is O(g (x)) says that f (x) grows slower that some fixed multiple of g (x) as x grows without bound. Big-O Notation Let f and g be functions from the set of integers or the set of real numbers 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. [This is read as f (x) is big-oh of g (x).] Intuitively, the definition that f (x) is O(g (x)) says that f (x) grows slower that some fixed multiple of g (x) as x grows without bound. The constants C and k in the definition of big-O notation are called witnesses to the relationship f (x) is O(g (x)). Big-O Notation Let f and g be functions from the set of integers or the set of real numbers 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. [This is read as f (x) is big-oh of g (x).] Intuitively, the definition that f (x) is O(g (x)) says that f (x) grows slower that some fixed multiple of g (x) as x grows without bound. The constants C and k in the definition of big-O notation are called witnesses to the relationship f (x) is O(g (x)). To establish that f (x) is O(g (x)) we need only one pair of witnesses to this relationship. That is, to show that f (x) is O(g (x)), we need find only one pair of constants C and k, the witnesses, such that |f (x)| ≤ C |g (x)| whenever x > k Big-O f (x) = x 2 + 2x + 1 is O(x 2 ) Big-O f (x) = x 2 + 2x + 1 is O(x 2 ) graph of x 2 + 2x + 1, x 2 and 2x 2 Big-O f (x) = x 2 + 2x + 1 is O(x 2 ) graph of x 2 + 2x + 1, x 2 and 2x 2 Big-O Show that f (x) = x 2 + 2x + 1 is O(x 2 ) Big-O Show that f (x) = x 2 + 2x + 1 is O(x 2 ) when x > 1 ? Find witness? Big-O Show that f (x) = x 2 + 2x + 1 is O(x 2 ) when x > 1 ? Find witness? when x > 2 ? Find witness? Big-O Show that f (x) = x 2 + 2x + 1 is O(x 2 ) when x > 1 ? Find witness? when x > 2 ? Find witness? Show that 7x 2 is O(x 3 ). Big-O Show that f (x) = x 2 + 2x + 1 is O(x 2 ) when x > 1 ? Find witness? when x > 2 ? Find witness? Show that 7x 2 is O(x 3 ). Note that if C and k are one pair of witnesses, then any pair C 0 and k 0 , where C < C 0 and k < k 0 , is also a pair of witnesses, because |f (x)| ≤ C |g (x)| ≤ C 0 |g (x)| whenever x > k > k 0 . Big-Omega, (Ω) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Ω(g (x)) if there are positive constants C and k such that Big-Omega, (Ω) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Ω(g (x)) if there are positive constants C and k such that |f (x)|≥C |g (x)| whenever x > k. This is read as “f (x) is big-Omega of g (x)” Big-Omega, (Ω) f (x) = 8x 3 + 5x 2 + 7 is Ω(g (x)), where g (x) is the function g (x) = x 3 Big-Omega, (Ω) f (x) = 8x 3 + 5x 2 + 7 is Ω(g (x)), where g (x) is the function g (x) = x 3 This is easy to see because Big-Omega, (Ω) f (x) = 8x 3 + 5x 2 + 7 is Ω(g (x)), where g (x) is the function g (x) = x 3 This is easy to see because f (x) = 8x 3 + 5x 2 + 7 ≥ 8x 3 for all positive real number x. O vs Ω O vs Ω f (x) is Ω(g (x)) if and only if g (x) is O(f (x)). Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g (x)) if Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g (x)) if f (x) is O(g (x)) and f (x) is Ω(g (x)). Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g (x)) if f (x) is O(g (x)) and f (x) is Ω(g (x)). Then f (x) is Θ(g (x)). Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g (x)) if f (x) is O(g (x)) and f (x) is Ω(g (x)). Then f (x) is Θ(g (x)). We say that f is big-Theta of g (x), Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g (x)) if f (x) is O(g (x)) and f (x) is Ω(g (x)). Then f (x) is Θ(g (x)). We say that f is big-Theta of g (x),that f (x) is of order g (x), and that f (x) and g (x) are of the same order. Big-Theta,(Θ) Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g (x)) if f (x) is O(g (x)) and f (x) is Ω(g (x)). Then f (x) is Θ(g (x)). We say that f is big-Theta of g (x),that f (x) is of order g (x), and that f (x) and g (x) are of the same order. f (x) is Θ(g (x)) if and only if there are real numbers C1 and C2 and a positive real number k such that C1 |g (x)| ≤ |f (x)| ≤ C2 |g (x)| whenever x > k. The existence of the constants C1 , C2 , and k tells us that f (x) is Ω(g (x)) and that f (x) is O(g (x)), respectively. Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? We have following two measures for an algorithm when input values are of a specified size: Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? We have following two measures for an algorithm when input values are of a specified size: 1. the time used by a computer to solve a problem using the algorithm 2. the amount of computer memory required to implement the algorithm Complexity Of Algorithms When does an algorithm provide a satisfactory solution to a problem? FIRST It must always produce the correct answer. SECOND It should be efficient. How can the efficiency of an algorithm be analyzed? We have following two measures for an algorithm when input values are of a specified size: 1. the time used by a computer to solve a problem using the algorithm 2. the amount of computer memory required to implement the algorithm I Computational Complexity Time Complexity An analysis of the time required to solve a problem of a particular size involves Space Complexity An analysis of the computer memory required involves the space complexity of the algorithm. Time Complexity The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. Time Complexity The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. a- Let b(n) be the minimum number of elementary operations required to execute A for all possible input sets of size n. Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. a- Let b(n) be the minimum number of elementary operations required to execute A for all possible input sets of size n.If b(n) is Θ(g (n)), we say that in the best case, A is Θ(g (n)) Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. a- Let b(n) be the minimum number of elementary operations required to execute A for all possible input sets of size n.If b(n) is Θ(g (n)), we say that in the best case, A is Θ(g (n)) or A has a best-case order of g (n). Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. a- Let b(n) be the minimum number of elementary operations required to execute A for all possible input sets of size n.If b(n) is Θ(g (n)), we say that in the best case, A is Θ(g (n)) or A has a best-case order of g (n). b- Let w (n) be the maximum number of elementary operations required to execute A for all possible input sets of size n. Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. a- Let b(n) be the minimum number of elementary operations required to execute A for all possible input sets of size n.If b(n) is Θ(g (n)), we say that in the best case, A is Θ(g (n)) or A has a best-case order of g (n). b- Let w (n) be the maximum number of elementary operations required to execute A for all possible input sets of size n.If w (n) is Θ(g (n)), we say that in the worst case, A is Θ(g (n)) Let A be an algorithm. Time complexity of A? 1. Suppose the number of elementary operations performed when A is executed for an input of size n depends on n alone and not on the nature of the input data; say it equals f (n). If f (n) is Θ(g (n)), we say that A is Θ(g (n)) or A is of order g (n). 2. Suppose the number of elementary operations performed when A is executed for an input of size n depends on the nature of the input data as well as on n. a- Let b(n) be the minimum number of elementary operations required to execute A for all possible input sets of size n.If b(n) is Θ(g (n)), we say that in the best case, A is Θ(g (n)) or A has a best-case order of g (n). b- Let w (n) be the maximum number of elementary operations required to execute A for all possible input sets of size n.If w (n) is Θ(g (n)), we say that in the worst case, A is Θ(g (n)) or A has a worst-case order of g (n). Describe the time complexity of Algorithm for finding the maximum element in a finite set of integers.1 1 The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. Describe the time complexity of Algorithm for finding the maximum element in a finite set of integers.2 2 The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. Describe the time complexity of Algorithm for finding the maximum element in a finite set of integers.3 3 The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. Describe the time complexity of Algorithm for finding the maximum element in a finite set of integers.4 4 The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. Describe the time complexity of Algorithm for finding the maximum element in a finite set of integers.4 Exactly 2n − 2 + 1 = 2n − 1 comparisons are used whenever this algorithm is applied. 4 The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. Describe the time complexity of Algorithm for finding the maximum element in a finite set of integers.4 Exactly 2n − 2 + 1 = 2n − 1 comparisons are used whenever this algorithm is applied. Hence, the algorithm for finding the maximum of a set of n elements has time complexity Θ(n), measured in terms of the number of comparisons used. 4 The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. Describe the time complexity of the binary search algorithm5 5 ignoring the time required to compute m = b(i + j)/2c in each iteration of the loop in the algorithm Describe the time complexity of the binary search algorithm6 6 ignoring the time required to compute m = b(i + j)/2c in each iteration of the loop in the algorithm Describe the time complexity of the binary search algorithm7 7 ignoring the time required to compute m = b(i + j)/2c in each iteration of the loop in the algorithm Describe the time complexity of the binary search algorithm8 8 ignoring the time required to compute m = b(i + j)/2c in each iteration of the loop in the algorithm Describe the time complexity of the binary search algorithm9 9 ignoring the time required to compute m = b(i + j)/2c in each iteration of the loop in the algorithm Describe the time complexity of the binary search algorithm10 10 ignoring the time required to compute m = b(i + j)/2c in each iteration of the loop in the algorithm Now n = 2k or k = log2 n Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. Now n = 2k or k = log2 n Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. If n is not a power of 2, the original list is expanded to a list with 2k+1 terms, where k = blog nc, Now n = 2k or k = log2 n Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. If n is not a power of 2, the original list is expanded to a list with 2k+1 terms, where k = blog nc, The search requires at most 2dlog ne + 2 comparisons. Now n = 2k or k = log2 n Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. If n is not a power of 2, the original list is expanded to a list with 2k+1 terms, where k = blog nc, The search requires at most 2dlog ne + 2 comparisons. It follows that in the worst case, binary search requires O(log n) comparisons. Now n = 2k or k = log2 n Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. If n is not a power of 2, the original list is expanded to a list with 2k+1 terms, where k = blog nc, The search requires at most 2dlog ne + 2 comparisons. It follows that in the worst case, binary search requires O(log n) comparisons. Note that in the worst case, 2 log n + 2 comparisons are used by the binary search. Hence, the binary search uses Θ(log n) comparisons in the worst case, Now n = 2k or k = log2 n Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. If n is not a power of 2, the original list is expanded to a list with 2k+1 terms, where k = blog nc, The search requires at most 2dlog ne + 2 comparisons. It follows that in the worst case, binary search requires O(log n) comparisons. Note that in the worst case, 2 log n + 2 comparisons are used by the binary search. Hence, the binary search uses Θ(log n) comparisons in the worst case, because 2 log n + 2 = Θ(logn). Let f and g be real-valued functions defined on the same set of non-negative real numbers. Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) if, and only if f (x) is Θ(g (x)). Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) if, and only if f (x) is Θ(g (x)). 2. f (x) is Ω(g (x)) if, and only if, Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) if, and only if f (x) is Θ(g (x)). 2. f (x) is Ω(g (x)) if, and only if, g (x) is O(f (x)). Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) if, and only if f (x) is Θ(g (x)). 2. f (x) is Ω(g (x)) if, and only if, g (x) is O(f (x)). 3. f (x) is O(g (x)) and g (x) is O(h(x)), then f (x) is O(h(x)) Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) if, and only if f (x) is Θ(g (x)). 2. f (x) is Ω(g (x)) if, and only if, g (x) is O(f (x)). 3. f (x) is O(g (x)) and g (x) is O(h(x)), then f (x) is O(h(x)) For any rational numbers r and s, if x > 1 and r < s, then x r < x s if r < s, then x r is O(x s ) Let f and g be real-valued functions defined on the same set of non-negative real numbers. 1. f (x) is Ω(g (x)) and f (x) is O(g (x)) if, and only if f (x) is Θ(g (x)). 2. f (x) is Ω(g (x)) if, and only if, g (x) is O(f (x)). 3. f (x) is O(g (x)) and g (x) is O(h(x)), then f (x) is O(h(x)) For any rational numbers r and s, if x > 1 and r < s, then x r < x s if r < s, then x r is O(x s ) Show that for any real number x, if x > 1, then 3x 3 + 2x + 7 ≤ 12x 3 . To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, f (x) = 2x 4 + 3x 3 + 5 Observe that for all real numbers x > 0, g (x) = x 4 To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, f (x) = 2x 4 + 3x 3 + 5 g (x) = x 4 Observe that for all real numbers x > 0, 2x 4 ≤ 2x 4 + 3x 3 + 5 To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, f (x) = 2x 4 + 3x 3 + 5 g (x) = x 4 Observe that for all real numbers x > 0, 2x 4 ≤ 2x 4 + 3x 3 + 5 Let C = 2 and k = 0, gives 2|x 4 | ≤ |2x 4 + 3x 3 + 5| implies 2x 4 + 3x 3 + 5 is of Ω(x 4 ) To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, f (x) = 2x 4 + 3x 3 + 5 g (x) = x 4 Observe that for all real numbers x > 0, 2x 4 ≤ 2x 4 + 3x 3 + 5 Let C = 2 and k = 0, gives 2|x 4 | ≤ |2x 4 + 3x 3 + 5| implies 2x 4 + 3x 3 + 5 is of Ω(x 4 ) Also for x > 1, 2x 4 + 3x 3 + 5 ≤ 2x 4 + 3x 4 + 5x 4 = 10x 4 . To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, f (x) = 2x 4 + 3x 3 + 5 g (x) = x 4 Observe that for all real numbers x > 0, 2x 4 ≤ 2x 4 + 3x 3 + 5 Let C = 2 and k = 0, gives 2|x 4 | ≤ |2x 4 + 3x 3 + 5| implies 2x 4 + 3x 3 + 5 is of Ω(x 4 ) Also for x > 1, 2x 4 + 3x 3 + 5 ≤ 2x 4 + 3x 4 + 5x 4 = 10x 4 . |2x 4 + 3x 3 + 5| ≤ 10|x 4 | whenever x > 1 implies 2x 4 + 3x 3 + 5 is of O(x 4 ). To show that 2x 4 + 3x 3 + 5 is Θ(x 4 ). Define functions f and g as follows. For all non-negative real numbers x, f (x) = 2x 4 + 3x 3 + 5 g (x) = x 4 Observe that for all real numbers x > 0, 2x 4 ≤ 2x 4 + 3x 3 + 5 Let C = 2 and k = 0, gives 2|x 4 | ≤ |2x 4 + 3x 3 + 5| implies 2x 4 + 3x 3 + 5 is of Ω(x 4 ) Also for x > 1, 2x 4 + 3x 3 + 5 ≤ 2x 4 + 3x 4 + 5x 4 = 10x 4 . |2x 4 + 3x 3 + 5| ≤ 10|x 4 | whenever x > 1 implies 2x 4 + 3x 3 + 5 is of O(x 4 ). Since 2x 4 + 3x 3 + 5 is of Ω(x 4 ) and of O(x 4 ) therefore, 2x 4 + 3x 3 + 5 is of Θ(x 4 ) Show that 3x 3 − 1000x − 200 is O(x 3 )11 . |3x 3 − 1000x − 200| ≤ C |x 3 | whenever x > k. 11 Triangular Inequality |a + b| ≤ |a| + |b| Show that 3x 3 − 1000x − 200 is O(x 3 )11 . |3x 3 − 1000x − 200| ≤ C |x 3 | whenever x > k. Show 3x 3 − 1000x − 200 is O(x s ) for s ≥ 3. 11 Triangular Inequality |a + b| ≤ |a| + |b| Show that 3x 3 − 1000x − 200 is O(x 3 )11 . |3x 3 − 1000x − 200| ≤ C |x 3 | whenever x > k. Show 3x 3 − 1000x − 200 is O(x s ) for s ≥ 3. Show 3x 3 − 1000x − 200 is Ω(x 3 ). 11 Triangular Inequality |a + b| ≤ |a| + |b| Show that 3x 3 − 1000x − 200 is O(x 3 )11 . |3x 3 − 1000x − 200| ≤ C |x 3 | whenever x > k. Show 3x 3 − 1000x − 200 is O(x s ) for s ≥ 3. Show 3x 3 − 1000x − 200 is Ω(x 3 ). Show 3x 3 − 1000x − 200 is Ω(x r ) for r < 3. 11 Triangular Inequality |a + b| ≤ |a| + |b| Show that 3x 3 − 1000x − 200 is O(x 3 )11 . |3x 3 − 1000x − 200| ≤ C |x 3 | whenever x > k. Show 3x 3 − 1000x − 200 is O(x s ) for s ≥ 3. Show 3x 3 − 1000x − 200 is Ω(x 3 ). Show 3x 3 − 1000x − 200 is Ω(x r ) for r < 3. Theorem Suppose a0 , a1 , a2 , ..., an are real numbers and an 6= 0. 1. an x n + an−1 x n−1 + · · · + a1 x + a0 is O(x s ) for all integers s≥n 2. an x n + an−1 x n−1 + · · · + a1 x + a0 is Ω(x r ) for all integers r ≤n 3. an x n + an−1 x n−1 + · · · + a1 x + a0 is Θ(x n ) 11 Triangular Inequality |a + b| ≤ |a| + |b| The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 I f ∗∗ = log(x 2 + 1). For x > 2, 1 log(x 2 + 1) ≤ log(2x 2 ) = log 2 + 2 log x ≤ 3 log x Hence f1∗∗ is of O(log x). The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 I f ∗∗ = log(x 2 + 1). For x > 2, 1 log(x 2 + 1) ≤ log(2x 2 ) = log 2 + 2 log x ≤ 3 log x Hence f1∗∗ is of O(log x). f1 (x) is O(x log x) The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 I f ∗∗ = log(x 2 + 1). For x > 2, 1 log(x 2 + 1) ≤ log(2x 2 ) = log 2 + 2 log x ≤ 3 log x Hence f1∗∗ is of O(log x). f1 (x) is O(x log x) f2 (x) : f2 (x) = 3x 2 is O(x 2 ) The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 I f ∗∗ = log(x 2 + 1). For x > 2, 1 log(x 2 + 1) ≤ log(2x 2 ) = log 2 + 2 log x ≤ 3 log x Hence f1∗∗ is of O(log x). f1 (x) is O(x log x) f2 (x) : f2 (x) = 3x 2 is O(x 2 ) f (x) is O(max(x log x), (x 2 )). The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 I f ∗∗ = log(x 2 + 1). For x > 2, 1 log(x 2 + 1) ≤ log(2x 2 ) = log 2 + 2 log x ≤ 3 log x Hence f1∗∗ is of O(log x). f1 (x) is O(x log x) f2 (x) : f2 (x) = 3x 2 is O(x 2 ) f (x) is O(max(x log x), (x 2 )). Because x log x ≤ x 2 for x > 1. The Growth of Combinations of Functions Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 + f2 )(x) is O(max(|g1 (x)|, |g2 (x)|)). Suppose that f1 (x) is O(g1 (x)) and that f2 (x) is O(g2 (x)). Then (f1 .f2 )(x) is O((|g1 (x)|.|g2 (x)|)). Example Give a big-O estimate for f (x) = (x + 1) log(x 2 + 1) + 3x 2 . Here f1 (x) = (x + 1) log(x 2 + 1) and f2 (x) = 3x 2 f1 (x) : f1 (x) is product of two functions, I f ∗ = x + 1 is O(x) 1 I f ∗∗ = log(x 2 + 1). For x > 2, 1 log(x 2 + 1) ≤ log(2x 2 ) = log 2 + 2 log x ≤ 3 log x Hence f1∗∗ is of O(log x). f1 (x) is O(x log x) f2 (x) : f2 (x) = 3x 2 is O(x 2 ) f (x) is O(max(x log x), (x 2 )). Because x log x ≤ x 2 for x > 1. Therefore f (x) is O(x 2 ). Quiz # 2 1. Show that x 5 is not O(x 2 ). Quiz # 2 1. Show that x 5 is not O(x 2 ). 2. Show 7x 4 − 95x 3 + 3 is Ω(x 4 ) Quiz # 2 1. Show that x 5 is not O(x 2 ). 2. Show 7x 4 − 95x 3 + 3 is Ω(x 4 ) 3. How can big-O notation be used to estimate the sum of the first n positive integers? 4. Give big-O estimates for the factorial function and the logarithm of the factorial function. Quiz # 2 1. Show that x 5 is not O(x 2 ). 2. Show 7x 4 − 95x 3 + 3 is Ω(x 4 ) 3. How can big-O notation be used to estimate the sum of the first n positive integers? 4. Give big-O estimates for the factorial function and the logarithm of the factorial function. 5. Show that 3x 2 + 8x log x is Θ(x 2 ).