ARRAYS
๏ถ Array is a consecutive set of memory locations and it's mainly used to store
similar data.
๏ถ It is a particular method of storing elements of indexed data. Elements of data are
stored sequentially in blocks within the array.
๏ถ Each element is referenced by an index, or subscript. The index is usually a
number used to address an element in the array.
๏ถ An array is a set of pairs, index and a value.
๏ถ For each index which is defined, there is a value associated with that index.
๏ถ In mathematical terms it is called as correspondence or mapping.
ARRAYS...
ARRAYS...
๏ถSimply, declaration of array is as follows:
int arr[10]
๏ถWhere int specifies the data type or type of elements
arrays stores.
๏ถโ€œarrโ€ is the name of array & the number specified
inside the square brackets is the number of elements
an array can store, this is also called sized or length
of array.
ARRAYS...
๏€ขFollowing are some of the concepts to be remembered about arrays:
๏€ขThe individual element of an array can be accessed by
specifying name of the array, following by index or
subscript inside square brackets.
๏€ขThe first element of the array has index zero[0]. It means
the first element and last element will be specified
as:arr[0] & arr[9]
Respectively.
ARRAYS...
๏€ขThe elements of array will always be
stored in the consecutive (continues)
memory location.
๏€ขThe number of elements that can be stored
in an array, that is the size of array or its
length is given by the following equation:
(Upperbound-lowerbound)+1
ARRAYS...
๏€ขFor the above array it would be
(9-0)+1=10,where 0 is the lower bound of array and 9 is
the upper bound of array.
๏€ขArray can always be read or written through loop. If we
read a one-dimensional array it require one loop for
reading and other for writing the array.
ARRAYS...
๏€ขFor example: Reading an array
For(i=0;i<=9;i++)
scanf(โ€œ%dโ€,&arr[i]);
๏€ขFor example: Writing an array
For(i=0;i<=9;i++)
printf(โ€œ%dโ€,arr[i]);
ARRAYS...
๏€ขIf we are reading or writing two-dimensional array it
would require two loops. And similarly the array of a N
dimension would required N loops.
๏€ขSome common operation performed on array are:
๏€ขCreation of an array
๏€ขTraversing an array
ARRAYS...
๏€ขInsertion of new element
๏€ขDeletion of required element
๏€ขModification of an element
๏€ขMerging of arrays
SPARSE MATRICES
๏ถ 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.
Why to use Sparse Matrix instead of simple 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 MATRICES......
๏ถ Example
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
๏ถ 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 MATRICES...
๏ถ The same time they are sparse: say only 1000 out of one million possible
elements are nonzero.
๏ถ On most computers today it would be impossible to store a full 1000 X 1000
matrix in the memory at once.
๏ถ Sparse Matrix Representations can be done in many ways following are two
common representations:
๏ถ Array representation
๏ถ Linked list representation
Array Representation
๏ถ 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)
Linked List Representation
๏ถ 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

data structures array and sparse matrics

  • 1.
    ARRAYS ๏ถ Array isa consecutive set of memory locations and it's mainly used to store similar data. ๏ถ It is a particular method of storing elements of indexed data. Elements of data are stored sequentially in blocks within the array. ๏ถ Each element is referenced by an index, or subscript. The index is usually a number used to address an element in the array. ๏ถ An array is a set of pairs, index and a value. ๏ถ For each index which is defined, there is a value associated with that index. ๏ถ In mathematical terms it is called as correspondence or mapping.
  • 2.
  • 3.
    ARRAYS... ๏ถSimply, declaration ofarray is as follows: int arr[10] ๏ถWhere int specifies the data type or type of elements arrays stores. ๏ถโ€œarrโ€ is the name of array & the number specified inside the square brackets is the number of elements an array can store, this is also called sized or length of array.
  • 4.
    ARRAYS... ๏€ขFollowing are someof the concepts to be remembered about arrays: ๏€ขThe individual element of an array can be accessed by specifying name of the array, following by index or subscript inside square brackets. ๏€ขThe first element of the array has index zero[0]. It means the first element and last element will be specified as:arr[0] & arr[9] Respectively.
  • 5.
    ARRAYS... ๏€ขThe elements ofarray will always be stored in the consecutive (continues) memory location. ๏€ขThe number of elements that can be stored in an array, that is the size of array or its length is given by the following equation: (Upperbound-lowerbound)+1
  • 6.
    ARRAYS... ๏€ขFor the abovearray it would be (9-0)+1=10,where 0 is the lower bound of array and 9 is the upper bound of array. ๏€ขArray can always be read or written through loop. If we read a one-dimensional array it require one loop for reading and other for writing the array.
  • 7.
    ARRAYS... ๏€ขFor example: Readingan array For(i=0;i<=9;i++) scanf(โ€œ%dโ€,&arr[i]); ๏€ขFor example: Writing an array For(i=0;i<=9;i++) printf(โ€œ%dโ€,arr[i]);
  • 8.
    ARRAYS... ๏€ขIf we arereading or writing two-dimensional array it would require two loops. And similarly the array of a N dimension would required N loops. ๏€ขSome common operation performed on array are: ๏€ขCreation of an array ๏€ขTraversing an array
  • 9.
    ARRAYS... ๏€ขInsertion of newelement ๏€ขDeletion of required element ๏€ขModification of an element ๏€ขMerging of arrays
  • 10.
    SPARSE MATRICES ๏ถ Amatrix 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. Why to use Sparse Matrix instead of simple 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..
  • 11.
    SPARSE MATRICES...... ๏ถ Example 00 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 ๏ถ 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).
  • 12.
    SPARSE MATRICES... ๏ถ Thesame time they are sparse: say only 1000 out of one million possible elements are nonzero. ๏ถ On most computers today it would be impossible to store a full 1000 X 1000 matrix in the memory at once. ๏ถ Sparse Matrix Representations can be done in many ways following are two common representations: ๏ถ Array representation ๏ถ Linked list representation
  • 13.
    Array Representation ๏ถ 2Darray 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)
  • 14.
    Linked List Representation ๏ถ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