0% found this document useful (0 votes)
18 views60 pages

Unit 1 Sequential Organization

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)
18 views60 pages

Unit 1 Sequential Organization

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/ 60

SEQUENTIAL

ORGANIZATION
CONCEPT OF SEQUENTIAL
ORGANIZATION:
• It mean data is stored in sequential form, in consecutive
memory locations.
• Ex. Array
• There are two basic operations performed on this data.
i. Storing data at desired location.
ii. Retrieving data from desired location.
LINEAR DATA STRUCTURE

• Data structure in which data is arranged in a list or straight


sequence.
• Adjacency relation or one-to-one relationship can be
represented using linear data structure
• Eg- Array and linked list
ORDERED LIST

• Manipulated by position
• Linear list
• Eg- days of week, digits etc.
• integer = {0, +1, -1, +2, -2, +3, -3, …}
• daysOfWeek = {S,M,T,W,Th,F,Sa}
• Data structures used: Array(static & dynamic) & linked list
• Operations: Display, Search, Insert, Delete
ORDERED LISTS

• a sorted list is a “value oriented list”,


• ordered list is: ordered by “position” i.e. this is a “position
oriented list”
• lists ordered by time,
• grocery lists as in the order we think about it
• to-do lists: In priority order…
ABSOLUTE ORDERED LIST

– may have holes


– which means if the first data inserted is at position 12, there
are 11 holes (1-11) prior
– may “replace” data if inserted at the same position
– similar to “forms” such as a tax form
- Insertion & deletion does not affect the remaining list.
- Array is suitable data structure
RELATIVE ORDERED LIST

– may not have holes


– which means if the first data inserted is at position 12, it would
actually be inserted at the first position!
– may “shift” data if inserted at the same position
– similar to “editors” such as vi
- Insertion & deletion affects the remaining list.
- Linked list is suitable data structure
ARRAYS

• Consecutive memory locations.


• Advantage
• Index makes storage and retrieval efficient
• Search is easy as continuous memory allocation
• Disadvantage
• Insertion & deletion is complicated
• Continuous free blocks are necessary
STORAGE REPRESENTATION FOR
ARRAYS:
• One dimensional array Ex. int a[10];
a

0 10
Value
1 20 stored in
Index array
used to 2 30
find 3 40
element
.
.
.
9
ARRAYS

Imagine that we have 100 scores. We need to read them,


process them and print them. We must also keep these
100 scores in memory for the duration of the program.
We can define a hundred variables, each with a different
name, as shown in Figure.

A hundred individual variables


But having 100 different names creates other problems. We
need 100 references to read them, 100 references to process
them and 100 references to write them.

Figure Processing individual variables


An array is a sequenced collection of elements, normally of
the same data type. We can refer to the elements in the array
as the first element, the second element and so forth, until we
get to the last element.

Figure Arrays with indexes


We can use loops to read and write the elements in an array.
We can also use loops to process elements. Now it does not
matter if there are 100, 1000 or 10,000 elements to be
processed—loops make it easy to handle them all. We can
use an integer variable to control the loop and remain in the
loop as long as the value of this variable is less than the total
number of elements in the array
Figure Processing an array
Application
Thinking about the operations discussed in the previous
section gives a clue to the application of arrays. If we have a
list in which a lot of insertions and deletions are expected
after the original list has been created, we should not use an
array. An array is more suitable when the number of
deletions and insertions is small, but a lot of searching and
retrieval activities are expected.

An array is a suitable structure when a small number of


insertions and deletions are required, but a lot of
searching and retrieval is needed.
Example
Compare the number of instructions needed to handle 100
individual elements and the array with 100. Assume that
processing each score needs only one instruction.

Solution
❑ In the first case, we need 100 instructions to read, 100
instructions to write and 100 instructions to process. The
total is 300 instructions.
❑ In the second case, we have three loops. In each loop we have
two instructions, for a total of six instructions. However, we
also need three instructions for initializing the index and three
instructions to check the value of the index. In total, we have
twelve instructions.
Example
The number of cycles (fetch, decode, and execute phases) the
computer needs to perform is not reduced if we use an array. The
number of cycles is actually increased, because we have the extra
overhead of initializing, incrementing and testing the value of the
index. But our concern is not the number of cycles: it is the
number of lines we need to write the program.
Array name versus element name
In an array we have two types of identifiers: the name of the
array and the name of each individual element. The name of
the array is the name of the whole structure, while the name
of an element allows us to refer to that element.

In the array, the name of the array is scores and name of


each element is the name of the array followed by the index,
for example, scores[1], scores[2], and so on.
Multi-dimensional arrays
The arrays discussed so far are known as one-dimensional
arrays because the data is organized linearly in only one
direction. Many applications require that data be stored in
more than one dimension. Figure shows a table, which is
commonly called a two-dimensional array.

Figure A two-dimensional array


Memory layout
The indexes in a one-dimensional array directly define the
relative positions of the element in actual memory. Figure
shows a two-dimensional array and how it is stored in
memory using row-major or column-major storage.
Row-major storage is more common.

Figure Memory layout of arrays


ROW MAJOR REPRESENTATION
• If the elements are stored in row wise manner then it is
called “Row Major Representation”.
• Ex. If we want to store elements
• 10 20 30 40 50 60 then
• In a 2D array

0 1 2

0 10 20 30
1 40 50 60
.
.
9
COLUMN MAJOR REPRESENTATION
• If the elements are stored in column wise manner then it is
called “Column Major Representation”.
• Ex. If we want to store elements
• 10 20 30 40 50 60 then elements will be filled up by column
wise manner (consider array a[3][2])
• In a 2D array
0 1

0
10 40
1
20 50
2
30 60
• Each element is occupied at successive location if the
element is of integer type then 2 bytes of memory
will be allocated.
• If it is float then 4 bytes of memory will be allocated.
• Ex.
• int a[3][2]={ {10,20}
{30,40}
{50,60} }
• Then in a row major matrix
a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a [2][1]

10 20 30 40 50 60

100 102 104 106 108 110


And in the column major matrix
a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a [2][1]

10 30 50 20 40 60

100 102 104 106 108 110


• Address calculation for any element will be as follows
• In row major matrix, the element a[i][j] will be
base address + (i * total number of columns + j)* size of data type.

In column major matrix, the element a[i][j] will be


base address +( j * total number of rows + i)* size of data type
ORDERED LIST

• Ordered list is a set of elements where set may be empty or it


can be written as a collection of elements such as
(a1,a2,a3……..an) a list sometimes called as linear list.
POLYNOMIALS

• One classic example of an ordered list is a polynomials.


• Def.-
• A polynomial is the sum of terms where each terms consists of variable,
coefficient and exponent.
• Various operations –
• Addition of two polynomials
• Multiplication
• Evaluation
POLYNOMIAL REPRESENTATION

one dimensional array where:


// an array of struct
index represents the exponent
#define MAXSIZE 10
and the stored value is the
typedef struct poly {
corresponding coefficient
real coeff;
OR int expo;

2x8 + 4x2 + 1 }term;


term poly1[MAXSIZE];
0 1 2 3 4 5 6 7 8 9 term poly2[MAXSIZE];
1 4 2 term poly3[MAXSIZE];
DRAWBACKS OF USING ARRAY:

1. If the exponent is too large then unnecessary the size of


7x999-10. Scanning such array
array will be more. Ex.
will be time consuming.
2. Wastage of space.
3. Can not decide what the array size should be.
• Ex.
• 3x4+5x3+7x2+10x-19

• This type of representation of polynomials is suitable if, the upper limit


on exponent value is reasonably high
• Actual number or terms are closer to this limiting value.
POLYNOMIAL REPRESENTATION

• By using structure.
• 2 main advantages
• 1. There is no limit on maximum value of the exponent.
• 2. It requires less number of terms though there is vast
difference in max & min value of exponent.
• Ex.
typedef struct ploy
{
int coeff;
int expo;
}p;
p p1[10];
Ex. 7x999-10
Coeff expo
0
7 999
1 -10 0
.
.
.
9
POLYNOMIAL ADDITION

• Take polynomial A & B


3x3+2x2+x+1
+ 5x3+7x

Add coef of similar power terms


POLYNOMIAL MULTIPLICATION

• Ex.
• If two polynomial are given as
3x3+2x2+x+1
x 5x3+7x

• Multiply coef and add exponent


• Add coef of similar power terms
POLYNOMIAL EVALUATION

• Consider the polynomial for evaluation as


• -10x7+4x5+3x2

Algorithm:
Step1: Read the polynomial array A.
Step2: Read the value of x.
Step3: Initialize the variable sum to zero.
Step 4: Then calculate coeff * pow (x, exp) of each term and
add the result to sum.
Step 5: Display sum.
Step 6 : stop
POLYNOMIAL – ABSTRACT DATA TYPE (ADT)

Object: Polynomial.

Operations:
◦ Boolean IsZero(poly)
::= return FALSE or TRUE.

◦ Coefficient Coeff(poly, expon)


::= return coefficient of xexpon

◦ Polynomial Add(poly1, poly2)


::= return poly1 + poly2

◦ Polynomial Subtract(poly1, poly2)


::= return poly1 - poly2
SPARSE MATRICES
◆ An example sparse matrix:
15 0 0 22 0 -15
0 11 3 0 0 0
A= 0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0

● A lot of “zero” entries.


Thus large memory space is wasted.

● Could we use other representation to save


memory space ??
37
REPRESENTATION FOR SPARSE
MATRICES
• Use triple <row, col, value> to characterize an
element in the matrix.

• Use array of triples a[] to represent a matrix.


row col value
• row by row
a[0] 6 6 8
a[1] 0 0 15
• within a row, a[2] 0 3 22
column by column a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
SPARSE MATRICES

• Definition
• Sparse matrix is that matrix which has a very few non zero
elements.
• Representation
• Ex.
• Suppose a matrix is 6X7 & number of non-zero elements are say 8
then representation will be
Index Row No Column value
No
0 6 7 8
1 0 6 -10
2 1 0 55
3 2 5 -23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 -28
8 5 0 99
Sparse Matrices

col1 col2 col3 col4 col5 col6


row0

row1

row2

row3

row4

row5

5*3 6*6
15/15 8/36
sparse matrix
data structure?
SPARSE MATRIX REPRESENTATION
• In normal Matrix –
• Space will be 6*6*2=72 bytes

• In sparse matrix –
• (TOTAL NO. OF NON-ZERO VALUE +1)*3*2
(8+1)*3*2=9*6 =54 bytes
Sparse matrix representation saves (72-54=18 bytes) of
memory.
.
REPRESENTATION FOR SPARSE
MATRICES

typedef struct {
int col, row, value;
} term;

term a[MAX_TERMS];
READ SPARSE

cout<<"\n Enter the size of matrix (rows,columns)”;


cin>>m>>n;
a[0][0]=m;
a[0][1]=n;

cout<<"\nEnter no. of non-zero elements:”;


cin>>t;
a[0][2]=t;
for(i=1; i<=t; i++)
{
cout<<"\n Enter the next triple(row,column,value) :”;
cin>>a[i][0]>>a[i][1]>>a[i][2];
}
DISPLAY SPARSE

n=a[0][2]; //no of 3-triples


cout<<"\nrows”<<a[0][0]<<“columns”<<[0][1]<<“Valu
es<<a[0][2]”;
cout<<"\n”;
for(i=1;i<=n;i++)
cout<<a[i][0]<<a[i][1]<<a[i][2]);
ADDITION OF SPARSE MATRIX

• Conventions
• Order of 2 matrices are same.
• If there is an element at <i ,j> in one matrix & also an element at the
position in another matrix then add two elements & store the sum in
resultant matrix.
• Otherwise copy both the elements in resulting matrix.
ALGORITHM:

• 1. Start
• 2.Read two sparse matrices SP1 and SP2
• 3.The index of SP1 and SP2 will be i=1,j=1 resp. Non zero
elements are t1 and t2 for SP1 and SP2.
• 4.The k=1 index will be for sparse matrix SP3 which will store
the addition of two matrices.
• 5. SP3[0][0]=SP1[0][0]
• SP3[0][1]=SP1[0][1]
Row Col Val Row Col Val
3 3 4 3 3 4
0 0 1 0 0 1
1 0 2 0 1 2
2 0 3 1 0 3
2 1 4 2 1 4

SP1 SP2
Row Col Val
3 3 5
0 0 2
0 1 2
1 0 5
2 0 3
2 1 8
SP3
OPERATIONS: TRANSPOSE
• c transpose(a) // a: m x n matrix

//Algorithm 1:
for each row i {
take element (i, j, value) and
store it as (j, i, value).
}
row col value row col value
• Eg.
a[0] 6 6 8 c[0] 6 6 8
a[1] 0 0 15 c[1 ] 0 0 15
a[2] 0 3 22 c[2] 3 0 22
a[3] 0 5 -15 c[3] 5 0 -15
a[4] 1 1 11 c[4] 1 1 11
a[5] 1 2 3 c[5] 2 1 3
a[6] 2 3 -6 c[6] 3 2 -6
a[7] 4 0 91 c[7] 0 4 91
a[8] 5 2 28 c[8] 2 5 28
Index Row No Column value
No
0 6 7 8
Find Transpose 1 0 6 -10
of this sparse
matrix. 2 1 0 55
3 2 5 -23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 -28
8 5 0 99
Index Row No Column value
No
0 7 6 8
1 0 1 55
2 0 5 99
3 1 3 67
4 3 4 14
5 4 4 -28
6 5 2 -23
7 6 0 -10
8 6 3 88
OPERATIONS: TRANSPOSE
Problem: If we just place them consecutively, we need to
do a lot of insertions to make the ordering right.

row col value

c[0] 6 6 8
c[1 ] 0 0 15
c[2] 3 0 22
c[3] 5 0 -15
c[4] 1 1 11
c[5] 2 1 3
c[6] 3 2 -6
c[7] 0 4 91
c[8] 2 5 28
ALG. 2 FOR TRANSPOSE
• Algorithm 2:
• Find all elements in col. 0, and store them in row 0;
Find all elements in col. 1, and store them in row 1; … … … …
etc

row col value row col value

a[0] 6 6 8 c[0] 6 6 8
a[1 ] 0 0 15 c[1 ] 0 0 15
a[2] 0 3 22 c[2] 0 4 91
a[3] 0 5 -15 c[3] 1 1 11
a[4] 1 1 11 c[4] 2 1 3
a[5] 1 2 3 c[5] 2 5 28
a[6] 2 3 -6 c[6] 3 0 22
a[7] 4 0 91 c[7] 3 2 -6
a[8] 5 2 28 c[8] 5 0 -15
ALG. 2 FOR TRANSPOSE
Algorithm 2 :
for (j=0; j<#col; j++) { //O(#col)
Running time = O(#col x #terms)
for all elements in col j { //O(#terms)
place element (i, j, value) in the
next position of array c[];
}
}
SIMPLE TRANSPOSE
B[0][0]=A[0][1];
B[0][1]=A[0][0];
B[0][2]=A[0][2];
noterms=A[0][2];
noc=A[0][1];
if(A[0][2]>1)
{
nxt=1;
for(c=0;c<noc;c++)//loop till col number of nonzero elements
{ for(Term=1;Term<=noterms;Term++)// loop till we have nonzero
elements
{
/* if a column number of current triple == c, then insert the current triple in B */

if(A[Term][1]== c)
{
B[nxt][0]=A[Term][1];
B[nxt][1]=A[Term][0];
B[nxt][2]=A[Term][2];
nxt++;
}
}
}
}
COMPLEXITY OF SIMPLE TRANSPOSE

• O(No.of columns* No. of terms)


• Fast Transpose:
• Determine the number of elements in each column of the
original matrix.
• ==>
• Determine the starting positions of each row in the transpose
matrix.
a[0] 6 6 8
a[1] 0 0 15
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
[0] [1] [2] [3] [4] [5]
ROW_TERMS = 2 1 2 2 0 1
STARTING_POS =1 3 4 6 8 8
CHAPTER 2 58
VOID FAST_TRANSPOSE(TERM A[ ], TERM B[ ])
{
/* THE TRANSPOSE OF A IS PLACED IN B */
INT ROW_TERMS[MAX_COL],
STARTING_POS[MAX_COL];
INT I, J, NUM_COLS = A[0].COL, NUM_TERMS =
A[0].VALUE;
B[0].ROW = NUM_COLS; B[0].COL = A[0].ROW;
B[0].VALUE = NUM_TERMS;
columns IF (NUM_TERMS > 0){ /*NONZERO MATRIX*/
FOR (I = 0; I < NUM_COLS; I++) //INITIALISE TO 0.
elements ROW_TERMS[I] = 0;
FOR (I = 1; I <= NUM_TERMS; I++)
ROW_TERM [A[I].COL]++
STARTING_POS[0] = 1;
columns
FOR (I =1; I < NUM_COLS; I++)
STARTING_POS[I]=STARTING_POS[I-1]
+ROW_TERMS [I-1];
CHAPTER 2 59
FOR (I=1; I <= NUM_TERMS, I++) {
J = STARTING_POS[A[I].COL];
B[J].ROW = A[I].COL;
B[J].COL = A[I].ROW;
elements
B[J].VALUE = A[I].VALUE;
STARTING_POS[A[I].COL]++;

}
}
}

CHAPTER 2 60

You might also like