0% found this document useful (0 votes)
29 views4 pages

CSC 221 Notes - 1

Linear data structures arrange elements sequentially and allow single pass traversal, while non-linear structures connect elements with multiple paths. Sparse matrices contain mostly zero values and use less memory by storing only non-zero elements and their indices.

Uploaded by

oyinlola israel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

CSC 221 Notes - 1

Linear data structures arrange elements sequentially and allow single pass traversal, while non-linear structures connect elements with multiple paths. Sparse matrices contain mostly zero values and use less memory by storing only non-zero elements and their indices.

Uploaded by

oyinlola israel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FUNDAMENTALS OF DATA STRUCTURES

Linear and Non-linear Data Structures_Notes_1

Linear Data Structures

A Linear data structure has data elements arranged in sequential manner


and each member element is connected to its previous and next element.
This connection helps to traverse a linear data structure in a single level
and in single run. Such data structures are easy to implement as computer
memory is also sequential. Examples of linear data structures are List,
Queue, Stack, Array etc.

Non-linear Data Structures

A non-linear data structure has no set sequence of connecting all its


elements and each element can have multiple paths to connect to other
elements. Such data structure supports multi-level storage and often
cannot be traversed in single run. Such data structures are not easy to
implement but are more efficient in utilizing computer memory. Examples of
non-linear data structures are Tree, BST, Graphs etc.

Ordered List

The structure of an ordered list is a collection of items where each item


holds a relative position that is based upon some underlying characteristic
of the item. The ordering is typically either ascending or descending and we
assume that list items have a meaningful comparison operation that is
already defined.

The Ordered List Abstract Data Type

We will now consider a type of list known as an ordered list. For example, if
the list of integers shown above were an ordered list (ascending order),
then it could be written as 17, 26, 31, 54, 77, and 93. Since 17 is the
smallest item, it occupies the first position in the list. Likewise, since 93 is
the largest, it occupies the last position.

The structure of an ordered list is a collection of items where each item


holds a relative position that is based upon some underlying characteristic
of the item. The ordering is typically either ascending or descending and we
assume that list items have a meaningful comparison operation that is
already defined. Many of the ordered list operations are the same as those
of the unordered list.

 OrderedList() creates a new ordered list that is empty. It needs no


parameters and returns an empty list.
 add(item) adds a new item to the list making sure that the order is
preserved. It needs the item and returns nothing. Assume the item is
not already in the list.
 remove(item) removes the item from the list. It needs the item and
modifies the list. Assume the item is present in the list.
 search(item) searches for the item in the list. It needs the item and
returns a boolean value.
 isEmpty() tests to see whether the list is empty. It needs no
parameters and returns a boolean value.
 size() returns the number of items in the list. It needs no parameters
and returns an integer.
 index(item) returns the position of item in the list. It needs the item
and returns the index. Assume the item is in the list.
 pop() removes and returns the last item in the list. It needs nothing
and returns an item. Assume the list has at least one item.
 pop(pos) removes and returns the item at position pos. It needs the
position and returns the item. Assume the item is in the list.

What is Sparse Matrix?

In computer programming, a matrix can be defined with a 2-dimensional


array. Any array with 'm' columns and 'n' rows represent a m X n matrix.
There may be a situation in which a matrix contains more number of ZERO
values than NON-ZERO values. Such matrix is known as sparse matrix.

When a sparse matrix is represented with a 2-dimensional array, we waste


a lot of space to represent that matrix. For example, consider a matrix of
size 100 X 100 containing only 10 non-zero elements. In this matrix, only
10 spaces are filled with non-zero values and remaining spaces of the
matrix are filled with zero. That means, totally we allocate 100 X 100 X 2 =
20000 bytes of space to store this integer matrix. And to access these 10
non-zero elements we have to make scanning for 10000 times. To make it
simple we use the following sparse matrix representation.

Sparse Matrix Representations

A sparse matrix can be represented by using TWO representations, those


are as follows...
1. Triplet Representation (Array Representation)
2. Linked Representation

Triplet Representation (Array Representation)

In this representation, we consider only non-zero values along with their


row and column index values. In this representation, the 0th row stores the
total number of rows, total number of columns and the total number of non-
zero values in the sparse matrix.

For example, consider a matrix of size 5 X 6 containing 6 number of non-


zero values. This matrix can be represented as shown in the image...

In above example matrix, there are only 6 non-zero elements ( those are 9,
8, 4, 2, 5 & 2) and matrix size is 5 X 6. We represent this matrix as shown
in the above image. Here the first row in the right side table is filled with
values 5, 6 & 6 which indicates that it is a sparse matrix with 5 rows, 6
columns & 6 non-zero values. The second row is filled with 0, 4, & 9 which
indicates the non-zero value 9 is at the 0th-row 4th column in the Sparse
matrix. In the same way, the remaining non-zero values also follow a
similar pattern.

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..

Example:

00304
00570
00000
02600

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:

1. Array representation
2. Linked list representation

Method 1: 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)

You might also like