25.4. Serving Multiple Clients

 
[Page 746 ( continued )]

23.2. Estimating Algorithm Efficiency

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 approach:

  1. First, there are many tasks running concurrently on a computer. The execution time of a particular program is dependent on the system load.

  2. 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.

It is very difficult 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 approximates the effect of a change on the size of the input. 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 .

23.2.1. 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. The growth rate has an order of magnitude of n . Computer scientists use the Big O notation to abbreviate for "order of magnitude." Using this notation, the complexity of the linear search algorithm is O ( n ), pronounced as " order of n ."

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. Best-case and worst-case are not representative, but worst-case analysis is very useful. You can show that the algorithm will never be slower than the worst-case. An average-case analysis attempts to determine the average amount of time among all possible input of the same size. Average-case analysis is ideal, but difficult to perform, because it is hard to determine the relative probabilities and distributions of various input instances for many problems. Worst-case analysis is easier to obtain and is thus common. So, the analysis is generally conducted for the worst-case.


[Page 747]

The linear search algorithm requires n comparisons in the worst-case and n /2 comparisons in the average-case. Using the Big O notation, both cases require O ( n ) time. The multiplicative constant (1/2) can be omitted. Algorithm analysis is focused on growth rate. The multiplicative constants have no impact on growth rates. The growth rate for n /2 or 100 n is the same as n , i.e., O ( n ) = O ( n/2 ) = O (100 n ).

Consider the algorithm for finding the maximum number in an array of n elements. If n is 2, it takes one comparison to find the maximum number. If n is 3, it takes two comparisons to find the maximum number. In general, it takes n - 1 times of comparisons to find the maximum number in a list of n elements. Algorithm analysis is for large input size . If the input size is small, there is no significance to estimate an algorithm's efficiency. As n grows larger, the n part in the expression n - 1 dominates the complexity. The Big O notation allows you to ignore the non-dominating part (e.g., -1 in the expression n - 1) and highlight the important part (e.g., n in the expression n - 1). So, the complexity of this algorithm is O ( n ).

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). For 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.

23.2.2. Analyzing Binary Search

The binary search algorithm presented in Listing 6.7, BinarySearch.java, searches a key in a sorted array. Each iteration in the algorithm contains a fixed number of operations, denoted by c . 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 and k = log n . Since binary search eliminates half of the input after two comparisons,


Ignoring constants and smaller terms, the complexity of the binary search algorithm is O (log n ). An algorithm with the O (log n ) time complexity is called a logarithmic algorithm . 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, the time for the algorithm is doubled .

23.2.3. Analyzing Selection Sort

The selection sort algorithm presented in Listing 6.8, SelectionSort.java, 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. 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. So,


Ignoring constants and smaller terms, the complexity of the selection sort algorithm is O ( n 2 ).


[Page 748]

An algorithm with the O ( n 2 ) time complexity is called a quadratic algorithm . The quadratic algorithm grows quickly as the problem size increases. If you double the input size, the time for the algorithm is quadrupled. Algorithms with two nested loops are often quadratic.

23.2.4. Analyzing Insertion Sort

The insertion sort algorithm presented in Listing 6.9, InsertionSort.java, sorts a list of values by repeatedly inserting a new element into a sorted partial array until the whole array is sorted. At the k th iteration, to insert an element to a array of size k , it may take k comparisons to find the insertion position, and k moves to insert the element. So, the total number of operations is 2 k . 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,

T ( n ) = 2 + c + 2 x 2 + c ... + 2 x ( n - 1) + c = n 2 - n + c ( n - 1)

Ignoring constants and smaller terms, the complexity of the insertion sort algorithm is O ( n 2 ).

23.2.5. Analyzing Towers of Hanoi

The Towers of Hanoi problem presented in Listing 19.7, TowersOfHanoi.java, moves n disks from tower A to tower B with the assistance of tower C recursively as follows :

  1. Move the first n - 1 disks from A to C with the assistance of tower B.

  2. Move disk n from A to B.

  3. Move n - 1 disks from C to B with the assistance of tower A.

Let T ( n ) denote the complexity for the algorithm that moves n disks and c denote the constant time to move one disk, i.e., T (1) is c . So,


An algorithm with the O ( c n ) time complexity is called an exponential algorithm . As the input size increases, the time for the exponential algorithm grows exponentially. The exponential algorithms are not practical for large input size.

23.2.6. Comparing Common Growth Functions

The preceding sections analyzed the complexity of several algorithms. Table 23.1 lists some common growth functions. These functions are ordered as follows:

O (1) < O (log n ) < O ( n ) < O ( n log n ) < O ( n 2 ) < O ( n 3 ) < O (2 n )

Table 23.1. Common Growth Functions
Big-O Function Name
O (1) Constant time
O (log n ) Logarithmic time
O ( n ) Linear time
O ( n log n ) log-linear time
O ( n 2 ) Quadratic time
O ( n 3 ) Cubic time
O (2 n ) Exponential time


[Page 749]

Table 23.2 shows how growth rates change as the input size doubles from to n = 25 to n = 50.

Table 23.2. Change of Growth Rates
Function n = 25 n = 50 f(50)/f(25)
O (1) 1 1 1
O (log n) 4.64 5.64 1.21
O ( n ) 25 50 2
O ( n log n ) 116 282 2.431
O ( n 2 ) 625 2500 4
O ( n 3 ) 15625 125000 8
O (2 n ) 3.36 x 10 7 1.27 x 10 15 3.35 x 10 7

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net