Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

DSA ASSIGNMENT – 3

Tutorial sheet 2
-Praveen Kumar
-2020UCP1110

1) Assume that n lists, n > 1, are being represented sequentially in the


one-dimensional array A(1: m). Let FRONT(i) be one less than the position
of the first element in the ith list and let REAR(i) point to the 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)

Sol->
A)FRONT(i) represents position of first element of ( ith list – 1)
REAR(i) represents position of last element of ith list
Boundary condition,
REAR(i) <= FRONT(i+1) , 1<=i<=n
FRONT(n+1) = m
REAR(N) <= m
First position in the list starts from 1
Therefore,
FRONT(i) >= 0
(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 A.

Sol->
B)Procedure to insert(i,j,item) to insert item after (j-1)st element in list i ,
for(int i=REAR(i+1); i>j; i--)
{
array[REAR(i)] = array[REAR(i-1)];
}
array[FRONT(i) + 1 + j] = item;

if REAR(i)==m
No insertion

2) In a lower triangular matrix, A, with n rows, the maximum number of


nonzero terms inrow k is k. Hence, the total number of nonzero terms is 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).

Sol->
Given that the elements of array A (which is lower triangular matrix) is stored by
row in array B.
In matrix A ith row contains i elements.
So the address of A[i][j] in array B will be

Address of A[i][j] = Σ(k+1) from(0 to i-1) + j + 1


=((i-1)(i))/2 + i - 1 + j + 1
=((i-1)(i+2))/2 + j +1
=(i(i+1))/2 + j

3. 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 .
Sol->
If(n==m){
For(I=0; i<n; i++){
If(ai==bi){
Count++;
}
if(ai<bi && count == i-1){
Return –1;
}
Else if(ai>bi && count == i-1){
Return +1;
}
}
if(n<m){
for(i=0; i<n; i++){
if(ai==bi){
Count++;
}
else if(ai>bi and count == i-1){
Return +1;
}
if(count==n){
return –1;
}
else{
Return +1;
}
4. Write elements of a C array A[3][2][3] in
(a) row-major order
(b) column-major order

Sol->
A[3][2][3] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}
(a) Row Major:
A[0,0,0] = 0 , A [0,0,1] = 1 , A[0,0,2] = 2
A[0,1,0] = 3 , A [0,1,1] = 4 , A[0,1,2] = 5
A[1,0,0] = 6 , A [1,0,1] = 7 , A[1,0,2] = 8
A[1,1,0] = 9 , A [1,1,1] = 10 , A[1,1,2] = 11
A[2,0,0] = 12 , A [2,0,1] = 13 , A[2,0,2] = 14
A[2,1,0] = 15 , A [2,1,1] = 16 , A[2,1,2] = 17

(b) Column-major order


A[0,0,0] = 0 , A [0,0,1] = 2 , A[0,0,2] = 4
A[0,1,0] = 1 , A [0,1,1] = 3 , A[0,1,2] = 5
A[1,0,0] = 6 , A [1,0,1] = 8 , A[1,0,2] = 10
A[1,0,0] = 7 , A [1,0,1] = 9 , A[1,0,2] = 11
A[2,0,0] = 12 , A [2,0,1] = 14 , A[2,0,2] = 16
A[2,0,0] = 13 , A [2,0,1] = 15 , A[2,0,2] = 17

5) 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⟩. ⟨A,B⟩⟨ ̇C,D⟩ = (A+ιB)(̇C+ιD) =
(AC−BD)+ι(AD+BC)

Sol->
for(int i=0; i<m; i++){
for(int j=0; j<q; j++){
c[i][j] = 0;
}
}
for(int i=0; i<m; i++){
for(k=0; k<n; k++){
for(int j=0; j<q; j++){
c[i][j]= c[i][j] + a[i][k]*b[k][j];
}
}
}
//This is the code for matrix multiplication
Now, use this code for multiplying matrix A*C and B*D
Real part = A*C - B*D
Use this code for multiplying matrix A*D and B*C
Imaginary part = A*D + B*C

6) Obtain addressing formula for elements of a n-dimensional


matrix A(l1 : u1,l2 : u2,l3 : u3, . . . , ln : un) assuming dimension order
representation.

Sol->
Size of array = u1*u2*u3*u4....un
No. Of bytes per element = k
Starting address of first row: M
u1 represents dimension of row
No. Of elements per row = u2*u3*u4....un
Address of second row : M + u2*u3*u4....un*k
Address of third row : M + 2*u2*u3*u4....un*k
Address of (r1 + 1)th row : M + r1* u2*u3*u4....un*k
Address upto A[r1,r2,0]: M + r1* u2*u3*u4....un*k + r2*d3*d4*d5...dn*k
Address upto A[r1,r2,r3,...rn]: M + r1* u2*u3*u4....un*k + r2* u3*u4*u5....un*k
+ r3* u4*u5*u5....un*k …...rn*k

= M + s*[Σx=1 to x=n-1 ((rx){∏ y=1 to y=(x+1) uy})] + rn*k

7) A band matrix An;a is a n _ n matrix in which non-zero terms lie in a band


centered around main diagonal. Band includes a total of 2a 1 diagonals
including main diagonal, a diagonals above and a diagonals below this main
diagonal as illustrated in Figure ??. Obtain

(a) Number of elements in a band matrix An;a


Sol->
Number of elements in the kth diagonal from the center is: n-k
int sum = 0;
for(int i=1; i<=a; i++){
sum = sum + n - i;
}
sum = 2(sum) + n;
Where sum represent the total number of elements in the matrix.
sum = n(2*a+ 1) - a(a+1)

(b) Obtain addressing formula for A[i, j] if A is stored sequentially in an


array B such that only non-zero terms of A are stored
i. row-wise
Sol->
int a[i][j]
if(i<=a){
int address = 0
for(int k=0; k<i; k++){
address = address + (a+1+k);
}
address = address +j +1;
(address of a[i][j] in B is = (i+2)(i-1)/2 + (i-1)a + j + 1)
}
else{
int address = 0, sum =0;
for(int k=0; k<=a; k++){
sum = sum + (a+1+k);
}
for(int k =a+1; k<i; k++){
address = address + n-(k-a);
}
address = sum + address + j + 1 - (i-a)
(address of a[i][j] in B is = 3(a)(a+1)/2 + (k-a-1)(n+a) - (i)(i– 1)/2
+ a(a+1)/2)
}

ii. column-wise
Sol->
int a[i][j]
if(j<=a){
int address = 0
for(int k=0; k<j; k++){
address = address + (a+1+k);
}
address = address +i +1;
(address of a[i][j] in B is = (j+2)(j-1)/2 + (j-1)a + i + 1)
}
else{
int address = 0, sum =0;
for(int k=0; k<=a; k++){
sum = sum + (a+1+k);
}
for(int k =a+1; k<j; k++){
address = address + n-(k-a);
}
address = sum + address + i + 1 - (j-a)
(address of a[i][j] in B is = 3(a)(a+1)/2 + (i-a-1)(n+a) - (j)(j – 1)/2 + a(a+1)/2)
}

iii. diagonal-wise (lowermost diagonal _rst and each diagonal stored from
leftmost to rightmost element)
Sol->
int a[i][j]
if(I>=j){
int temp = i-j, address = 0;
for(int k=temp-1; k<=a; k++){
address = address + (n - k);
}
address = address + j + 1;
(address of a[i][j] in B = n(a- (i-j) + 2) - a(a+1)/2 + (i-j-2)(i-j-1)/2 + j + 1)
}
else{
int temp = j-i, address = 0, sum = 0;
for(int k=0; k<=a; k++){
sum = sum + (n - k);
}
for(int k=1; k<=temp -1; k++){
address = address + n- k;
}
address = address + sum + i + 1 ;
(address of a[i][j] in B = (a+1)(n-a)/2 + (j-i)(n -(j-i) -1)/2)
}

(c) relationship between i and j for non-zero elements.


Sol->
Relation between i and j is:
|i – j| = distance of the diagonal(in which a[I][j] belongs) from the
main diagonal

You might also like