-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransporse_sparse.c
144 lines (127 loc) · 3.81 KB
/
transporse_sparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//CSL201 DATA STRUCTURES LAB ----- VISHRUTH S, CS3A, 61
//CYCLE 2 QUESTION 9
//PROGRAM TO CONVERT A SPARSE MATRIX TO TUPLE FORM AND PRINT IT
//AND FIND ITS TRANPOSE. PRINT BOTH TUPLE AND ORIGINAL FORM OF TRANSPOSE
#include <stdio.h>
#define MAX_TERMS 100
struct Sparse_matrix
{
int row;
int col;
int value;
};
//SPARSE MATRIX STRUCTURE AND SIZE
struct Sparse_matrix sparse1[MAX_TERMS];
struct Sparse_matrix transposeSparse[MAX_TERMS];
int SIZE;
//SIZE OF ROWS AND COLS OF INPUT MATRIX
const int MATRIX_ROWS, MATRIX_COLS;
//INPUT MATRIX AND TRANSPOSE
int matrix[100][100];
int transpose[100][100];
//FUNCTION TO PRINT MATRIX IN 2D NORMAL ARRAY FORM
void printMatrix(int printMatrix[100][100], int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%4d", printMatrix[i][j]);
}
printf("\n");
}
}
//FUNCTION TO PRINT SPARSE MATRIX (TUPLE FORM)
void printSparseMatrix(struct Sparse_matrix sparse[100])
{
int i;
printf("\n");
printf("ROW COLUMN VALUE");
for (i = 0; i < SIZE; i++)
{
printf("\n");
printf("%d\t%d\t%d", sparse[i].row, sparse[i].col, sparse[i].value);
}
}
//CONVERT INPUT MATRIX TO SPARSE MATRIX (TUPLE FORM)
void convertToSparseMatrix()
{
//FIRST ROW IS META-DATA: < no.of rows, no.of cols, no.of non-zero entries >
sparse1[0].row = MATRIX_ROWS;
sparse1[0].col = MATRIX_COLS;
int i, j, k = 1;
for (i = 0; i < MATRIX_ROWS; i++)
{
for (j = 0; j < MATRIX_COLS; j++)
{
if (matrix[i][j])
{
sparse1[k].row = i + 1; // +1 since tuple is 1 based indexing
sparse1[k].col = j + 1;
sparse1[k].value = matrix[i][j];
k++;
}
}
}
SIZE = k;
sparse1[0].value = k - 1; //value = no.of non-zero elements = k-1
}
//FIND TRANSPOSE OF SPARSE MATRIX
void transposeSparseMatrix()
{
//FIRST ROW IS META-DATA: < no.of rows, no.of cols, no.of non-zero entries >
transposeSparse[0].row = MATRIX_COLS; //rows and cols are interchanged
transposeSparse[0].col = MATRIX_ROWS;
transposeSparse[0].value = sparse1[0].value; //value will be same for transpose
int i, j, k = 1, min_col;
for (i = 1; i <= MATRIX_COLS; i++)
{
for (j = 1; j <= SIZE; j++)
{
if (sparse1[j].col == i)
{
transposeSparse[k].row = sparse1[j].col;
transposeSparse[k].col = sparse1[j].row;
transposeSparse[k].value = sparse1[j].value;
k++;
}
}
}
}
//CONVERT TUPLE FORM BACK TO 2D MATRIX
void convertSparseToNormal()
{
int i, j;
//INITILIAZE TRANSPOSE ARRAY WITH ALL ELEMENTS 0
for (i = 0; i < MATRIX_COLS; i++)
for (j = 0; j < MATRIX_ROWS; j++)
transpose[i][j] = 0;
//transpose[row][col] = Tuple[i].value (-1 is added since tuple is 1 based indexing)
for (i = 1; i < SIZE; i++)
{
transpose[transposeSparse[i].row - 1][transposeSparse[i].col - 1] = transposeSparse[i].value;
}
}
int main()
{
int i, j;
printf("\nEnter matrix dimensions: ");
scanf("%d %d", &MATRIX_ROWS, &MATRIX_COLS);
printf("\nEnter matrix: ");
for (i = 0; i < MATRIX_ROWS; i++)
for (j = 0; j < MATRIX_COLS; j++)
scanf("%d", &matrix[i][j]);
printf("\nINPUT MATRIX\n");
printMatrix(matrix, MATRIX_ROWS, MATRIX_COLS);
convertToSparseMatrix();
printf("\n\nSPARSE MATRIX (TUPLE FORM)");
printSparseMatrix(sparse1);
printf("\n\nTRANSPOSE OF SPARSE MATRIX (TUPLE FORM)");
transposeSparseMatrix();
printSparseMatrix(transposeSparse);
printf("\n\nTRANSPOSE OF INPUT MATRIX\n");
convertSparseToNormal();
printMatrix(transpose, MATRIX_COLS, MATRIX_ROWS);
return 0;
}