Nonrecursive Algorithms: Analysis and Examples - Prof. B. Karki, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/31/2009

koofers-user-kw5-1
koofers-user-kw5-1 🇺🇸

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B.B. Karki, LSU
0.1
CSC 3102
Nonrecursive Algorithms
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Nonrecursive Algorithms: Analysis and Examples - Prof. B. Karki and more Study notes Computer Science in PDF only on Docsity!

Nonrecursive Algorithms

Definition and Examples

 Nonrecursive algorithm:

 Executed only once to solve the problem.

 Examples

 Largest element in a list of numbers  Element uniqueness problem  Matrix operations: addition, multiplication, and transpose  Digits in binary representation

 Recursive algorithm invokes (makes reference to) itself

repeatedly until a certain condition matches.

Example 1: Maximum Element

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 maxvalA[0] for i1 to n - 1 do if A[ i] > maxval maxvalA[ i] return maxval

Example 1: Maximum Element (Cont.)

 Input size = n , the number of elements in the array

 Algorithm’s 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.

C ( n ) = 1 i = 1 n − 1 ∑ =^ n^ −^1 ∈^ Θ( n )

Example 2: Unique Elements (Cont.)

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

C

worst

( n ) = 1

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 ∑ =^

( n − 1 ) n
∈ Θ( n

2

Example 3: Matrix Operations

 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 i0 to n - 1 do for j0 to n - 1 do C[ i, j]0. for k0 to n - 1 do C[ i, j]C[ i, j] + A[ i, k] * B[ k, j] return C

Cij = Aik Bkj

k = 0 n − 1 ∑

CSC 3102 0.10 B.B. Karki, LSU

Sparse Matrix (SM) Transpose

 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].colM'[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

Algorithms for SM Transpose

 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

Naïve SM Transpose Analysis

 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, tn^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 ∑

Fast Sparse Matrix Transpose: Code

Example 4: Binary

 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 count1 while n > 1 do countcount + 1 n ←  n/2return count