0% found this document useful (0 votes)
11 views22 pages

CENG205 Ders4

This document discusses different data structures for sparse matrices including storing non-zero elements in a struct. It also covers algorithms for matrix transpose including a basic O(n^2) method and an improved O(n) method using counting and prefix sums.

Uploaded by

alimurattmercan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

CENG205 Ders4

This document discusses different data structures for sparse matrices including storing non-zero elements in a struct. It also covers algorithms for matrix transpose including a basic O(n^2) method and an improved O(n) method using counting and prefix sums.

Uploaded by

alimurattmercan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CENG 205

Data structures
Lecture 4:
Lower/Upper Triangular Matrix
Band Matrix
Sparse Matrix
Lower Triangular Matrix
Lower Triangular Matrix
• Does the definition of a special data structure for triangular matrix provide any
benefits over a typical matrix in terms of memory and processing time?
• We can insert the items in a single dimensional array:

• ALT
a00 a10 a11 a20 a21 a22 a30 a31 a32 a33
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

• Number of items in the array becomes:

1+ 2 +... + (n-1) + n = n(n+1)


2
Lower Triangular Matrix

• How can we find the position of u[i][j] in the array?

• Answer: i=1, there is one item in the 0th row, 2 items in the 1st row.
• i=2, there is one item in the 0th row, 2 items in the 1st row, 3 items in the 2nd row.

• Therefore the address of u[i][j] in the array is calculated as below:


i
k = å(t) + ( j ) = (0 +1+ 2 +... + i) + ( j )
t=0

i(i +1)
= +( j)
2
Lower Triangular Matrix

void main(void){
int alt[MAX_SIZE];
int i, n;
scanf(“%d“, &n); //matrix size
readtriangularmatrix(alt,n);
for(i=0; i<=n*(n+1)/2-1; i++)
printf(“ %d“, alt[i]);

i=gettriangularmatrix(3,0,n);
if(i==-2)
printf(“\n invalid index\n“);
else if(i==-1)
printf(“\n access to the upper triangular\n“);
else
printf(“\n the position in ‘alt’ matrix: %d value: %d \n“, i, alt[i]);
Lower Triangular Matrix
void readtriangularmatrix(int alt[], int n) int gettriangularmatrix(int i, int j, int n){
{
int i, j, k; if(i<0 || i>=n || j<0 || j>=n){
if(n*(n+1)/2 > MAX_SIZE){ printf(“\n invalid index\n“);
printf(“\n invalid array size \n“); exit(-2);
exit(-1); }
} else if(i>=j) //valid index
else return (i+1)*i/2+j;
for(i=0; i<=n-1; i++){ else return -1; //outside of the triangular
k=(i+1)*i/2; //value is zero
for(j=0; j<=i; j++)
scanf(“%d“, &alt[k+j]);
}
}
Band Matrix

Matrix (n, a) : n by n matrix, non-zero entries are confined to a diagonal band,


comprising the main diagonal and zero or more diagonals (a-1) on either side.
Band Matrix
Band Matrix

• What kind of a data structure can we use?

• We can insert the items in a single dimensional array:


a20 a31 a10 a21 a32 a00 a11 a22 a32 a33 a01 a12 a23
• ALT

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

• What is the number of items in the array?


Band Matrix

• What is the number of items in the array?

• Number of items on and below the diagonal:

n+ (n-1)+ (n- 2)+... + n- (a-1)


• Number of items above the diagonal:

(n-1)+ (n- 2)+... + n- (b-1)


• Sum of these becomes:

Sum= n+ (n-1)+ (n- 2)+... + n- (a-1)+ (n-1)+ (n- 2)+... + n- (b-1)

(a-1)a (b-1)b
= n(a+ b-1) - -
2 2
Band Matrix
Band Matrix
void main(void){
int band[MAX_SIZE];
int search[MAX_SIZE];

int i, n, a, b;
printf(“ n:“, &n); scanf(“%d“, &n);
printf(“ a:“, &a); scanf(“%d“, &a);
printf(“ b:“, &b); scanf(“%d“, &b);

buildbandmatrix(band,search,a,b);

for(i=0; i<=n*(a+b-1)-a*(a-1)/2-b*(b-1)/2-1; i++)


printf(“ %d “,band[i];

printf(“\n“);
for(i=0; i<=a+b-2; i++)
printf(“ %d“, search[i]);

i=getbandmatrix(3,3,n,a,b,search);

if(i==2)
printf(“\n invalid index“);
else if(i==1)
printf(“\n item to be searched: 0“);
else
printf(“\n item to be searched: %d→%d“, i, band[i]);
Band Matrix
void buildbandmatrix(int band[], int search[], int n, int a, int b){

int i, k, itemnum;

if(n*(a+b-1)-a*(a-1)/2-b*(b-1)/2 > MAX_SIZE){

printf(“\n not enough memory“);


exit(-1);
}
else{
itemnum=0;
for(i=-a+1; i<=b-1; i++){ //for each diagonal
search[i+a-1]=itemnum;

for(k=0; k<= n-abs(i)-1; k++) //for the current diagonal


scanf(“%d“, &band[search[i+a-1]+k]);
itemnum = itemnum+(n-abs(i));
}
}
}
Band Matrix
void getbandmatrix(int i, int j, int n, int a, int b, int search[]){

if(i>=n || i<0 || j>=n || j<0){ //index overflow


printf(“\n invalid index\n“);
return -2;
}
else{
if(j>i) //above the diagonal
if(j-i<b) //above the upper band
return(search[a-1+j-i]+i); //yes
else //no
return -1;
else if(i-j<a) //below or on the diagonal
return(search[j-i+a-1]+j);
else //not on the band
return -1;
}
Sparse Matrix

• Most of the elements are zero.


• It wastes space.
Sparsity: the fraction of zero elements.

Basic matrix operations:


1. Creation
2. Addition
3. Multiplication
4. Transpose
Sparse Matrix
Data Structure
#define MAX_TERMS 101
• a[0].row: row index
typedef struct{ • a[0].col: column index
int col;
int row; • a[0].value: number of items in the
int value; sparse matrix
}term;
term a[MAX_TERMS];

Rows and columns are in ascending


order!
Sparse Matrix
Matrix Transpose

• Replacement of rows and columns in a matrix is called the transpose


of the matrix:
é 1 3 ù é 1 0 ù
A=ê ú A =ê
'
ú
ë 0 4 û ë 3 4 û
• The item a[i][j] becomes a[j][i].
Matrix Transpose
void transpose(term a[],term b[])
{
int n,i,j,currentb;
n=a[0].value; //number of items
b[0].row=a[0].col; //number of rows
b[0].col=a[0].row; //number of columns
b[0].value=n;

if(n>0){
currentb=1;
for(i=0; i<a[0].col; i++)
for(j=1; j<=n; j++) //find the ones with col i in a
if(a[j].col==i){
b[currentb].row=a[j].col;
b[currentb].col=a[j].row;
b[currentb].value=a[j].value;
currentb++;
}
}
}

Question: What is the complexity of this method?


Fast Transpose
#define MAX_TERM 101
typedef struct{
int row;
int col;
int value;
} term;
term a[MAX_TERM];

void fastTranspose(term a[], term b[])


{

int ItemNum[MAX_COL], StartPos[MAX_COL];


int i,j,ColNum=a[0].col,TermNum=a[0].value;
b[0].value=TermNum;
if(TermNum>0){ //does the item exist?
for(i=0;i<ColNum;i++)
ItemNum[i]=0;
for(i=1;i<=TermNum;i++)
ItemNum[a[i].col]++;
StartPos[0]=1;
for(i=1;i<ColNum;i++)
StartPos[i]=StartPos[i-1]+ItemNum[i-1];
for(i=1;i<=TermNum;i++){
j=StartPos[a[i].col]++;
b[j].row=a[i].col; b[j].col=a[i].row;
b[j].value=a[i].value;
}
}
}
Fast Transpose

• Execute the fastTranspose method.

• Question: What is the complexity of the method?

• Compare its complexity with the previous transpose method.

You might also like