Sie sind auf Seite 1von 16

Nonrecursive Algorithms

CSC 3102

0.1

B.B. Karki, LSU

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.


0.2

CSC 3102

B.B. Karki, LSU

Analyzing Efficiency of Nonrecursive Algorithms


Steps in mathematical analysis of nonrecursive algorithms:
Decide on parameter n indicating input size Identify algorithms basic operation Determine worst, average, and best case for input of size n if the basic

operation count depends not only on n


Set up summation for C(n) reflecting algorithms loop structure Express the number of times the algorithms basic operation is executed Simplify summation using standard formulas and rules of sum manipulation

(see Appendix A)
Find a closed-form formula for the count and/or establish its order of growth.
CSC 3102
0.3

B.B. Karki, LSU

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

maxval A[0] for i 1 to n - 1 do if A[i] > maxval maxval A[i] return maxval

CSC 3102

0.4

B.B. Karki, LSU

Example 1: Maximum Element (Cont.)

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

B.B. Karki, LSU

Example 2: Unique Elements

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

B.B. Karki, LSU

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.

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

Example 3: Matrix Operations


A 2D matrix is the standard representation Classic matrix multiplication: O(n3) Given two n-by-n matrices A and B, compute their product C = AB using n1 definition based algorithm. C is an n-by-n matrix whose Cij = Aik Bkj elements are computed as the scalar (dot) products of k= 0 the rows of A and the columns of B. Strassens algorithm based on a divide-and-conquer approach: O(n2.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.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

B.B. Karki, LSU

Matrix Multiplication: C(n)


Input size = n, the matrix order Two operations in the innermost loop are addition and multiplication Both are executed exactly once on each repetition of the innermost loop The basic operation is multiplication Formula for the basic operation count: One multiplication is executed on each repetition of the innermost loop (k) and we need to find the total number of multiplications made for all pairs of i and j The algorithm computes n2 elements of the product matrix. Each element is computed as the scalar (dot) product of an n-element row of A and an n-element column of B, which takes n multiplications.
n1 n1 n1 n1 n1 n1

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

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

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.

Transpose of M is M': M'[i].row = M[i].col M'[i].col = M[i].row


CSC 3102
0.10

B.B. Karki, LSU

Algorithms for SM Transpose

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

B.B. Karki, LSU

Nave Sparse Matrix Transpose Code

; 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

B.B. Karki, LSU

Nave 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:
n1 t n1

C(n) = 1 = t = nt (nt)
i= 0 j=1 i= 0

For dense matrix, t n2, the complexity is of the order O(n3).


CSC 3102

0.13

B.B. Karki, LSU

Fast Sparse Matrix Transpose: Code

CSC 3102

0.14

B.B. Karki, LSU

Fast SM Transpose Analysis


Given a matrix M, find its transpose matrix M': The process involves four

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

B.B. Karki, LSU

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

the loops body + 1.


CSC 3102
0.16

B.B. Karki, LSU

Das könnte Ihnen auch gefallen