Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
An algorithm must be correct. It should correctly solve the problem. Once we have a correct algorithm for a problem, we have to determine the efficiency of that algorithm.
how much time that algorithm requires. how much space that algorithm requires.
We will focus:
How to estimate the time required for an algorithm How to reduce the time required
Analysis of Algorithms
We can have different algorithms to solve a given problem. Which one is the most efficient? Between the given two algorithms, which one is more efficient? Analysis of Algorithms is the area of computer science that provides tools to analyze the efficiency of different methods of solutions. The efficiency of an algorithm means
How much time it requires. How much memory space it requires. How much disk space and other resources it requires. We will concentrate on the time requirement.
We will try to find the efficiency of the algorithms, not their implementations. An analysis should focus on gross differences in the efficiency of algorithms that are likely to dominate the overall cost of a solution.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 2
Cost: c1 Cost: c2
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*c5 The time required for this algorithm is proportional to n
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 7
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*(n+1)*c5+n*n*c6+n*n*c7+n*c8 The time required for this algorithm is proportional to n2
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 8
The most important thing to learn is how quickly the algorithms time requirement grows a function of the problem size.
Algorithm A requires time proportional to n2. Algorithm A requires time proportional to n.
An algorithms proportional time requirement is known as growth rate. We can compare the efficiency of two algorithms by comparing their growth rates.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 10
11
12
13
Order of an Algorithm
If an algorithm requires n23*n+10 seconds to solve a problem size n. If constants k and n0 exist such that k*n2 > n23*n+10 for all n u n0 . the algorithm is order n2 (In fact, k is 3 and n0 is 2) 3*n2 > n23*n+10 for all n u 2 . Thus, the algorithm requires no more than k*n2 time units for n u n0 , So it is O(n2)
14
15
16
17
Growth-Rate Functions
O(1) Time requirement is constant, and it is independent of the problems size. O(log2n) Time requirement for a logarithmic algorithm increases increases slowly as the problem size increases. O(n) Time requirement for a linear algorithm increases directly with the size of the problem. O(n*log2n) Time requirement for a n*log2n algorithm increases more rapidly than a linear algorithm. O(n2) Time requirement for a quadratic algorithm increases rapidly with the size of the problem. O(n3) Time requirement for a cubic algorithm increases more rapidly with the size of the problem than the time requirement for a quadratic algorithm. O(2n) As the size of the problem increases, the time requirement for an exponential algorithm increases too rapidly to be practical.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 18
Growth-Rate Functions
If an algorithm takes 1 second to run with the problem size 8, what is the time requirement (approximately) for that algorithm with the problem size 16? If its order is: O(1) T(n) = 1 second O(log2n) T(n) = (1*log216) / log28 = 4/3 seconds O(n) T(n) = (1*16) / 8 = 2 seconds O(n*log2n) T(n) = (1*16*log216) / 8*log28 = 8/3 seconds O(n2) T(n) = (1*162) / 82 = 4 seconds O(n3) T(n) = (1*163) / 83 = 8 seconds O(2n) T(n) = (1*216) / 28 = 28 seconds = 256 seconds
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 19
2. We can ignore a multiplicative constant in the higher-order term of an algorithms growth-rate function.
If an algorithm is O(5n3), it is also O(n3).
n * ( n 1) * ( 2n 1) n 3 i 2 ! 1 4 ... n 2 ! } 6 3 i !1
n
2i ! 0 1 2 ... 2 n1 ! 2 n 1
i !0
n 1
21
= c1 + c2 + (n+1)*c3 + n*c4 + n*c5 = (c3+c4+c5)*n + (c1+c2+c3) = a*n + b So, the growth-rate function for this algorithm is O(n)
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 22
Cost c1 c2 c3 c4 c5 c6 c7
23
Times
n+1
n
c1 c2 c3 c4
( j 1)
j !1 j n
(k 1)
j !1 k !1 j n
k
j !1 k !1
n j
T(n)
= c1*(n+1) + c2*(
( j 1)
j !1
) + c3* (
(k 1)
j !1 k !1
) + c4*(
k
j !1 k !1
= a*n3 + b*n2 + c*n + d So, the growth-rate function for this algorithm is O(n3)
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 24
The time-complexity function T(n) of a recursive algorithm is defined in terms of itself, and this is known as recurrence equation for T(n). To find the growth-rate function for that recursive algorithm, we have to solve that recurrence relation.
25
(assuming n>2)
2
i !0
27
What to Analyze
An algorithm can require different times to solve different problems of the same size.
Ex. Searching an item in a list of n elements using sequential search. Cost: 1,2,...,n
Worst-Case Analysis The maximum amount of time that an algorithm require to solve a problem of size n.
This gives an upper bound for the time complexity of an algorithm. Normally, we try to find worst-case behavior of an algorithm.
Best-Case Analysis The minimum amount of time that an algorithm require to solve a problem of size n.
The best case behavior of an algorithm is NOT so useful.
Average-Case Analysis The average amount of time that an algorithm require to solve a problem of size n.
Sometimes, it is difficult to find the average-case behavior of an algorithm. We have to look at all possible data organizations of a given size n, and their distribution probabilities of these organizations. Worst-case analysis is more common than average-case analysis.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 28
What is Important?
An array-based list retrieve operation is O(1), a linked-list-based list retrieve operation is O(n).
But insert and delete operations are much easier on a linked-list-based list implementation.
If the problem size is always small, we can probably ignore an algorithms efficiency.
In this case, we should choose the simplest algorithm.
We have to weigh the trade-offs between an algorithms time requirement and its memory requirements. We have to compare algorithms for both style and efficiency.
The analysis should focus on gross differences in efficiency and not reward coding tricks that save small amount of time. That is, there is no need for coding tricks if the gain is not too much. Easily understandable program is also important.
Sequential Search
int sequentialSearch(int a[], int item, int n){ for (int i = 0; i < n && a[i]!= item; i++); if (i == n) return 1; return i; }
Unsuccessful Search:
O(n)
Successful Search: Best-Case: item is in the first location of the array O(1) Worst-Case: item is in the last location of the array O(n) Average-Case: The number of key comparisons 1, 2, ..., n
n
i
i !1
( n 2 n) / 2 ! n n
O(n)
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 30
Binary Search
int binarySearch(int a[], int size, int x) { int low =0; int high = size 1; int mid; // mid will be the index of // target when its found. while (low <= high) { mid = (low + high)/2; if (a[mid] < x) low = mid + 1; else if (a[mid] > x) high = mid 1; else return mid; } return 1; }
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 31
34
Sorting
Sorting is a process that organizes a collection of data into either ascending or descending order. An internal sort requires that the collection of data fit entirely in the computers main memory. We can use an external sort when the collection of data cannot fit in the computers main memory all at once but must reside in secondary storage such as on a disk. We will analyze only internal sorting algorithms. Any significant amount of computer output is generally arranged in some sorted order so that it can be interpreted. Sorting also has indirect uses. An initial sort of the data can significantly enhance the performance of an algorithm. Majority of programming projects use a sort somewhere, and in many cases, the sorting cost determines the running time. A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 35
Sorting Algorithms
There are many sorting algorithms, such as: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort First three sorting algorithms are not so efficient, but last two are efficient sorting algorithms.
36
Selection Sort
The list is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. We find the biggest element from the unsorted sublist and swap it with the element at the end of the unsorted data. After each selection and swapping, the imaginary wall between the two sublists move one element back, increasing the number of sorted elements and decreasing the number of unsorted ones. Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. A list of n elements requires n-1 passes to completely rearrange the data.
37
Unsorted
Sorted
38
In selectionSort function, the for loop executes n-1 times. In indexOfLargest function, the for loop executes the size of the unsorted part minus 1 (from n-1 to 1), and each iteration we make one key comparison. # of key comparisons = 1+2+...+n-1 = n*(n-1)/2 So, Selection sort is O(n2) The best case, the worst case, and the average case of the selection sort algorithm are same. all of them are O(n2)
This means that the behavior of the selection sort algorithm does not depend on the initial organization of data. Since O(n2) grows so rapidly, the selection sort algorithm is appropriate only for small n.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 40
Insertion Sort
Insertion sort is a simple sorting algorithm that is appropriate for small inputs.
Most common sorting technique used by card players.
The list is divided into two parts: sorted and unsorted. In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. A list of n elements will take at most n-1 passes to sort the data.
41
Unsorted
8 8 8 78 45 45 32 32 32 32 78 56 56 56 56 56 56 78 Original List
After pass 1
After pass 2
After pass 3
43
Worst-case:
O(n2)
O(n2)
Array is in reverse order: Inner loop is executed p-1 times, for p = 2,3, , n The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2
Average-case:
O(n2)
Bubble Sort
The list is divided into two sublists: sorted and unsorted. The largest element is bubbled from the unsorted list and moved to the sorted sublist. After that, the wall moves one element back, increasing the number of sorted elements and decreasing the number of unsorted ones. Each time an element moves from the unsorted part to the sorted part one sort pass is completed. Given a list of n elements, bubble sort requires up to n-1 passes (maximum passes) to sort the data.
46
47
Worst-case:
O(n2)
O(n2)
Array is in reverse order: Inner loop is executed n-1 times, The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2
Average-case:
O(n2)
49
Mergesort
Mergesort algorithm is one of two important divide-and-conquer sorting algorithms (the other one is quicksort). It is a recursive algorithm. Divides the list into halves, Sort each halve separately, and Then merge the sorted halves into one sorted array.
50
Mergesort - Example
51
Merge
const int MAX_SIZE = maximum-number-of-items-in-array; void merge(int theArray[], int first, int mid, int last) { int tempArray[MAX_SIZE]; // temporary array int first1 = first; // beginning of first subarray int last1 = mid; // end of first subarray int first2 = mid + 1; // beginning of second subarray int last2 = last; // end of second subarray int index = first1; // next available location in tempArray for ( ; (first1 <= last1) && (first2 <= last2); ++index) { if (theArray[first1] < theArray[first2]) { tempArray[index] = theArray[first1]; ++first1; } else { tempArray[index] = theArray[first2]; ++first2; } }
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 52
Merge (cont.)
// finish off the first subarray, if necessary for (; first1 <= last1; ++first1, ++index) tempArray[index] = theArray[first1]; // finish off the second subarray, if necessary for (; first2 <= last2; ++first2, ++index) tempArray[index] = theArray[first2]; // copy the result back into the original array for (index = first; index <= last; ++index) theArray[index] = tempArray[index]; // end merge
53
Mergesort
void mergesort(int theArray[], int first, int last) { if (first < last) { int mid = (first + last)/2; // index of midpoint mergesort(theArray, first, mid); mergesort(theArray, mid+1, last); // merge the two halves merge(theArray, first, mid, last); } } // end mergesort
54
Mergesort - Example
6 3 9 1 5 4 7 2
divide
6 3 9 1
divide
5 4 7 2
divide
6 3
divide
9 1
divide
5 4
divide
7 2
divide
6
merge
3 3 6
9
merge merge
1 1 9
5
merge
4 4 5
7
merge
2 2 7
merge
1 3 6 9
merge
2 4 5 7
1 2 3 4 5 6 7 9
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 55
Mergesort Example2
56
57
..........
0
..........
2k-1
..........
Best-case:
All the elements in the first array are smaller (or larger) than all the elements in the second array. The number of key comparisons: k
Worst-case:
The number of key comparisons: 2k-1
58
Mergesort - Analysis
Levels of recursive calls to mergesort, given an array of eight items
59
Mergesort - Analysis
2m 2m-1 2m-2 2m-2 2m-2 2m-1 2m-2
level 0 : 1 merge (size 2m-1) level 1 : 2 merges (size 2m-2) level 2 : 4 merges (size 2m-3)
. . .
. . .
20
.................
20 level m
60
Mergesort - Analysis
Worst-case The number of key comparisons: = 20*(2*2m-1-1) + 21*(2*2m-2-1) + ... + 2m-1*(2*20-1) = (2m - 1) + (2m - 2) + ... + (2m 2m-1) = m*2m 2i
i !0 m 1
( m terms )
= 6 different cases
Mergesort Analysis
Mergesort is extremely efficient algorithm with respect to time. Both worst case and average cases are O (n * log2n ) But, mergesort requires an extra array whose size equals to the size of the original array. If we use a linked list, we do not need an extra array
But, we need space for the links And, it will be difficult to divide the list into half ( O(n) )
63
Quicksort
Like mergesort, Quicksort is also based on the divide-and-conquer paradigm. But it uses this technique in a somewhat opposite manner, as all the hard work is done before the recursive calls. It works 1. First, it partitions an array into two parts, 2. Then, it sorts the parts independently, 3. Finally, it combines the sorted subsequences by a simple concatenation.
64
Quicksort (cont.)
The quick-sort algorithm consists of the following three steps: 1. Divide: Partition the list. To partition the list, we first choose some element from the list for which we hope about half the elements will come before and half after. Call this element the pivot. Then we partition the elements so that all those with values less than the pivot come in one sublist and all those with greater values come in another. 2. Recursion: Recursively sort the sublists separately. 3. Conquer: Put the sorted sublists together.
65
Partition
Partitioning places the pivot in its correct place position within the array.
Arranging the array elements around the pivot p generates two smaller sorting problems.
sort the left section of the array, and sort the right section of the array. when these two smaller sorting problems are solved recursively, our bigger sorting problem is solved.
66
67
Partition Function
void partition(int theArray[], int first, int last, int &pivotIndex) { // Partitions an array for quicksort. // Precondition: theArray[first..last] is an array; first <= last. // Postcondition: Partitions theArray[first..last] such that: // S1 = theArray[first..pivotIndex-1] < pivot // theArray[pivotIndex] == pivot // S2 = theArray[pivotIndex+1..last] >= pivot // Calls: choosePivot and swap. // place pivot in theArray[first] choosePivot(theArray, first, last); DataType pivot = theArray[first]; // copy pivot
68
70
71
72
73
74
Quicksort Function
void quicksort(int theArray[], int first, int last) { // Sorts the items in an array into ascending order. // Precondition: theArray[first..last] is an array. // Postcondition: theArray[first..last] is sorted. // Calls: partition. int pivotIndex; if (first < last) { // create the partition: S1, pivot, S2 partition(theArray, first, last, pivotIndex); // sort regions S1 and S2 quicksort(theArray, first, pivotIndex-1); quicksort(theArray, pivotIndex+1, last); } }
75
Quicksort Analysis
Worst Case: (assume that we are selecting the first element as pivot)
The pivot divides the list of size n into two sublists of sizes 0 and n-1. The number of key comparisons = n-1 + n-2 + ... + 1 = n2/2 n/2 O(n2) The number of swaps = = n-1 + n-1 + n-2 + ... + 1 swaps outside of the for loop = n2/2 + n/2 - 1 O(n2) swaps inside of the for loop
76
Quicksort Analysis
Quicksort is O(n*log2n) in the best case and average case. Quicksort is slow when the array is sorted and we choose the first element as the pivot. Although the worst case behavior is not so good, and its average case behavior is much better than its worst case.
So, Quicksort is one of best sorting algorithms using key comparisons.
77
Quicksort Analysis
A worst-case partitioning with quicksort
78
Quicksort Analysis
An average-case partitioning with quicksort
79
Radix Sort
Radix sort algorithm different than other sorting algorithms that we talked. It does not use key comparisons to sort an array. The radix sort : Treats each data item as a character string. First it groups data items according to their rightmost character, and put these groups into order wrt this rightmost character. Then, combine these groups. We, repeat these grouping and combining operations for all other character positions in the data items from the rightmost to the leftmost character position. At the end, the sort operation will be completed.
BIL741: Advanced Analysis of Algorithms I ( leri Algoritma zmleme I) 80
81
82
84
85