You cannot copy content of this page.

Author Archive admin

Sparse Polynomial representation and addition

Sparse Polynomial representation and addition:

Polynomial is an expression which is composed of terms, wherein terms are composed of coefficient and exponent.  An example of a polynomial is: 4x3+5x2+6x+9.  This polynomial is composed of four terms with the following sets of coefficient and exponent – {(4,3), (5,2), (6,1), (9,0)}.  Thus representation of a polynomial using arrays is straightforward.  The subscripts of the array may be considered as exponents and the coefficients may be stored at an appropriate place referred to by the subscript.  Array representation for the above example is:

arr:                9          6        5        4         coefficients

subscripts: 0         1        2         3          exponents

The above representation works fine for the above example, but for polynomials such as 5×6+7, array representation is considered feasible on account of memory usage, since it results in a sparse arrangement as follows:

 

arr    7         0         0        0        0        0          5

         0          1          2        3         4        5          6 (Subscripts)
Sparse matrix representation may be considered for the above matrix as given below:

Rows                     Cols                       Value
———————————————————–

1                              7                              2              (Total)

0                              0                              7

0                              6                              5

For addition of two polynomials using arrays, subscript-wise elements may be added to result in the polynomial containing result of addition of two polynomials.

Example:

Polynomial 1:     4x3 + 5x2 + 6x + 7

Polynomial 2:     3x3 + 4x2 + 2x + 2

———————–

7x3 + 9x2 + 8x + 9

———————–

Array Representation:

Subscripts:           0              1              2              3

———————————————————

Polynomial 1:     7              6              5              4

Polynomial 2:     2              2              4              3

Result of sum:   9              8              9               7

 
Program to represent two polynomials using arrays and compute their sum

Tags, ,

Program to represent Sparse Matrix using singly linked list (One dimensional list)

Sparse Matrix representation using singly Linked List:

/* Sparse Matrix representation using linked list */
#include <stdio.h>
#include <stdlib.h>

typedef struct list{
int rows, cols, value;
struct list *next;
}list;

list *create(){
list *temp = (list *)malloc(sizeof(list));
if(temp==NULL){
printf(“nMemory Allocation Error !”);
exit(1);
}
return temp;
}

list *makenode(int r, int c, int val){
list *temp = create();
temp->rows = r;
temp->cols = c;
temp->value = val;
temp->next = NULL;
return temp;
}

list *insert(list *head, int r, int c, int val){
list *ptr, *temp = head;
if(head == NULL){
head = makenode(r,c,val);
}
else{
while(temp->next != NULL)
temp = temp->next;
ptr = makenode(r,c,val);
temp->next = ptr;
}
return head;
}

void display(list *head){
list *temp;
if(head == NULL){
printf(“nList is empty.”);
exit(1);
}
temp = head;
while(temp != NULL){
printf(“(%d,%d,%d->)->”,temp->rows,temp->cols,temp->value);
temp = temp->next;
}
printf(“bb “);
}

int main(){
int arr[3][4],i,j,m,n,ct=0;
list *head = NULL;
for(i=0; i<3; i++){
printf(“nEnter the values for row %d?”, i+1);
for(j=0;j<4;j++){
scanf(“%d”,&arr[i][j]);
if(arr[i][j] != 0)
ct++;
}
}
head = makenode(3,4,ct);
for(i=0;i<3;i++){
for(j=0;j<4;j++){
if(arr[i][j] != 0)
head = insert(head,i,j,arr[i][j]);
}
}
printf(“nList representation of Sparse Matrix is: n”);
display(head);
getch();
}

Tags, ,

Program to represent Sparse Matrix using arrays

Program to represent Sparse Matrix using arrays:

/* Sparse Matrix representation using arrays */

#include <stdio.h>
#include <stdlib.h>

#define MAX 15

int main(){
int arr[3][4],i,j,m,n,nrows,ncols,ct=0;
int sparse_matrix[MAX][MAX];
for(i=0;i<3;i++){
printf(“nEnter values for row %d?”,i+1);
for(j=0;j<4;j++){
scanf(“%d”,&arr[i][j]);
if(arr[i][j] != 0)
ct++;
}
}
nrows = ct+1;
ncols = 3;
sparse_matrix[0][0] = 3;
sparse_matrix[0][1] = 4;
sparse_matrix[0][2] = ct;
m=1;
n=0;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
if(arr[i][j]!=0){
sparse_matrix[m][n++]=i;
sparse_matrix[m][n++]=j;
sparse_matrix[m][n]=arr[i][j];
m++;
n=0;
}
}
}
printf(“nThe new sparse matrix in 3-tuple representation is:n”);
for(i=0;i<nrows;i++){
for(j=0;j<ncols;j++){
printf(“%dt”,sparse_matrix[i][j]);
}
printf(“n”);
}
}

Tags, ,

Sparse Matrix

Sparse Matrix:

A matrix that has minimum number of non-zero elements is called a sparse matrix i.e. very few elements are sparsely distributed in the matrix.

Example: An array of order 3 x 4 containing sparsely located non-zero elements.

0

1

2

3

0

0

25

0

7

1

0

0

2

8

2

0

0

1

0

(Order 3 x 4)

In the above example, out of 12 elements only 5 elements contain non zero values.  Such a matrix is called sparse matrix.

Representation of Sparse Matrix:

  • 3-Tuple Representation
  • List Representation

1.  3-Tuple Representation: An array of three columns is required.  The size of the array is the number of non-zero elements plus one.  First row is called the header row that contains total number of rows, total number of columns and non-zero element’s value i.e.

Header Row:    Row    |      Column     |     Element

rows

columns

Elements (non zero)

0

3

4

5

1

0

1

25

2

0

3

7

3

1

2

2

4

1

3

8

5

2

2

1

Sparse Matrix representation for the array given above

Number of rows:  3

Number of columns:  4

Number of elements:  5

Order of Sparse Matrix = (5+1) x 3 = 6 x 3.

 

2.  List Representation:  Each row is converted to a node in linked representation where each node contains, row subscripts, column subscript and non-zero element.  The first node returns the total number of rows, columns and elements.

Example:

3|4|5 -> 0|1|25 -> 0|3|7 -> 1|2|2 -> 1|3|8 -> 2|2|1 -> NULL

Array representation of Sparse Matrix
Linked representation of Sparse Matrix

 

Tags,

Array Representation – Column-major & Row-major

Array Representation:

  • Column-major
  • Row-major

Arrays may be represented in Row-major form or Column-major form.  In Row-major form, all the elements of the first row are printed, then the elements of the second row and so on upto the last row.  In Column-major form, all the elements of the first column are printed, then the elements of the second column and so on upto the last column.  The ‘C’ program to input an array of order m x n and print the array contents in row major and column major is given below.  The following array elements may be entered during run time to test this program:

Input:     Rows: 3;  Columns: 3;

1     2     3

4     5     6

7     8     9

Output:

Row Major:

1     2     3

4     5     6

7     8     9

Column Major:

1     4     7

2     5     8

3     6     9

 



/* Program to input an array and display in row-major and column major form */

#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX][MAX],m,n,i,j;
printf(“nEnter total number of rows?”);
scanf(“%d”,&m);
printf(“nEnter total number of columns?”);
scanf(“%d”,&n);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf(“nEnter number?”);
scanf(“%d”,&arr[i][j]);
}
}
printf(“nnRow-Major Order:n”);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf(“%dt”,arr[i][j]);
}
printf(“n”);
}
printf(“nnColumn-Major Order:n”);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf(“%dt”,arr[j][i]);
}
printf(“n”);
}
}

Tags,

Program to delete a value from a given array such that array remains sorted

Sorted Deletion in an Array:

Deletion is same as in previous post with the only difference that if the given array is sorted, then the deletion automatically will result in a sorted array.

/* Sorted Deletion */

#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX],n,i,num;
printf(“nEnter total numbers?”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
printf(“nEnter number?”);
scanf(“%d”,&arr[i]);
}
printf(“nEnter the number to delete?”);
scanf(“%d”,&num);
for(i=0;i<n;i++){
if(arr[i] == num)
break;
}
if(i == n){
printf(“nNumber not found”);
return;
}
for(;i<n;i++)
arr[i] = arr[i+1];
printf(“nArray after deletion:”);
for(i=0;i<n-1;i++)
printf(“%dt”,arr[i]);
}

 

Tags

Program to delete a value from a given array from a given position

Deletion from a position in an array:

Deletion of a value is straightforward, since the elements from the given position are overwritten with the subsequent elements upto the last element, last element is initialized with zero and variable n indicating total number of elements is decremented by one.

/* Deletion from a position */

#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX],i,n,pos;
printf(“nEnter total numbers?”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
printf(“nEnter number?”);
scanf(“%d”,&arr[i]);
}
printf(“nEnter the position from where to delete element?”);
scanf(“%d”,&pos);
for(i=pos;i<n;i++)
arr[i] = arr[i+1];
arr[i] = 0;
n–;
printf(“nArray elements after deletion:”);
for(i=0;i<n;i++)
printf(“%dt”,arr[i]);
}

 

Tags

Program to insert a value in an array such that array remains sorted

Sorted insertion in an Array:

Whenever a new value is inserted, appropriate place is located to insert this value.  After locating that place, shifting of elements is required from that position to the last element by one position to the right.  Then the located position may be assigned the new element to be inserted.  The ‘C’ program for the same is given below:

/* Program for sorted insertion */
#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX]={0},n,i,j,k,num,ct=0;
printf(“nEnter total numbers?”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
printf(“nEnter number to insert?”);
scanf(“%d”,&num);
for(j=0;j<ct && arr[j]<=num;j++);
for(k=n-1;k>=j;k–)
arr[k+1] = arr[k];
arr[j] = num;
ct++;
}
printf(“nArray elements are: “);
for(i=0;i<n;i++)
printf(“%dt”,arr[i]);
}

Tags

Program to insert a value in an array at a given position

Insert a value at a given position in an array:

For inserting a value at a given position, the elements are shifted from the last element by one position to the right, and the element at the specified position is overwritten with the new value to be inserted.

/* Insertion at a position in an array */

#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX],n,i,pos,val;
printf(“nEnter total numbers?”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
printf(“nEnter number?”);
scanf(“%d”,&arr[i]);
}
printf(“nEnter the value to insert?”);
scanf(“%d”,&val);
printf(“nEnter the position where to insert?”);
scanf(“%d”,&pos);
for(i=n-1;i>=pos;i–)
arr[i+1] = arr[i];
arr[pos] = val;
printf(“nArray elements after insertion:”);
for(i=0;i<n+1;i++)
printf(“%dt”,arr[i]);
}

 

Tags

Program to perform Binary Search

Binary Search:

Binary Search involves reducing the search range to half by dividing the range into half of its original size.  Binary Search operates upon sorted array.  It compares the element at the mid of this range with the value to be searched, if the value is smaller than the mid value, then the value is looked up in the range from first element to mid, otherwise the new search range becomes mid to last element.  This process continues until the required element is located or lower bound becomes greater than upper bound.  Efficiency of Binary Search is O(log2n) in average and worst case and is O(1) in best case.  The ‘C’ program to perform Binary Search is given below:

 

/* Binary Search */
#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX],i,n,val;
int lb,ub,mid;
printf(“nEnter total numbers?”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
printf(“nEnter number?”);
scanf(“%d”,&arr[i]);
}
printf(“nEnter the value to search?”);
scanf(“%d”,&val);
lb=0;ub=n-1;
while(lb<=ub){
mid=(lb+ub)/2;
if(val<arr[mid])
ub = mid-1;
else if(val>arr[mid])
lb = mid+1;
else {
printf(“nNumber found…!”);
return;
}
}
printf(“nNumber not found…!”);
}

Tags

x Close

Like Us On Facebook