Sie sind auf Seite 1von 14

SPARSE MATRIX

AND
MULTI DIMENSIONAL ARRAY

PRESENTED BY:
SPARSE MATRIX
A matrix is a two-dimensional data object made of m rows and n
columns, therefore having total m x n values. If most of the elements of
the matrix have 0 value, then it is called a sparse matrix.

Conceptually, sparsity corresponds to systems with few pairwise


interactions. Consider a line of balls connected by springs from one to
the next: this is a sparse system as only adjacent balls are coupled. By
contrast, if the same line of balls had springs connecting each ball to all
other balls, the system would correspond to a dense matrix. The
concept of sparsity is useful in combinatorics and application areas
such as network theory , which have a low density of significant data or
connections.

Large sparse matrices often appear in


scientific , engineering applications when solving partial differential
equations.
STORING A SPARSE
MATRIX
A matrix is typically stored as a two-dimensional array. Each entry in
the array represents an element ai,j of the matrix and is accessed by
the two indices i and j. Conventionally, i is the row index, numbered
from top to bottom, and j is the column index, numbered from left to
right. For an m × n matrix, the amount of memory required to store the
matrix in this format is proportional to m × n (disregarding the fact
that the dimensions of the matrix also need to be stored).

In the case of a sparse matrix, substantial memory requirement


reductions can be realized by storing only the non-zero entries.
Depending on the number and distribution of the non-zero entries,
different data structures can be used and yield huge savings in
memory when compared to the basic approach. The trade-off is that
accessing the individual elements becomes more complex and
additional structures are needed to be able to recover the original
matrix unambiguously.
ADVANTAGES OF SPARSE MATRIX
Storage: There are lesser non-zero elements than zeros
and thus lesser memory can be used to store only those
elements.

Computing time: Computing time can be saved by


logically designing a data structure traversing only non-
zero elements..
SPARSE MATRIX
REPRESENTATIONS
Representing a sparse matrix by a 2D array leads to
wastage of lots of memory as zeroes in the matrix are of
no use in most of the cases. So, instead of storing
zeroes with non-zero elements, we only store non-zero
elements. This means storing non-zero elements
with triples- (Row, Column, value).

Sparse Matrix Representations can be done in many


ways following are two common representations:
Array representation
Linked list representation
USING ARRAYS

2D array is used to represent a sparse matrix in


which there are three rows named as
Row: Index of row, where non-zero element is
located
Column: Index of column, where non-zero element
is located
Value: Value of the non zero element located at
index – (row,column)
USING LINKED LIST
In linked list, each node has four fields. These four
fields are defined as:
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is
located
Value: Value of the non zero element located at index
– (row,column)
Next node: Address of the next node
MULTI DIMENSIONAL
ARRAY
In C/C++, we can define multidimensional arrays in
simple words as array of arrays. Data in
multidimensional arrays are stored in tabular form (in
row major order)

Programming languages such C,C++,java etc support


multi dimensional arrays. They occupy a large amount
of memory area but still they can be used to store a
large amount of data under a single variable name.
SIZE OF MULTI DIMENSIONAL
ARRAYS
Total number of elements that can be stored in a
multidimensional array can be calculated by multiplying the
size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000
elements.
DECLARING A MULTI
DIMENSIONAL ARRAY

In C/C++, initialization of a multidimensional arrays can


have left most dimension as optional. Except the left most
dimension, all other dimensions must be specified.

C programming language allows multidimensional arrays.


Here is the general form of a multidimensional array
declaration −
type name[size1][size2]...[sizeN]; For example, the
following declaration creates a three dimensional integer
array −
int threedim[5][10][4];
MULTI DIMENSIONAL ARRAY
PROGRAM
#include <stdio.h>
int main()
{
    int a[1000],i,n;
     printf("Enter size of array: ");
    scanf("%d",&n);
     printf("Enter %d elements in the array : ", n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    printf("\nElements in array are: ");
    for(i=0;i<n;i++)
    {
        printf("%d  ", a[i]);
    }
    return 0;
}
OUTPUT OF THE PROGRAM
Enter size of array: 5
 
Enter 5 elements in the array: 1
 
2
 
3
 
4
 
5
 
Elements in array are: 1  2  3  4  5
 
DISADVANTAGES OF MULTI
DIMENSIONAL ARRAY

1. We must know in advance that how many elements


are to be stored in multi dimensional array.
2. Array is static structure. It means that array is of fixed
size. The memory which is allocated to array can not be
increased or reduced.
3. Since multi dimensional arrays are of fixed size, if we
allocate more memory than requirement then the
memory space will be wasted. And if we allocate less
memory than requirement, then it will create problem.
4. The elements of multi dimensional array are stored in
consecutive memory locations. So insertions and
deletions are very difficult and time consuming.
THANK YOU

Das könnte Ihnen auch gefallen