Beruflich Dokumente
Kultur Dokumente
CSC 3102
0.1
CSC 3102
(see Appendix A)
Find a closed-form formula for the count and/or establish its order of growth.
CSC 3102
0.3
Algorithm MaxElement(A[0.. n - 1]) //Determines the value of the largest element in a given array //Input: An array A[0.. n - 1] of real numbers //Output: The value of the largest element in A
maxval A[0] for i 1 to n - 1 do if A[i] > maxval maxval A[i] return maxval
CSC 3102
0.4
Input size = n, the number of elements in the array Algorithms basic operation is comparison It is executed on each repetition of the loop Formula for the basic operation count: Sum is simply 1 repeated by n-1 times.
n1
C(n) = 1 = n 1 (n)
i=1
CSC 3102
0.5
Algorithm UniqueElements(A[0.. n - 1]) //Checks whether all the elements in a given array are distinct //Input: An array A[0.. n - 1] //Output: Returns true if all the elements in A are distinct // and false otherwise. for i 0 to n - 2 do for j i + 1 to n - 1 do if A[i] = A[j] return false return true
CSC 3102
0.6
innermost loop
Formula for the basic operation count: Depends also on whether there are equal elements in the array and,
if there are, which positions they occupy In worst case, the algorithm needs to compare all n(n-1)/2 distinct pairs.
n2 n1
n2
n2
Cworst (n) =
CSC 3102
1 = [(n 1) (i + 1) + 1] = (n 1 i) =
i= 0 i= 0
0.7
i= 0 j= i+1
(n 1)n (n 2 ) 2
B.B. Karki, LSU
Algorithm MatrixMultiplication(A[0.. n - 1, 0.. n - 1], B[0.. n - 1, 0.. n - 1]) //Multiplies two square matrices of order n by the definition-based algorithm //Input: Two n-by-n matrices A and B //Output: Matrix C = AB for i 0 to n - 1 do for j 0 to n - 1 do C[i, j] 0.0 for k 0 to n - 1 do C[i, j] C[i, j] + A[i, k] * B[k, j] return C
CSC 3102
0.8
C(n) = 1 = n = n 2 = n 3 (n 3 )
i= 0 j= 0 k= 0 i= 0 j= 0 i= 0
T(n) cC(n) = (c a + c m )n
CSC 3102
0.9
Where ca and cm are times of one addition and one multiplication for a given machine. B.B. Karki, LSU
15 0 0 22 0 15 0 11 3 0 0 0 0 0 0 6 0 0 M = 0 0 0 0 0 0 91 0 0 0 0 0 0 0 28 0 0 0
n=6
t=8
Matrix M
row col value M[0] 6 6 8 [1] 0 0 15 [2] 0 3 22 [3] [4] [5] [6] [7] [8] 0 1 1 2 4 5 5 1 2 3 0 2 15 11 3 6 91 28
Transpose M'
row col value M [0] [1] [2] [3] [4] [5] [6] [7] [8] 6 0 0 1 2 2 3 3 5 6 0 4 1 1 5 0 2 0 8 15 91 11 3 28 22 6 15
each element in the matrix value is the actual element or item Constraint: The entries must be sorted in ascending order using the row and col indices as the primary and secondary keys, respectively.
Nave approach: Swap the row and column indices of all terms Sort the terms according to the new row index as a primary key Fast approach: Calculate the position in the list where each term would go after
the transpose Swap the row and column indices of all terms by placing them in their appropriate positions.
CSC 3102
0.11
; j++)
Variables M[0].row and M[0].col define the order of matrix M, and M[0].value gives the number of elements in the matrix with non-zero value.
CSC 3102
0.12
Basic operation: Check whether a new row (M') contains non-zero element(s): M[j].col = i The runtime complexity of the algorithm is:
n1 t n1
C(n) = 1 = t = nt (nt)
i= 0 j=1 i= 0
0.13
CSC 3102
0.14
isolated loops:
Initialize the number of terms per row in M' Count the number of terms per row in M' Scan all the non-zero elements and put them into multiple bins (rows) Determine the starting position of each new row in M' Scan all terms and decide the position of each bin (row) Create the transpose matrix M' For each term, find the correct old col number and swap the row and
col indices.
The runtime complexity of the algorithm is O(n+t) Four loops listed above are O(n), O(t), O(n) and O(t), respectively. So putting together and ignoring the constant factor, we have O(n+t) For dense matrix t n2, the complexity is of the order O(n2).
CSC 3102
0.15
Example 4: Binary
Algorithm Binary(n) //Find the number of binary digits in the binary representation of a positive decimal integer //Input: A positive decimal integer n //Output: The number of binary digits count 1 while n > 1 do count count + 1 n n/2 return count Basic operation is comparison n > 1, which decides whether the
while loop will be executed. The value of n is about halved on each repetition of the loop The complexity is log2n + 1
Number of times the comparison is executed = Number of repetitions of