Academia.eduAcademia.edu

Data Structures and Algorithms

Data Structures and Algorithms September 23, 2014 1. If A = (a1 , ..., an )and B = (b1 , ..., bm ) are ordered lists, then A < B if ai = bi for 1 ≤ i < j and aj < bj or if ai = bi for 1 ≤ i ≤ n and n < m. Write a procedure which returns -1,0, + 1 depending upon whether A < B, A = B or A > B. Assume you can compare atoms ai and bj . 2. Assume that n lists, n > 1, are being represented sequentially in the one dimensional array SPACE (1:m). Let FRONT(i) and REAR(i) point to the first and last element of the ith list, 1 ≤ i ≤ n. Further assume that REAR(i) ≤ FRONT(i + 1), 1 ≤ i ≤ n with FRONT(n+ 1) = m. The functions to be performed on these lists are insertion and deletion. (a) Obtain suitable initial and boundary conditions for FRONT(i) and REAR(i). (b) Write a procedure INSERT(i,j,item) to insert item after the (j - 1)st element in list i. This procedure should fail to make an insertion only if there are already m elements in SPACE. 3. Using the assumptions of above question, write a procedure DELETE(i,j,item) which sets item to the j-th element of the i-th list and removes it. The i-th list should be maintained as sequentially stored. 4. The polynomials A(x) = x2n +x2n−2 +...+x2 +x0 and B(x) = x2n+1 +x2n−1 +...+x3 +x cause PADD to work very hard. For these polynomials determine the exact number of times each statement will be executed. 5. When all the elements either above or below the main diagonal of a square matrix are zero, then the matrix is said to be triangular. In a lower triangular matrix, A, with n rows, the maximum number of nonzero terms in row i is i. Hence, the total number of Pi=n nonzero terms is i=1 i = n(n + 1)/2, For large n it would be worthwhile to save the space taken by the zero entries in the upper triangle. Obtain an addressing formula for elements aij in the lower triangle if this lower triangle is stored by rows in an array B(1:n(n + 1)/2) with A(1,1) being stored in B(1). What is the relationship between i and j for elements in the zero part of A? 1 6. Let A and B be two lower triangular matrices, each with n rows. The total number of elements in the lower triangles is n(n + 1). Devise a scheme to represent both the triangles in an array C(1:n,1:n + 1). [Hint: represent the triangle of A as the lower triangle of C and the transpose of B as the upper triangle of C.] Write algorithms to determine the values of A(i,j), B(i,j) 1 ≤ i, j ≤ n from the array C. 7. Another kind of sparse matrix that arises often in numerical analysis is the tridiagonal matrix. In this square matrix, all elements other than those on the major diagonal and on the diagonals immediately above and below this one are zero. If the elements in the band formed by these three diagonals are represented rowwise in an array, B, with A (1,1) being stored at B(1), obtain an algorithm to determine the value of A(i,j),1 ≤ i, j ≤ n from the array B. 8. A complex-valued matrix X is represented by a pair of matrices (A,B) where A and B contain real values. Write a program which computes the product of two complex valued matrices (A,B) and (C,D), where (A,B) * (C,D) = (A + iB) * (C + iD) = (AC - BD) + i(AD + BC) Determine the number of additions and multiplications if the matrices are all n × n. 9. Obtain an addressing formula for the element A(i1 , i2 , ..., in ) in an array declared as A(l1 : ul , l2 : u2 , ..., ln : un ). Assume a row major representation of the array with one word per element and the address of A(l1 , l2 , ..., ln ). 10. An m × n matrix is said to have a saddle point if some entry A(i,j) is the smallest value in row i and the largest value in column j. Write a SPARKS program which determines the location of a saddle point if one exists. What is the computing time of your method ? 11. Given an array A(1:n) produce the array Z(1:n) such that Z(1) = A(n), Z(2) = A(n - 1), ..., Z(n - 1) = A (2), Z(n) = A(1). Use a minimal amount of storage. 2