Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
based on some easy-to-understand searching and sorting algorithms that we may have seen earlier.
1
Thursday, September 15, 11
Searching Problem
Assume A is an array with n elements A[1], A[2], A[n]. For a given element x, we must determine whether there is an index j; 1 j n, such that x = A[j] Two algorithms, among others, address this problem
Linear Search Binary Search
2
Thursday, September 15, 11
Binary Search
We can do better than linear search if we knew that the elements of A are sorted, say in nondecreasing order. The idea is that you can compare x to the middle element of A, say A[middle].
If x < A[middle] then you know that x cannot be an element from A[middle+1], A[middle+2], A[n]. Why? If x > A[middle] then you know that x cannot be an element from A[1], A[2], A[middle-1]. Why?
5
Thursday, September 15, 11
Theorem
The number of comparisons performed by Algorithm BINARYSEARCH on a sorted array of size n is at most
8
Thursday, September 15, 11
Input 3 7 9 12 1 2 4 13 14
Output
9
Thursday, September 15, 11
Input 3 7 9 12 1 2 4 13 14
Output 1
Thursday, September 15, 11
12 13 149
We will be interested in merging two subarrays. Input: A[1..n], p, q, r. Merge A[p..q] with A[q+1..r].
10
Thursday, September 15, 11
245
3 78
10
Thursday, September 15, 11
245 2 245
3 78 378
10
Thursday, September 15, 11
3 78 378 378
10
Thursday, September 15, 11
10
Thursday, September 15, 11
10
Thursday, September 15, 11
245 2 23 23 4 23 4 5 23 4 5 78
Thursday, September 15, 11
Algorithm MERGE
Algorithm: MERGE Input: An array A[1..m] of elements and three indices p, q and r, with 1 p q <r m, such that both the subarrays A[p..q] and A[q + 1..r] are sorted individually in nondecreasing order. Output: A[p..r] contains the result of merging the two subarrays A[p..q] and A[q + 1..r]. Comment: B[p..r] is an auxiliary array.
11
Thursday, September 15, 11
with A[q+1..r]
12
Thursday, September 15, 11
Analyzing MERGE
Assuming arrays A[p,q] and A[q+1,r], where p =1 and r =n,
n element assignments are needed to copy A to temp. n element assignments are needed to copy temp to A. The total number of element assignments is 2n. Hence, the time complexity is O(n).
13
Thursday, September 15, 11
Selection Sort
Algorithm: SELECTIONSORT Input: An array A[1..n] of n elements. Output: A[1..n] sorted in nondecreasing order. 1. for i 1 to n - 1 2. k i 3. for j i + 1 to n
{Find the index of the ith smallest element}
4. if A[j] < A[k] then k j 5. end for 6. if k i then interchange A[i] and A[k] 7. end for
14
Thursday, September 15, 11
15
Thursday, September 15, 11
15
Thursday, September 15, 11
15
Thursday, September 15, 11
15
Thursday, September 15, 11
15
Thursday, September 15, 11
15
Thursday, September 15, 11
15
Thursday, September 15, 11
k 2 5
5 2 2
2 9 8 5 9 8 4 9 8
4 4 5
5 2 2
2 9 8 5 9 8 4 9 8
4 4 5
k 2 5 5
5 2 2 2
2 9 8 5 9 8 4 9 8 4 5 8
4 4 5 9
15
Insertion Sort
Algorithm: INSERTIONSORT Input: An array A[1..n] of n elements. Output: A[1..n] sorted in nondecreasing order. 1. for i 2 to n 2. x A[i] 3. j i - 1 4. while (j > 0) and (A[j] > x) 5. A[j + 1] A[j] 6. j j - 1 7. end while 8. A[j + 1] x 9. end for
17
Thursday, September 15, 11
18
Thursday, September 15, 11
18
Thursday, September 15, 11
2 5
18
Thursday, September 15, 11
2 5
18
Thursday, September 15, 11
2 5
18
Thursday, September 15, 11
2 5 2 5
18
Thursday, September 15, 11
2 5 2 5
18
Thursday, September 15, 11
2 5 2 5
2 5 2 5
2 5 2 5 2 5
2 5 2 5 2 5
2 5 2 5 2 5
2 5 2 5 2 5
2 5 2 5 2 5
18
4 4 4 4 9
18
2 5 2 5 2 5
8 8 8 9 8
4 4 4 4 9
18
2 5 2 5 2 5
2 5 2 5 2 5 2
2 5 2 5 2 5 2 4
2 9 8
4 12 7
3 6 10
21
5 5
2 2 9 8 4 12 7 1 3 6 10
21
5 5
8 4 12 7 1 3 6 10
21
2 9 8
5 5
4 12 4 12 7 1 3 6 10
21
2 9 8
5 5
4 12 4 12 7
7 1
1 3 6 10
21
2 9 8
5 5
4 12 4 12 7
7 1
2 9 8
3 6 10
21
5 5
4 12 4 12 7
7 1
10
2 9 8
3 6 10
21
2 5 5 5 2
8 9
9 8
4 12 4 12 4 12 7
1 7 7 1 1
3 6 3 6
10 10
2 9 8
3 6 10
21
2 5 2 5 5 5 2
8 8 9
9 9 8 4 12 4 12 4 12 7 1 7 7 1 1 3 6 3 6 10 10
2 9 8
3 6 10
21
2 5 2 5 5 5 2
8 8 9
9 9 8
4 7 12 1 7 7 1 1 3 6 3 6 10 10
4 12 4 12 4 12 7
2 9 8
3 6 10
21
2 5 2 5 5 5 2
8 8 9
9 9 8
4 7 12 1 7 7 1 1
3 6 10 3 6 3 6 10 10
4 12 4 12 4 12 7
2 9 8
3 6 10
21
4 12 4 12 4 12 7
2 9 8
3 6 10
21
4 12 4 12 4 12 7
2 9 8
3 6 10
21
4 12 4 12 4 12 7
2 9 8
3 6 10
21
Algorithm BOTTOMUPSORT
Algorithm: BOTTOMUPSORT Input: An array A[1..n] of n elements. Output: A[1..n] sorted in nondecreasing order. 1. t 1 2. while t < n 3. s t; t 2s; i 0 4. while i + t n 5. MERGE(A, i + 1, i + s, i + t) 6. i i + t 7. end while 8. if i + s < n then 9. MERGE(A, i + 1, i+ s, n) 10. end while
22
Thursday, September 15, 11
Analyzing Algorithm
With no loss of generality, assume the size of the array is a power of 2.
n In the first iteration, we have pairs that are merged using 2 n * 2 * 2 = 2n element assignments. 2
In the first iteration, we have
n * 4 * 2 = 2n assignments. 4
.
n 4
In the last iteration, we have 1 pair that are merged using element
n 2 * 2 * = 2n 2
Thursday, September 15, 11
assignments.
23
24
Thursday, September 15, 11
Time Complexity
One way of measuring the performance of an algorithm is how fast it executes. The question is how to measure this time?
Is having a digital stop watch suitable?
In general, we are not so much interested in the time and space complexity for small inputs. For example, while the difference in time complexity between linear and binary search is meaningless for a sequence with n = 10, it is gigantic for n = 230.
25
Thursday, September 15, 11
Complexity
For example, let us assume two algorithms A and B that solve the same class of problems. The time complexity of A is 5,000n, the one for B is 1.1n for an input with n elements. For n = 10, A requires 50,000 steps, but B only 3, so B seems to be superior to A. For n = 1000, however, A requires 5,000,000 steps, while B requires 2.51041 steps.
26
Thursday, September 15, 11
Complexity
Comparison: time complexities of algorithms A and B
Order of Growth
This means that algorithm B cannot be used for large inputs, while algorithm A is still feasible. So what is important is the growth of the complexity functions. The growth of time and space complexity with increasing input size n is a suitable measure for the comparison of algorithms. we focus on asymptotic analysis
28
Thursday, September 15, 11
Example
29
Example
Growth rate for same previous functions showing larger input sizes
30
Thursday, September 15, 11
31
Thursday, September 15, 11
n n2 n3 n5 2n 3n log2 n
10 110-5 sec 0.0001 sec 0.001 sec 0.1 sec 0.001sec 0.59sec
20 210-5 sec 0.0004 sec 0.008 sec 3.2 sec 1.0 sec 58 min
30 310-5 sec 0.0009 sec 0.027 sec 24.3 sec 17.9 min 6.5 years
40 410-5 sec 0.016 sec 0.064 sec 1.7 min 12.7 days 3855 cent
50 510-5 sec 0.025 sec 0.125 sec 5.2 min 35.7 years 2108cent
60 610-5 sec 0.036 sec 0.216 sec 13.0 min 366 cent 1.31013cent
310-6 sec 410-6 sec 510-6 sec 510-6 sec 610-6 sec 610-6 sec
n log2 n 310-5 sec 910-5 sec 0.0001 sec 0.0002 sec 0.0003 sec 0.0004 sec
31
Thursday, September 15, 11
32
Thursday, September 15, 11
35
Thursday, September 15, 11
38
Thursday, September 15, 11
f (n) = O(g(n))
f (n) = (g(n)) f (n) = (g(n))
41
Summary
f (n) = O(g(n)) if
f (n) lim n g(n)
f (n) = O(g(n)) and f (n) = (g(n)) if f (n) = (g(n)) f (n) = O(g(n)) and f (n) = (g(n)) if g(n) = O( f (n))
42
Thursday, September 15, 11
Examples 1
Hence,
It follows that
44
Thursday, September 15, 11
Examples 2
Hence,
It follows that
45
Thursday, September 15, 11
Examples 3
46
Thursday, September 15, 11
Example 4
Prove that We only need to show that the two functions are not related by O or
Hence, Or
which means
Example 5
Show that log(n!) is in (n log n).
39
Thursday, September 15, 11
54
Thursday, September 15, 11
16
Thursday, September 15, 11
16
Thursday, September 15, 11
16
Thursday, September 15, 11
54
Thursday, September 15, 11
Example 1.23
Consider Algorithm count2, which con loops and a variable count which counts the number of itera Computing running time positive integer. the algorithm on input n, which is acontinued.. Algorithm 1.9 count2 Input: A positive integer n. Output: count = number of times Step 5 is executed. 1. 2. 3. 4. 5. 6. 7. 8. count 0 for i 1 to n m n/i for j 1 to m count count + 1 end for end for return count
51
16
Thursday, September 15, 11
16
Thursday, September 15, 11
n i
n i=0
the algorithm on input n, which is a positive integer. Algorithm 1.9 count2 Input: A positive integer n. Output: count = number of times Step 5 is executed. 1. 2. 3. 4. 5. 6. 7. 8. count 0 for i 1 to n m n/i for j 1 to m count count + 1 end for end for return count n n
n 1 =n = O(n log n). i i Theiinner for loop is executed repeatedly for the followin i=0 i=0 i=0
53
Thursday, September 15, 11
54
Thursday, September 15, 11
= (n2 )
54
Thursday, September 15, 11
= (n2 ) 6= 1
a =
an+1 1 a 1 ;a
54
Thursday, September 15, 11
= (n2 ) 6= 1
a =
an+1 1 a 1 ;a
1 n
1 j=0 2j
=2
= (1)
54
Thursday, September 15, 11
= (n2 ) 6= 1
a =
an+1 1 a 1 ;a
1 n
1 j=0 2j
=2
= (1)
Pn
Thursday, September 15, 11
1 j=1 j
ln n = (log n)
54
Space Complexity
Space complexity refers to the number of memory cells needed to carry out the computational steps required in an algorithm excluding memory cells needed to hold the input. Compare additional space needed to carry out SELECTIONSORT to that of BOTTOMUPSORT if we have an array with 2 million elements!
48
Thursday, September 15, 11
Examples
What is the space complexity for
Linear search Binary search Selection sort Insertion sort Merge (that merges two sorted lists) Bottom up merge sort
49
Thursday, September 15, 11