0% found this document useful (0 votes)
12 views

Special Matrices

Uploaded by

riteshyaaraa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Special Matrices

Uploaded by

riteshyaaraa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Handling Special Matrices

Special Matrices
• Square – same number of rows and columns.
• Some special forms of square matrices are
– Diagonal: M(i,j) = 0 for i ≠ j
– Tridiagonal: M(i,j) = 0 for |i-j| > 1
– Lower triangular: M(i,j) = 0 for i >= j
– Upper triangular: M(i,j) = 0 for i <= j
– Symmetric M(i,j) = M(j,i) for all i and j
Thapar University UCS406 - Data Structures and Algorithms 2
Contd…
2 0 0 0 2 1 0 0 2 0 0 0
0 1 0 0 3 1 3 0 5 1 0 0
0 0 4 0 0 5 2 7 0 3 1 0
0 0 0 6 0 0 9 0 4 2 7 0
Diagonal Tri-Diagonal Lower Triangular

2 1 3 0 2 4 6 0
0 1 3 8 4 1 9 5
0 0 1 6 6 9 4 7
0 0 0 0 0 5 7 0
Upper Triangular Symmetric
Thapar University UCS406 - Data Structures and Algorithms 3
Contd…
• Why are we interested in these "special" matrices?
– We can provide more efficient implementations
for specific special matrices.
– Rather than having a space complexity of O(n2),
we can find an implementation that is O(n).
– We need to be clever about the "store" and
"retrieve" operations to reduce time.

Thapar University UCS406 - Data Structures and Algorithms 4


Diagonal Matrix
• Naive way to represent n x n diagonal matrix
– <datatype> d[n][n]
– d[i-1][j-1] for D(i,j)
– Requires n2 x sizeof(<datatype>) bytes of memory.
2 0 0 0
• Better way 0 1 0 0
– <datatype> d[n] 0 0 4 0
– d[i-1] for D(i,j) where i = j 0 0 0 6
0 for D(i,j) where i ≠ j
– Requires n x sizeof(<datatype>) bytes of memory.
Thapar University UCS406 - Data Structures and Algorithms 5
Example
1. #include<stdio.h>
2. #define MAX 5
3. int main()
4. { int i,j, a[MAX];
5. printf("\nEnter elements (row major):\n");
6. for(i = 0; i < MAX; i++)
7. scanf("%d",&a[i]);
8. printf("\nThe matrix is...\n");
9. for(i = 0; i < MAX; i++)
10. { for(j = 0; j < MAX; j++)
11. { if(i==j)
12. printf("%d ", a[i]);
13. else
14. printf("0 "); }
15. printf("\n"); }
16. return 0; }
Thapar University UCS406 - Data Structures and Algorithms 6
Tridiagonal Matrix
• Nonzero elements lie on one of three diagonals:
– main diagonal: i = j
– diagonal below main diagonal: i = j+1
– diagonal above main diagonal: i = j-1
• Total elements are 3n – 2: <datatype> d[3n-2]
• Mappings 2 1 0 0
– by row [2,1,3,1,3,5,2,7,9,0] 3 1 3 0
– by column [2,3,1,1,5,3,2,9,7,0] 0 5 2 7
– by diagonal [3,5,9,2,1,2,0,1,3,7] 0 0 9 0
Thapar University UCS406 - Data Structures and Algorithms 7
Example
1. #include<stdio.h>
2. #define MAX 5
3. int main()
4. { int i,j,k=0, size = 3*MAX-2, a[size];
5. printf("\nEnter elements (row major):\n");
6. for(i = 0; i < size; i++)
7. scanf("%d",&a[i]);
8. printf("\nThe matrix is...\n");
9. for(i = 0; i < MAX; i++)
10. { for(j = 0; j < MAX; j++)
11. { if(i-j == -1 || i-j == 0 || i-j == 1)
12. { printf("%d ", a[k]); k++; }
13. else
14. printf("0 "); }
15. printf("\n"); }
16. return 0; }
Thapar University UCS406 - Data Structures and Algorithms 8
Triangular Matrix
• Nonzero elements lie in the upper triangular or
lower triangular region.
• Total elements are 1 + 2 + … + n = n(n+1)/2:
<datatype> d[(n(n+1)/2)]
• Mappings
– by row 2 5 1 0 2 0 0 0
[2,5,1,0,3,1,4,2,7,0] 0 3 1 4 5 1 0 0
– by column 0 0 2 7 0 3 1 0
[2,5,0,4,1,3,2,1,7,0] 0 0 0 0 4 2 7 0
[2,5,3,1,1,2,0,4,7,0] Upper Triangular Lower Triangular

Thapar University UCS406 - Data Structures and Algorithms 9


Example
1. #include<stdio.h>
2. #define MAX 4
3. int main()
4. { int i,j,k=0, size = (MAX*(MAX+1))/2, a[size];
5. printf("\nEnter elements (row major):\n");
6. for(i = 0; i < size; i++)
7. scanf("%d",&a[i]);
8. printf("\nThe upper triangular matrix is...\n");
9. for(i = 0; i < MAX; i++)
10. { for(j = 0; j < MAX; j++)
11. { if(i <= j)
12. { printf("%d ", a[k]); k++; }
13. else
14. printf("0 "); }
15. printf("\n"); }
Thapar University UCS406 - Data Structures and Algorithms 10
Contd…
16. k = 0;
17. printf("\nThe lower triangular matrix is...\n");
18. for(i = 0; i < MAX; i++)
19. { for(j = 0; j < MAX; j++)
20. { if(i >= j)
21. { printf("%d ", a[k]); k++; }
22. else
23. printf("0 "); }
24. printf("\n"); }
25. return 0; }

Thapar University UCS406 - Data Structures and Algorithms 11


Symmetric Matrix
• An n x n matrix can be represented using 1-D array
of size n(n+1)/2 by storing either the lower or
upper triangle of the matrix.
2 4 6 0
4 1 9 5
6 9 4 7
0 5 7 0

• Use one of the methods for a triangular matrix


• The elements that are not explicitly stored may be
computed from those that are stored
Thapar University UCS406 - Data Structures and Algorithms 12
Example
1. #include<stdio.h>
2. #define MAX 5
3. int getIndx(int i, int j) // Function to compute index for values to be
4. { // printed in lower triangular matrix.
5. if ( j == 0)
6. return i;
7. else
8. return (getIndx(i,j-1) + MAX - j);
9. }
10. int main()
11. {
12. int i,j,k=0, size = (MAX*(MAX+1))/2, a[size];
13. printf("\nEnter elements (row major):\n");
14. for(i = 0; i < size; i++)
15. scanf("%d",&a[i]);
Thapar University UCS406 - Data Structures and Algorithms 13
Contd…
16. printf("\nThe matrix is...\n");
17. for(i = 0; i < MAX; i++)
18. { for(j = 0; j < MAX; j++)
19. { if(i <= j)
20. { printf("%d ", a[k]); k++; }
21. else
22. printf("%d ",a[getIndx(i,j)]);
23. }
24. printf("\n");
25. }
26. return 0;
27. }

Thapar University UCS406 - Data Structures and Algorithms 14


Sparse Matrix
• A matrix is sparse if many of its elements are zero.
• A matrix that is not sparse is dense.
• Two possible representations
– Array (also known as triplet) 0 0 0 2
0 6 0 0
– Linked list
0 0 0 9
0 5 4 0

Thapar University UCS406 - Data Structures and Algorithms 15


Array representation
Row Col Value
[0] [1] [2] [3] [4] [5]
6 6 8
[0] 15 0 0 22 0 -15 0 0 15
[1] 0 11 3 0 0 0 0 3 22
0 5 -15
[2] 0 0 0 -6 0 0
1 1 11
[3] 0 0 0 0 0 0 1 2 3
[4] 91 0 0 0 0 0 2 3 -6
4 0 91
[5] 0 0 28 0 0 0
5 2 28

Thapar University UCS406 - Data Structures and Algorithms 16


Linked-list Representation
• Special header node contains
– Pointers to the first node of the two linked lists
formed with row and column header nodes.
– Information about the size of sparse matrix.
Special Header Node
Pointer to Total Total Pointer to the
the first row number number of first column
header node of rows columns header node

Thapar University UCS406 - Data Structures and Algorithms 17


Contd…
• Column/Row header node contains
– Pointer to the first node in the column/row list.
– Column/row number.
– Pointer to the next column/row header node.
Column Header Node
Pointer to the first Column Pointer to the next
node in the column list number column header node
Row Header Node
Pointer to the next Row Pointer to the first
row header node number node in the row list
Thapar University UCS406 - Data Structures and Algorithms 18
Contd…
• Element node contains
– Column number, row number, and the non-zero
value.
– Pointers to the next node in the same
column/row list.
Element Node
Row number Column number Non-zero value
Pointer to the next node Pointer to the next node
in the same column list in the same row list

Thapar University UCS406 - Data Structures and Algorithms 19


6 6 0 1 2 3 x 4 5 x

0 0 0 15 0 3 22 0 5 -15
x x

1 1 1 11 1 2 3
x x

2 2 3 -6
x x

3 x

4 4 0 91
x x

x 5 5 2 28
Operations
• Transpose
• Addition
• Multiplication

Thapar University UCS406 - Data Structures and Algorithms 21


Transpose
Row Col Value Row Col Value Row Col Value
0 0 15 0 0 15 0 0 15
0 3 22 3 0 22 0 4 91
0 5 -15 5 0 -15 1 1 11
1 1 11 1 1 11 2 1 3
1 2 3 2 1 3 2 5 28
2 3 -6 3 2 -6 3 0 22
4 0 91 0 4 91 3 2 -6
5 2 28 2 5 28 5 0 -15
Original Column Major Row Major
Thapar University UCS406 - Data Structures and Algorithms 22
Row Col Value
Addition 0 0 30
0 3 22
Row Col Value Row Col Value 0 4 91
0 0 15 0 0 15 0 5 -15
0 3 22 0 4 91 1 1 22
0 5 -15 1 1 11 1 2 3
2 1 3
1 1 11 2 1 3
2 3 -6
1 2 3 2 5 28
2 5 28
2 3 -6 3 0 22
3 0 22
4 0 91 3 2 -6
3 2 -6
5 2 28 5 0 -15 4 0 91
5 0 -15
Thapar University UCS406 - Data Structures and Algorithms 5 2 2823
Multiplication
• Compute A x B
• First take transpose of B.
• Multiply only if the corresponding elements are
present and add them for each position in the
resultant matrix.

Thapar University UCS406 - Data Structures and Algorithms 24


Example
• Uploaded in a separate file.

Thapar University UCS406 - Data Structures and Algorithms 25

You might also like