|
| 1 | +/* Submitted by |
| 2 | + Name:REDDI HAREESH & Roll Number:150101051 |
| 3 | + Question3: |
| 4 | +*/ |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <time.h> |
| 8 | + |
| 9 | +//structure to store data |
| 10 | + struct node { |
| 11 | + int row, col, val; |
| 12 | + struct node *right, *down; |
| 13 | + }; |
| 14 | + |
| 15 | +//structure of column head |
| 16 | + struct chead { |
| 17 | + int col; |
| 18 | + struct chead *next; |
| 19 | + struct node *down; |
| 20 | + }; |
| 21 | + |
| 22 | +//structure of row head |
| 23 | + struct rhead { |
| 24 | + int row; |
| 25 | + struct rhead *next; |
| 26 | + struct node *right; |
| 27 | + }; |
| 28 | + |
| 29 | +//structure of additional head |
| 30 | + struct sparsehead { |
| 31 | + int rowCount, colCount; |
| 32 | + struct rhead *frow; |
| 33 | + struct chead *fcol; |
| 34 | + }; |
| 35 | + |
| 36 | +//main node |
| 37 | + struct sparse { |
| 38 | + int row, *data; |
| 39 | + struct node *nodePtr; |
| 40 | + struct sparsehead *smatrix; |
| 41 | + struct rhead **rowPtr; |
| 42 | + struct chead **colPtr; |
| 43 | + }; |
| 44 | + |
| 45 | +//Build the row and column links |
| 46 | + void initialize(struct sparse *sPtr, int row, int col) { |
| 47 | + int i; |
| 48 | + //memory allocation |
| 49 | + sPtr->rowPtr = (struct rhead **) calloc(1, (sizeof (struct rhead) * row)); |
| 50 | + sPtr->colPtr = (struct chead **) calloc(1, (sizeof (struct chead) * col)); |
| 51 | + for (i = 0; i < row; i++) |
| 52 | + sPtr->rowPtr[i] = (struct rhead *) calloc(1, sizeof (struct rhead)); |
| 53 | + |
| 54 | + for (i = 0; i < row - 1; i++) { |
| 55 | + sPtr->rowPtr[i]->row = i; |
| 56 | + sPtr->rowPtr[i]->next = sPtr->rowPtr[i + 1]; |
| 57 | + } |
| 58 | + |
| 59 | + for (i = 0; i < col; i++) |
| 60 | + sPtr->colPtr[i] = (struct chead *) calloc(1, sizeof (struct chead)); |
| 61 | + |
| 62 | + for (i = 0; i < col - 1; i++) { |
| 63 | + sPtr->colPtr[i]->col = i; |
| 64 | + sPtr->colPtr[i]->next = sPtr->colPtr[i + 1]; |
| 65 | + } |
| 66 | + |
| 67 | +//update additional head information |
| 68 | + sPtr->smatrix = (struct sparsehead *) calloc(1, sizeof (struct sparsehead)); |
| 69 | + sPtr->smatrix->rowCount = row;//assigning rowCount as no.of rows of matrix |
| 70 | + sPtr->smatrix->colCount = col;//assigning colCount as no.of columns of matrix |
| 71 | + sPtr->smatrix->frow = sPtr->rowPtr[0]; |
| 72 | + sPtr->smatrix->fcol = sPtr->colPtr[0]; |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | +//input sparse matrix |
| 77 | + void InputMatrix(struct sparse *sPtr, int row, int col,int k) |
| 78 | + { |
| 79 | + int t=time(NULL); |
| 80 | + srand(t); |
| 81 | + int i, j, n; |
| 82 | + int h=0,c=1;//starting counter c as 1 |
| 83 | + n = row * col;//n=no.of elements of the matrix |
| 84 | + sPtr->data = (int *) malloc(sizeof (int) * n);//allocate sufficient memory |
| 85 | + for(h=0;h<n;h++) |
| 86 | + { |
| 87 | + (sPtr->data[h])=0;//initialize all elements to 0 initially |
| 88 | + } |
| 89 | +//while loop will run if value of c is less than or equal to value of k |
| 90 | + while(c<=k) |
| 91 | + { |
| 92 | + i=rand()%row;//i will be randomly generated |
| 93 | + j=rand()%col;//j will be randomly generated |
| 94 | + if((sPtr->data[col*i+ j])==0) |
| 95 | + { |
| 96 | + (sPtr->data[col*i+ j])=rand()%1000;//replace 0 with randomly generated values for the randomly generated indexes |
| 97 | + c++; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +//displaying of randomly generated matrix |
| 103 | + void displayInputMatrix(struct sparse s, int row, int col) |
| 104 | + { |
| 105 | + int i; |
| 106 | + for (i = 0; i < row * col; i++) |
| 107 | + { |
| 108 | + if (i % col == 0) |
| 109 | + printf("\n\n"); |
| 110 | + printf("%d\t", s.data[i]); |
| 111 | + } |
| 112 | + printf("\n"); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | +//create 3-tuple array from randomly generated matrix |
| 117 | + void createThreeTuple(struct sparse *sPtr, struct sparse s, int row, int col,int k) |
| 118 | + { |
| 119 | + int i, j = 0, x = 0, y = 0, l = 0; |
| 120 | + sPtr->row = k; |
| 121 | + sPtr->data = (int *) malloc(sizeof(int)*(sPtr->row * 3)); |
| 122 | + |
| 123 | + for (i = 0; i < row * col; i++) { |
| 124 | + if (y % col == 0 && y != 0) { |
| 125 | + x++; |
| 126 | + y = 0; |
| 127 | + } |
| 128 | + if (s.data[i] != 0) { |
| 129 | + sPtr->data[l++] = x;//we are doing this assignment for indexes of l=0,3,6,9,12...... |
| 130 | + sPtr->data[l++] = y;//we are doing this assignment for indexes of l=1,4,7,10,13...... |
| 131 | + sPtr->data[l++] = s.data[i];//we are giving non zero values of the matrix for indexes of l=2,5,8,11,14..... |
| 132 | + } |
| 133 | + y++; |
| 134 | + } |
| 135 | + return; |
| 136 | + } |
| 137 | +//insert element to the list |
| 138 | + void insert(struct sparse *sPtr, int row, int col, int value) { |
| 139 | + struct rhead *rPtr; |
| 140 | + struct chead *cPtr; |
| 141 | + struct node *n1, *n2; |
| 142 | + struct sparsehead *smat = sPtr->smatrix; |
| 143 | + int i, j; |
| 144 | + |
| 145 | +//update node values |
| 146 | + sPtr->nodePtr = (struct node *) malloc(sizeof (struct node)); |
| 147 | + sPtr->nodePtr->row = row; |
| 148 | + sPtr->nodePtr->col = col; |
| 149 | + sPtr->nodePtr->val = value; |
| 150 | + |
| 151 | +//get the row head node |
| 152 | + rPtr = smat->frow; |
| 153 | + |
| 154 | +//move to corresponding row |
| 155 | + for (i = 0; i < row; i++) |
| 156 | + rPtr = rPtr->next; |
| 157 | + |
| 158 | +//traverse the nodes in current and locate new node |
| 159 | + n1 = rPtr->right; |
| 160 | + if (!n1) { |
| 161 | + rPtr->right = sPtr->nodePtr; |
| 162 | + sPtr->nodePtr->right = NULL; |
| 163 | + } else { |
| 164 | + while (n1 && n1->col < col) { |
| 165 | + n2 = n1; |
| 166 | + n1 = n1->right; |
| 167 | + } |
| 168 | + n2->right = sPtr->nodePtr; |
| 169 | + sPtr->nodePtr->right = NULL; |
| 170 | + } |
| 171 | + |
| 172 | +//get the column head node |
| 173 | + cPtr = sPtr->smatrix->fcol; |
| 174 | + |
| 175 | +//move to corresponding column |
| 176 | + for (i = 0; i < col; i++) |
| 177 | + cPtr = cPtr->next; |
| 178 | + |
| 179 | +//traverse the node in current column and locate new node in appropriate position |
| 180 | + n1 = cPtr->down; |
| 181 | + if (!n1) { |
| 182 | + cPtr->down = sPtr->nodePtr; |
| 183 | + sPtr->nodePtr->down = NULL; |
| 184 | + } else { |
| 185 | + while (n1 && n1->row < row) { |
| 186 | + n2 = n1; |
| 187 | + n1 = n1->down; |
| 188 | + } |
| 189 | + n2->down = sPtr->nodePtr; |
| 190 | + sPtr->nodePtr->down = NULL; |
| 191 | + } |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | +//create list for 3-Tuple representation |
| 196 | + void createList(struct sparse *sPtr) { |
| 197 | + int i, j = 0; |
| 198 | + for (i = 0; i < sPtr->row; i++) { |
| 199 | + insert(sPtr, sPtr->data[j], sPtr->data[j + 1], sPtr->data[j + 2]); |
| 200 | + j = j + 3; |
| 201 | + } |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | +//this function returns highest value of the matrix |
| 206 | +//in Highest function k is 3 times the no.of non zero elements of the matrix |
| 207 | +int Highest(struct sparse s,int k) |
| 208 | +{ if(k==0)//if number of non zero elements are 0 then highest number is 0 itself |
| 209 | + { |
| 210 | + return 0; |
| 211 | + } |
| 212 | + int i,maximum; |
| 213 | + maximum=s.data[2];//as indexes of l varies as 2,5,8,11,14......,k-4,k-1 for non zero values of the matrix |
| 214 | + //loop repeats for k/3 times as index i is increasing with speed 3 each time |
| 215 | + for(i=2;i<k;i++) |
| 216 | + { |
| 217 | + if(s.data[i]>=maximum) |
| 218 | + { |
| 219 | + maximum=s.data[i]; |
| 220 | + } |
| 221 | + i=i+2;//increase index by 3 each time |
| 222 | + } |
| 223 | + return maximum; |
| 224 | +} |
| 225 | + int main() |
| 226 | + { |
| 227 | + struct sparse s1,s2; |
| 228 | + int rows, cols,k; |
| 229 | + printf("\nPlease enter positive integer values for number of rows and columns of the matrix\n\n"); |
| 230 | + printf("Enter the number of rows of the matrix:"); |
| 231 | + scanf("%d",&rows); |
| 232 | + printf("\n"); |
| 233 | + printf("Enter the number of columns of the matrix:"); |
| 234 | + scanf("%d",&cols); |
| 235 | + printf("\n"); |
| 236 | + printf("Enter number of non zero elements of the matrix:"); |
| 237 | + scanf("%d",&k); |
| 238 | + printf("\n"); |
| 239 | + initialize(&s1, rows, cols); |
| 240 | + initialize(&s2, rows, cols); |
| 241 | + |
| 242 | + InputMatrix(&s1, rows, cols,k); |
| 243 | + printf("Randomly generated Matrix with %d rows and %d columns is:\n",rows,cols); |
| 244 | + displayInputMatrix(s1, rows, cols); |
| 245 | + printf("\n\n"); |
| 246 | +//s2 contains final output i.e. non zero elements with their indexes of the randomly generated matrix |
| 247 | + createThreeTuple(&s2,s1, rows, cols,k); |
| 248 | + createList(&s2); |
| 249 | +//in main function k is no.of non zero elements of the matrix |
| 250 | + printf("The Highest element in the sparse matrix is: %d\n\n",Highest(s2,3*k));//we are passing 3*k as argument because we have to check for indexes 2,5,8,.....3*k-4,3*k-1 |
| 251 | + return 0; |
| 252 | + } |
| 253 | +//Submitted by REDDI HAREESH-150101051-CSE Branch-Phone No:7664817751 |
0 commit comments