Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos

Similar presentations


Presentation on theme: "IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos"— Presentation transcript:

1 IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
Developing Efficient Algorithms IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition by Y. Daniel Liang

2 Executing Time Suppose two algorithms perform the same task such as
search (linear search vs. binary search) and sorting (selection sort vs. insertion sort). Which one is better? One possible approach to answer this question is to implement these algorithms in Java and run the programs to get execution time. But there are two problems for this benchmarking approach: First, there are many tasks running concurrently on a computer. The execution time of a particular program is dependent on the system load. Second, the execution time is dependent on specific input. Consider linear search and binary search for example. If an element to be searched happens to be the first in the list, linear search will find the element quicker than binary search. 2

3 Growth Rate It is very difficult / inaccurate to compare algorithms by measuring their execution time. To overcome these problems, a theoretical approach was developed to analyze algorithms independent of computers and specific input. This approach provides an approximated description of how a change on the size of the input affects the number of statements to be executed. In this way, you can see how fast an algorithm’s execution time increases as the input size increases, so you can compare two algorithms by examining their growth rates. 3

4 Analysis of Algorithms
Google search on ‘Donald Knut’ processed on April 2, 2013 4

5 F (N : input size) ⟶ ( N: number of executed statements )
Growth Rate Goal : Estimate the execution ‘time’ of an algorithm in relation to the input size. F (N : input size) ⟶ ( N: number of executed statements ) Algorithm_Performance = O( f(n) ) Note Computer scientists use the Big O notation to abbreviate for “order of magnitude.” 5

6 Big O Notation Consider linear search. The linear search algorithm compares the key with the elements in the array sequentially until the key is found or the array is exhausted. If the key is not in the array, it requires n comparisons for an array of size n. If the key is in the array, it requires n/2 comparisons on average. The algorithm’s execution time is proportional to the size of the array. If you double the size of the array, you will expect the number of comparisons to double. The algorithm grows at a linear rate. 6

7 Big O Notation Observations
The growth rate of the Linear Search algorithm has an order of magnitude of n. Using this notation, the complexity of the linear search algorithm is O(n), pronounced as “order of n” or “linear on n” 7

8 Best, Worst, and Average Cases
For the same input size, an algorithm’s execution time may vary, depending on the input. An input that results in the shortest execution time is called the best-case input and An input that results in the longest execution time is called the worst-case input. Worst-case analysis is very useful. You can show that the algorithm will never be slower than the worst-case. The analysis is generally conducted for the worst-case. 8

9 Ignoring Multiplicative Constants
The linear search algorithm requires n comparisons in the worst-case and n/2 comparisons in the average-case. Both cases require O(n) time. The multiplicative constants have no impact on growth rates. The growth rate for n/2 or 100n is the same as n, i.e., O(n) = O(n/2) = O(100n). f(n) n n / 2 100 n 100 50 10000 200 20000 2 f( xi+1 ) / f( xi ) ⟶ f(200) / f(100) f(100) / f(50) f(20000) / f(10000)

10 Ignoring Non-Dominating Terms
Consider the algorithm for finding the maximum number in an array of n elements. The Big O notation allows you to ignore the non-dominating parts of the function and highlight its most important component. For example consider the case: f(n) = 4n + 2; You should discard the additive and multiplicative factors so f(n)≈n Therefore, the complexity of this algorithm is O(n). 10

11 Ignoring Non-Dominating Terms
Consider the algorithm for finding the maximum number in an array of n elements. int largest = a[0]; int i = 1; while ( i < a.length ) { if ( a[i] > largest ) { largest = a[i]; } i++; Max number of statements executed in this fragment is: * N ⟶ f(n) = 4n ⟶ O(n) Therefore if the array’s length is n n = 1000 f(n) = 4002 n = 2000 f(n) = 8002 n = 3000 f(n) = 12002 n = 4000 f(n) = 16002 ... Complexity of this algorithm is O(n). 11

12 Useful Mathematical Summations
12

13 Examples: Determining Big-O
Patterns to look for Repetition Sequence Selection Logarithm 13

14 Repetition: Simple Loops
for (i = 1; i <= n; i++) { k = k + 5; } executed n times constant time Time Complexity T(n) = (a constant c) * n = cn = O(n) Ignore multiplicative constants (e.g., “c”). 14

15 Repetition: Nested Loops
for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { k = k + i + j; } executed n times inner loop executed n times constant time Time Complexity T(n) = (a constant c) * n * n = cn2 = O(n2) Ignore multiplicative constants (e.g., “c”). 15

16 Repetition: Nested Loops
for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { k = k + i + j; } executed n times inner loop executed i times constant time Time Complexity T(n) = c + 2c + 3c + 4c + … + nc = c n(n+1)/2 = (c/2)n2 + (c/2)n = O(n2) quadratic algorithm Ignore non-dominating terms Ignore multiplicative constants 16

17 Repetition: Nested Loops
for (i = 1; i <= n; i++) { for (j = 1; j <= 20; j++) { k = k + i + j; } executed n times inner loop executed 20 times constant time Time Complexity T(n) = 20 * c * n = O(n) Ignore multiplicative constants (e.g., 20*c) 17

18 Sequence executed 10 times executed inner loop n times executed
for (j = 1; j <= 10; j++) { k = k + 4; } executed 10 times for (i = 1; i <= n; i++) { for (j = 1; j <= 20; j++) { k = k + i + j; } executed n times inner loop executed 20 times Time Complexity T(n) = c * * c * n = O(n) 18

19 Selection O(n) Time Complexity
if (list.contains(e)) { System.out.println(e); } else for (Object t: list) { System.out.println(t); Constant c Let n be list.size(). Loop : O(n) Time Complexity T(n) = test time (condition) + worst-case (then/ else) = O(n) + O(n) = O(n) 19

20 Constant Time The Big O notation estimates the execution time of an algorithm in relation to the input size. If the time is not related to the input size, the algorithm is said to take constant time with the notation O(1). Example: A method that retrieves an element at a given index in an array takes constant time, because it does not grow as the size of the array increases. 1 i n-1 Data [i] ⟵ Extract From ( Array_Memory_Loc + (i-1)*cellSize ) 20

21 Logarithm: Analyzing Binary Search
Each iteration in the Binary Search algorithm contains a fixed number of operations, denoted by c (see next listing) Continue on Lower Half or Continue on Upper Half 1 2 3 n/2 n-2 n-1 n low middle high 21

22 Logarithm: Analyzing Binary Search
Let T(n) denote the time complexity for a binary search on a list of n elements. Without loss of generality, assume n is a power of 2 ( n = 2k ) and k=log(n). Since binary search eliminates half of the input after two comparisons we have… 22

23 Logarithm: Analyzing Binary Search
public class BinarySearch { /** Use binary search to find a key in the list */ public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; } return -low - 1; // Now high < low 23

24 Logarithmic Time Ignoring constants and smaller terms, the complexity of the binary search algorithm is O( log(n) ). It is called a logarithmic algorithm. Note The base of the log is 2, but the base does not affect a logarithmic growth rate, so it can be omitted. The logarithmic algorithm grows slowly as the problem size increases. If you square the input size, you only double the time for the algorithm. 24

25 President Obama & the Bubble Sort

26 Analyzing Selection Sort
SelectionSort (see next listing), finds the largest number in the list and places it last. It then finds the largest number remaining and places it next to last, and so on until the list contains only a single number. 4. Exclude last, continue exploration on remaining elements 1 2 3 n-2 n-1 n 1. Find Largest element Place largest element here 3. Exchange if necessary temp 26

27 Analyzing Selection Sort
The number of comparisons is n-1 for the first iteration, n-2 for the second iteration, and so on. Let T(n) denote the complexity for selection sort and c denote the total number of other operations such as assignments and additional comparisons in each iteration. Ignoring constants and smaller terms, the complexity of the selection sort algorithm is O(n2). 27

28 Analyzing Selection Sort
public class SelectionSort { public static void selectionSort(double[] list) { for (int i = 0; i < list.length - 1; i++) { // Find the minimum in the list[i..list.length-1] double currentMin = list[i]; int currentMinIndex = i; for (int j = i + 1; j < list.length; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; } // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin;

29 Quadratic Time An algorithm with the O(n2) time complexity is called a quadratic algorithm. A quadratic algorithm grows quickly as the problem size increases. If you double the input size, the time for the algorithm is quadrupled. Algorithms with a double-nested loop are often quadratic. n2 Quadratic n Linear 29

30 Analyzing Insertion Sort
The insertion sort algorithm (see next listing) sorts a list of values by repeatedly inserting a new element into a sorted partial array until the whole array is sorted. Add 10 10 Add 30 30 Add 25 25 Add 7 7 2 times each?. At step k you ask k-1 questions and make k-1 shifts therefore 2(k-1) expensive ops are performed plus additional c work. 30

31 Analyzing Insertion Sort
At the kth iteration, to insert an element into an array of size k, it may take k comparisons to find the insertion position, and k moves to insert the element. Let T(n) denote the complexity for insertion sort and c denote the total number of other operations such as assignments and additional comparisons in each iteration. So, 2 times each?. At step k you ask k-1 questions and make k-1 shifts therefore 2(k-1) expensive ops are performed plus additional c work. 31

32 Analyzing Insertion Sort
public static void insertionSort(double[] list) { for (int i = 1; i < list.length; i++) { // insert list[i] into a sorted sublist list[0..i-1] // so that list[0..i] is sorted. double currentElement = list[i]; int k; for (k = i - 1; k >= 0 && list[k] > currentElement; k--) { list[k + 1] = list[k]; } // Insert the current element into list[k+1] list[k + 1] = currentElement; 32

33 Comparing Common Growth Functions
Constant time Logarithmic time Linear time Log-linear time Quadratic time Cubic time Exponential time 33

34 Case Study: Fibonacci Numbers
public static long fib(long index) { if (index == 0) // Base case return 0; else if (index == 1) // Base case return 1; else // Reduction and recursive calls return fib(index - 1) + fib(index - 2); } Finonacci series: … indices: fib(0) = 0; fib(n) fib(1) = 1; fib(n) = fib(n -1) + fib(n -2); n >=2 34

35 Complexity for Recursive Fibonacci Numbers
Since and Therefore, the recursive Fibonacci method takes 35

36 Case Study: Non-recursive version of Fibonacci Numbers
public static long fib(long n) { long f0 = 0; // For fib(0) long f1 = 1; // For fib(1) long f2 = 1; // For fib(2) if (n == 0) return f0; else if (n == 1) return f1; else if (n == 2) return f2; for (int i = 3; i <= n; i++) { f0 = f1; f1 = f2; f2 = f0 + f1; } Obviously, the complexity of this new algorithm is O(n) This is a tremendous improvement over the recursive algorithm. 36

37 Practical Considerations
The big O notation provides a good theoretical estimate of algorithm efficiency. However, two algorithms of the same time complexity are not necessarily equally efficient. As shown in the preceding example, both gcd algorithms Version_1 and Version_2B have the same complexity, but the one in Version_2B is obviously better practically. 37


Download ppt "IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos"

Similar presentations


Ads by Google