









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Definitions, examples, and mathematical analysis of nonrecursive algorithms. Topics include the maximum element, unique elements, matrix operations, and sparse matrix transpose. The document also covers the efficiency analysis of nonrecursive algorithms and provides formulas for basic operation counts.
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Largest element in a list of numbers Element uniqueness problem Matrix operations: addition, multiplication, and transpose Digits in binary representation
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
C ( n ) = 1 i = 1 n − 1 ∑ =^ n^ −^1 ∈^ Θ( n )
Input size = n , the number of elements in the array Basic operation: single operation ( element comparison ) in the 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.
worst
j = i + 1 n − 1 ∑ =^ [( n^ −^1 )^ −^ ( i^ +^1 )^ +^1 ]^ =^ ( n^ −^1 −^ i ) i = 0 n − 2 ∑ i = 0 n − 2 ∑ i = 0 n − 2 ∑ =^
2
A 2D matrix is the standard representation Classic matrix multiplication: O( n 3 ) Given two n -by- n matrices A and B , compute their product C = AB using definition based algorithm. C is an n -by- n matrix whose elements are computed as the scalar (dot) products of the rows of A and the columns of B. Strassen’s algorithm based on a divide-and-conquer approach: O( n 2.81 ) 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. for k ← 0 to n - 1 do C[ i, j] ← C[ i, j] + A[ i, k] * B[ k, j] return C
k = 0 n − 1 ∑
CSC 3102 0.10 B.B. Karki, LSU
A sparse matrix (of order n) has only a small number ( t ) of nonzero elements A 2D representation is not efficient as storing the zero elements wastes space Sparse matrix representation: A set of ordered triplets < row, col, value > row and col are integers defining 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. Transpose of M is M' : M'[i].row = M[i].col M'[i].col = M[i].row row col value M [0] 6 6 8 [ 1 ] 0 0 15 [2] 0 3 22 [ 3 ] 0 5 − 15 [ 4 ] 1 1 11 [5] 1 2 3 [6] 2 3 − 6 [ 7 ] 4 0 91 [8] 5 2 28 € row col value M^ ′[0] 6 6 8 [ 1 ] 0 0 15 [2] 0 4 91 [ 3 ] 1 1 11 [ 4 ] 2 1 3 [5] 2 5 28 [6] 3 0 22 [ 7 ] 3 2 − 6 [8] 5 0 − 15 € M = 15 0 0 22 0 − 15 0 11 3 0 0 0 0 0 0 − 6 0 0 0 0 0 0 0 0 91 0 0 0 0 0 0 0 28 0 0 0 Matrix M Transpose^ M' n = 6 t = 8
Naïve 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.13 B.B. Karki, LSU
Given a matrix M of order n , find its transpose matrix M' : Inner loop over the number of non-zero elements ( t ) For each element, exchange the row and col indices (i.e, create a transpose) Outer loop over the col index of the original matrix ( n values) using it as the primary key (per row i in M') For each index i , process inner loop (i.e., process all elements and pick up their col numbers if they match with i. Basic operation: Check whether a new row ( M' ) contains non-zero element(s): M[j].col = i The runtime complexity of the algorithm is: For dense matrix, t → n^2 , the complexity is of the order O( n^3 ). € C ( n ) = 1 j = 1 t ∑ =^ t i = 0 n − 1 ∑ =^ nt^ ∈^ Ο( nt ) i = 0 n − 1 ∑
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 log 2 n + 1 Number of times the comparison is executed = Number of repetitions of the loop’s body + 1. 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