0% found this document useful (0 votes)
32 views

Chapter 05

The document discusses linear data structures using sequential organization. It covers concepts of sequential organization and linear data structures like arrays and lists. It describes storage representations of multidimensional arrays in row-major and column-major forms and the address calculation for elements. The document also discusses representation of sparse matrices using arrays and algorithms for operations like addition, transpose, and polynomial representation. It introduces stacks and queues and their implementation. Finally, it provides details on the row-major and column-major storage of arrays and the address calculation for elements in arrays.
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)
32 views

Chapter 05

The document discusses linear data structures using sequential organization. It covers concepts of sequential organization and linear data structures like arrays and lists. It describes storage representations of multidimensional arrays in row-major and column-major forms and the address calculation for elements. The document also discusses representation of sparse matrices using arrays and algorithms for operations like addition, transpose, and polynomial representation. It introduces stacks and queues and their implementation. Finally, it provides details on the row-major and column-major storage of arrays and the address calculation for elements in arrays.
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/ 86

UNIT V: LINEAR DATA STRUCTURES USING

SEQUENTIAL ORGANIZATION (8 HRS)


 Concept of sequential organization, Concept of
Linear data structures, Concept of ordered list,
 Multidimensional arrays and their storage
representation: row major and column major
form and address calculation.
 Representation of sparse matrix using arrays,
algorithms for sparse matrix addition, simple
and fast transpose, polynomial representation
using arrays .Analysis of these algorithms.
 Introduction to Stack and Queue, and their
implementation using sequential organization,
use of stack in recursion.
STORAGE OF ARRAYS
 Row Major
 Column Major
ADDRESS CALCULATION FOR ARRAYS
In computing, row-major order and column-
major order describe methods for arranging
multidimensional arrays in linear storage
such as memory.
 In row-major order, consecutive elements

of the rows of the array are in contiguous


memory; in column-major order,
consecutive elements of the columns are
contiguous.
a11 a12 a13
 A= a21 a22 a23

 Column Major Row Major

Address Value Address Value

0 a11 0 a11
1 a21 1 a12
2 a12 2 a13
3 a22 3 a21
4 a13 4 a22
5 a23 5 a23
ROW MAJOR ORDER
The Location of element A[i, j] can be obtained by
evaluating expression: 
LOC (A [i, j]) = Base_Address + W [M (i) + (j)]

Base_Address is the address of frst element in the array. 


W is the word size. bytes occupied by each element. 
N is number of rows in array. 
M is number of columns in array. 
 Base_Address = 2000, W= 2, M=4, N=2, i=1, j=2

 LOC (A [i, j])=Base_Address + W [M (i) + (j)]

LOC (A[1, 2])=2000 + 2 *[4*(1) + 2]


=2000 + 2 * [4 + 2]
=2000 + 2 * 6
=2000 + 12
=2012
COLUMN MAJOR
 The Location of element A[i, j] can be obtained by evaluating expression: 
LOC (A [i, j]) = Base_Address + W [N (j) + (i)]

Base_Address is the address of first element in the array. 


W is the word size. bytes occupied by each element. 
N is number of rows in array. 
M is number of columns in array. 
Suppose we want to calculate the address of element A [1, 2].

Base_Address = 2000, W= 2, M=4, N=2, i=1, j=2


 LOC (A [i, j])=Base_Address + W [N (j) + (i)]

 LOC (A[1, 2])=2000 + 2 *[2*(2) + 1]

 =2000 + 2 * [4 + 1]
 =2000 + 2 * 5
=2000 + 10
 =2010
SPARSE MATRIX
sparse … many elements are zero
dense … few elements are zero
  a sparse matrix is a matrix in which most
of the elements are zero.
 

 Application areas
 Network theory, which have a low density of
signifcant data or connections.
 scientifc or engineering applications.

Computer Graphics

Recommendor System

Machine Learning

Information Retrieval
EXAMPLE

-2 1 0 0 0
1 -2 1 0 0
0 1 -2 1 0
0 0 1 -2 1
0 0 0 1 -2
A minimal set of operations includes matrix
creation, addition, multiplication, and transpose.
REPRESENTATION OF CONVENTIONAL
INTO SPARSE MATRIX
 2D array

Declaration Sparse_arr[Max][3]

 Array of Structure
DECLARATION OF SPARSE MATRIX
ALGORITHM CONVERSION OF
CONVENTIONAL INTO SPARSE
MATRIX
 Precondition : declare array of structure
Spar[Max].
 Accept conventional matrix A with minimal

nonzer0 elements.
 Initialize k=1;

 For i=0 to row


 For j=0 to column
if( A[i][j] !=0)
store i in spar[k].row
store j in spar[k].col
store A[i][j] in spar[k].val
 Spar[0].row = row, Spar[0].col = column,
Spar[0].val = k
TRANSPOSE OF SPARSE
MATRIX
(1) for each row i
take element <i, j, value> and store it
in element <j, i, value> of the transpose.

difficulty: where to put <j, i, value>


(0, 0, 15) ====> (0, 0, 15)
(0, 3, 22) ====> (3, 0, 22)
(0, 5, -15) ====> (5, 0, -15)
(1, 1, 11) ====> (1, 1, 11)
Move elements down very often.

(2) For all elements in column j,


place element <i, j, value> in element <j, i,
value>
SIMPLE
TRANSPOSE
FAST TRANSPOSE
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
a[0] 6 6 8
for (i = 1; i <= num_terms; i++)
a[1] 0 0 15
row_term [a[i].col]++
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11 [0] [1] [2] [3] [4] [5]
a[5] 1 2 3 row_terms = 0 0 0 0 0 0
a[6] 2 3 -6
[0] [1] [2] [3] [4] [5]
a[7] 4 0 91
row_terms = 1 0 0 0 0 0
a[8] 5 2 28
[0] [1] [2] [3] [4] [5]
row_terms = 1 0 0 1 0 0

[0] [1] [2] [3] [4] [5]


25
row_terms = 1 0 0 1 0 1
A[0] 6 6 8
a[1] 0 0 15 [0] [1] [2] [3] [4] [5]
a[2] 0 3 22 row_terms = 1 1 0 1 0 1
A[3] 0 5 -15
A[4] 1 1 11 [0] [1] [2] [3] [4] [5]
a[5] 1 2 3 row_terms = 1 1 1 1 0 1
a[6] 2 3 -6
a[7] 4 0 91 [0] [1] [2] [3] [4] [5]
A[8] 5 2 28 row_terms = 1 1 1 2 0 1

[0] [1] [2] [3] [4] [5]


row_terms = 2 1 1 2 0 1

[0] [1] [2] [3] [4] [5] 26


row_terms = 2 1 2 2 0 1
a[0] 6 6 8 starting_pos[0] = 1;
a[1] 0 0 15 for (i =1; i < num_cols; i++)
a[2] 0 3 22 starting_pos[i]=starting_pos[i-1] +row_terms [i-
a[3] 0 5 -15 1];
a[4] 1 1 11
a[5] 1 2 3 [0] [1] [2] [3] [4] [5]
a[6] 2 3 -6 starting_pnt = 1
a[7] 4 0 91
a[8] 5 2 28

[0] [1] [2] [3] [4] [5]


row_terms = 2 1 2 2 0 1

[0] [1] [2] [3] [4] [5]


starting_pnt = 1
3
FOR (I =1; I < NUM_COLS; I++)
STARTING_POS[I]=STARTING_POS[I-1]
+ROW_TERMS [I-1]; [0] [1] [2] [3]
[4] [5]
starting_pnt = 1 3
[0] [1] [2] [3]
[4] [5]
row_terms = 2 1 2 2 0 1

[0] [1] [2] [3] [4]


[5]
4
starting_pnt = 1 3

[0] [1] [2] [3] [4] [5]


starting_pnt = 1 3 4 6 8 288
for (i=1; i <= num_terms, i++) {
j = starting_pos[a[i].col] [0] [1] [2] [3] [4] [5]
copy a[i] b[j] starting_pnt = 1 3 4 6 8 8
starting_pos[a[i].col]++; 2 4 5 8
7 9
}

a[0] 6 6 8 j=
j =starting_pnt[a[1].col]
starting_pnt[a[6].col] b[0] 6 6 8
a[1] 0 0 15 =starting_pnt[0]
= starting_pnt[3] b[1]
a[2] 0 3 22 =1= 7 b[2]
a[3] 0 5 -15 b[3] 0 0 15
a[4] 1 1 11 b[4]
a[5] 1 2 3 j =j =
starting_pnt[a[2].col]
starting_pnt[a[7].col] b[5] 0 4 91
a[6] 2 3 -6 == starting_pnt[3]
starting_pnt[0] b[6]
a[7] 4 0 91 == 6 2 b[7] 1 1 11
a[8] 5 2 28 b[8]
j =j =
starting_pnt[a[3].col]
starting_pnt[a[8].col] 2 1 3
== starting_pnt[5]
starting_pnt[2]
2 5 28
== 8 5

j = starting_pnt[a[4].col] 3 0 22
= starting_pnt[1]
3 2 -6
=3
5 0 -15
j = starting_pnt[a[5].col]
= starting_pnt[2]
=4
ADDITION OF TWO SPARSE
MATRICES
Pre condition: Declare sparse matrix A,  if(A[i].row
sparse matrix B, sparse matrix C
== B[j].row
Post condition : resultant matrix (addition of
&& A[i].col > B[j].col)
A,B) 
 C[k] = B[j]
 j++, k++

Algorithm : Sparse_addition  if(A[i].row < B[j].row )


 Initialize i = 1 , j=1 , k=1  C[k] = A[i]
 I++, k++
 while(i <= A[0].val && j<=
 if(A[i].row > B[j].row )
B[0].val)  C[k] = B[j]
 if(A[i].row == B[j].row &&  j++, k++

A[i].col == B[j].col) // end while


 C[k].row = A[i].row  while(i<=A[0].val)
 C[k].col = A[i].col
 C[k] = A[i]
 C[k].val = A[i].val + B[j].val

 I++, j++, k++


 I++, k++
 while(j <=B[0].val)
 if(A[i].row == B[j].row &&
 C[k] = B[j]
A[i].col < B[j].col)
 C[k].row = A[i].row  j++, k++
 C[k].col = A[i].col C[0].row = A[0].row, C[0].col
 C[k].val = A[i].val
= A[0].col, C[0].val = k
 I++, k++
Sparse A Sparse B Resultant Sparse C
row col val row col val row col val
0 10 10 9 0 10 10 8 0 10 10 12
+ 1 1 2 15
1 1 2 5 1 1 2 10 =
2 2 5 18
2 2 5 8 2 2 5 10
3 2 8 2
3 2 9 -9
3 2 8 2 4 2 9 -9
4 4 7 -15 5 3 6 -5
4 3 6 -5
5 5 4 10 6 4 2 11
5 4 2 11
6 6 1 80 7 4 7 -15
6 5 4 44 8 5 4 54
7 8 4 19
7 8 3 5 9 6 1 80
8 8 7 21
8 9 1 10 10 8 3 5
9 9 8 5 11 8 4 19
12 8 7 21
13 9 1 10
ASSIGNMENT 8
Accept conventional matrix and convert it
into sparse matrix using structure and
perform following operations on it:
 Addition
 Simple Transpose
 Fast Transpose.
STACK
APPLICATION N IMPLEMENTATION OF
STACKS

static array dynamic arrays


linked list
Parenthesis Graphical region fll Infx to postfx
checker
Evaluating
• Checking algebraic
balanced expressions
expressions
WHAT IS A STACK?

 Stores a set of elements in a particular order


 Stack principle: LAST IN FIRST OUT
 = LIFO
 The last element inserted is the frst one to be
removed
 Example

36
LAST IN FIRST OUT

top
top E
D D D
C top C C C
B top B B B B
A A A A A
A top

37
STACK APPLICATIONS

 Real life
 Pileof books
 Plate trays
 More applications related to computer science
 Program execution stack
 Evaluating expressions

38
ARRAY-BASED STACK
IMPLEMENTATION
 Allocate an array of some size (pre-
defned)
 Maximum N elements in stack
 Bottom stack element stored at element
0
 last index in the array is the top
 Increment top when one element is
pushed, decrement after pop

39
OPERATIONS PERFORMED ON STACK
 ISEMPTY // STACK IS EMPTY
 ISFULL // CHECK FOR OVERFLOW

 PUSH // STORES AN ELMENT ON STACK

 POP // DELETES AN ELEMENT FROM STACK


 Peep // Just to check stack top
ALGORITHM OF PEEP() FUNCTION −

 peep()

 begin procedure peep


 return stack[top]

 end procedure
ALGORITHM OF ISFULL() FUNCTION −

 begin procedure isfull


 if top equals to MAXSIZE
 return true
 else
 return false
 endif

 end procedure
ALGORITHM OF ISEMPTY() FUNCTION

 begin procedure isempty


 if top less than 1
 return true
 else
 return false
 endif
 end procedure
PUSH
 The process of putting a new data element
onto stack is known as PUSHOperation

 Step 1 − Check if stack is full.


 Step 2 − If stack is full, produce error and

exit.
 Step 3 − If stack is not full, increment top to

point next empty space.


 Step 4 − Add data element to the stack

location, where top is pointing.


 Step 5 − return success.
ALGORITHM
begin procedure push( stack, data )
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
POP
A POP operation may involve the following
steps
 Step 1 − Check if stack is empty.

 Step 2 − If stack is empty, produce error

and exit.
 Step 3 − If stack is not empty, access the

data element at which top is pointing.


 Step 4 − Decrease the value of top by 1.

 Step 5 − return success.


POP
begin procedure pop: (stack)
if stack is empty
return null
endif
data ← stack[top]
top ← top – 1
return data
end procedure
LIST STACK EXAMPLE

Stack st = new Stack();


st.push(6);

top

6
LIST STACK EXAMPLE

Stack st = new Stack();


st.push(6);
st.push(1);

top

6
LIST STACK EXAMPLE

Stack st = new Stack();


st.push(6);
st.push(1);
st.push(7);
top 7

6
LIST STACK EXAMPLE

Stack st = new Stack();


8 st.push(6);
st.push(1);
st.push(7);
top 7 st.push(8);

6
LIST STACK EXAMPLE

Stack st = new Stack();


8 st.push(6);
st.push(1);
st.push(7);
top 7 st.push(8);
st.pop();

6
LIST STACK EXAMPLE

Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
top 7 st.push(7);
st.push(8);
st.pop();
1

6
A LEGEND
THE TOWERS OF HANOI
 In the great temple of Brahma in Benares, on
a brass plate under the dome that marks the
center of the world, there are 64 disks of
pure gold that the priests carry one at a time
between these diamond needles according to
Brahma's immutable law: No disk may be
placed on a smaller disk. In the begging of
the world all 64 disks formed the Tower of
Brahma on one needle. Now, however, the
process of transfer of the tower from one
needle to another is in mid course. When the
last disk is fnally in place, once again
forming the Tower of Brahma but on a
diferent needle, then will come the end of
the world and all will turn to dust. 54
THE TOWERS OF HANOI
A STACK-BASED APPLICATION

 GIVEN: three poles


 a set of discs on the frst pole, discs of diferent sizes,
the smallest discs at the top
 GOAL: move all the discs from the left pole to the
right one.
 CONDITIONS: only one disc may be moved at a time.
 A disc can be placed either on an empty pole or on
top of a larger disc.

55
TOWERS OF HANOI

56
TOWERS OF HANOI

57
TOWERS OF HANOI

58
TOWERS OF HANOI

59
TOWERS OF HANOI

60
TOWERS OF HANOI

61
TOWERS OF HANOI

62
TOWERS OF HANOI

63
Queues

64

64
QUEUE
 Stores a set of elements in a particular order Restricted list
 principle: FIRST IN FIRST OUT = FIFO
 It means: the frst element inserted is frst one to be removed
 A list  structure with two access points called the front and rear.
 All insertions (enqueue) occur at the rear and deletions (dequeue)
occur at the front.
 Homogeneous components

cinemark

 The frst one in line is the frst one to be served


QUEUE APPLICATIONS

 Real life examples


 Waiting in line
 Waiting on hold for tech support

 Applications related to Computer Science


 Typical uses of queues are in simulations and
operating systems.
 Job scheduling (e.g. Round-Robin algorithm for CPU
allocation)

66
WORKING OF QUEUE

67
Queue : FIFO
A AB
rear

front front
rear

Operations Performed on Queue


isFullQ
Addqueue
isEmptyQ
DeleteQ 68
Queue ADT
METHODS:
FOR ALL QUEUE  QUEUE, ITEM  ELEMENT,
MAX_ QUEUE_ SIZE  POSITIVE INTEGER

BOOLEAN ISFULLQ(QUEUE, MAX_QUEUE_SIZE) ::=


IF(NUMBER OF ELEMENTS IN QUEUE ==
MAX_QUEUE_SIZE)
RETURN TRUE
ELSE RETURN FALSE

QUEUE ADDQUEUE(QUEUE, ITEM) ::=


IF (ISFULLQ(QUEUE)) QUEUE_FULL
ELSE INSERT ITEM AT REAR OF QUEUE AND
RETURN QUEUE 69
Queue ADT (cont’d)
BOOLEAN ISEMPTYQ(QUEUE) ::=
IF (QUEUE ==CREATEQ(MAX_QUEUE_SIZE))
RETURN TRUE
ELSE RETURN FALSE
ELEMENT DELQUEUE(QUEUE) ::=
IF (ISEMPTYQ(QUEUE)) RETURN
ELSE REMOVE AND RETURN THE ITEM AT
FRONT OF QUEUE.

70
Array-based Queue
 Use an array of size N
 Two variables keep track of the front and
rear
f index of the front element
r index immediately past the rear element
 Array location r is kept empty
normal confguration
Q
0 1 2 f r

Q
71
0 1 2 r f
Array implementation of Simple
queues
 A queue is a frst in, frst out (FIFO) data structure
 This is accomplished by inserting at one end (the
rear) and deleting from the other (the front)
0 1 2 3 4 5 6 7
Queue:
rear =-1
front =-1
0 1 2 3 4 5 6 7
Queue: 1
7
front =0 rear =0
 To insert: put new element in location 0, and set rear to 0 72
and front =0
 To delete: take element from location 0, and set front to 1
Array implementation of queues
front = 0 rear = 3

Initial queue: 17 23 97 44

After 17 23 97 44 333
insertion:
After deletion: 23 97 44 333

front = 1 rear = 4

73
PSEUDOCODE :
ADD ITEM IN SIMPLE QUEUE
 Initialize F & R to zero. And create /declare Queue
Q[MAX]
 Procedure ADDQ(Q,F,R,N,item)

1. [Overflow?]
If R>=N
then write (‘OVERFLOW’)
return
2. [Increment rear pointer]
R=R+1
3. [Insert Element]
Q[R]=item
4. [Is front properly set?]
If F=-1
Then F=0
return
74
PSEUDOCODE :
DELETE ITEM FROM SIMPLE QUEUE
 Procedure DELQ(Q,F,R)
1. [Underflow?]
If F=-1
then write (‘UNDERFLOW’)
return(0)
2. [Delete Element]
Item=Q[F]
3. [Is Queue empty?]
If F>R
Then F=R=-1
Else F=F+1
4. [Return Element]
Return(item)
createQ, isEmptyQ, isFullQ
Queue createQ(max_queue_size) ::=
# defne MAX_QUEUE_SIZE 10/* Maximum queue
size */
typedef struct {
int item [MAX_SIZE];
int rear, front;
/* other felds */
} QUEUE;
QUEUE Q1,Q2…;

int isEmpty(QUEUE q) ::= q.front == q.rear

int isFullQ(QUEUE q) ::= q.rear ==


MAX_QUEUE_SIZE-1 76
enqueue

void enqueue (QUEUE Q)


{
/* add an item to the queue */
if (Q.rear == MAX_QUEUE_SIZE_1)
{
queue_full;
return;
}
Q.rear++;

Q.item [Q.real] = item;


} 77
dequeue

Int dequeue( QUEUE Q)


{
/* remove element at the front of the queue */
if ( Q.front == Q.rear)
return queue_empty( ); /* return an error
key */
return Q.item [Q.front ++];
}

78
Exercise: Queues
 Describe the output of the
following series of queue
operations
 enqueue(8)
 enqueue(3)
 dequeue()
 enqueue(2)
 enqueue(5)
 dequeue()
 dequeue()
79
 enqueue(9)
 enqueue(1)
Circular Queue
 When a new item is inserted at the rear, the
pointer to rear moves upwards.
 Similarly, when an item is deleted from the queue
the front arrow moves downwards.
 After a few insert and delete operations the rear
might reach the end of the queue and no more
items can be inserted although the items from the
front of the queue have been deleted and there is
space in the queue.

80
Circular Queues
 We can treat the array holding the
queue elements as circular (joined at the
ends)

0 1 2 3 4 5 6 7
Queue: 44 55 11 22 33

rear = 1 front = 5
 Elements were added to this queue in the order 11,
22, 33, 44, 55, and will be removed in the same
order
81
 Use: front = (front + 1) % length;
and: rear = (rear + 1) % length;
Circular Queue
EMPTY QUEUE
[2] [3] [2] [3]
22 33

[1] [4] [1] 11 [4]

[0] [5] [0] [5]

front = 0 front = 0
rear = 0 rear = 3
82
Can be seen as a circular queue
Queue Using Linked lists
 Each node in a dynamic data structure contains data AND a
reference to the next node.
 A queue also needs a reference to the head node AND a reference
to the tail node.
 The following diagram describes the storage of a queue called
Queue. Each node consists of data (DataItem) and a reference
(NextNode).

 The first node is accessed using the name Queue->Head.


 Its data is accessed using Queue->Head->DataItem
 The second node is accessed using Queue->Head->NextNode
 The last node is accessed using Queue->Tail 83
Adding a node (Add)
The new node is to be added at the tail of the queue. The reference
Queue.Tail should point to the new node, and the NextNode
reference of the node previously at the tail of the queue should
point to the DataItem of the new node.

84
Removing a Node
 The value of Queue.Head.DataItem is returned. A temporary
reference Temp is declared and set to point to head node in the
queue (Temp = Queue.Head).
 Queue.Head is then set to point to the second node instead of
the top node.
 The only reference to the original head node is now Temp and
the memory used by this node can then be freed.

85
QUEUE APPLICATION
 Queues, any time there is arrival and waiting - basically
when there are more consumer than resources.

 For example, there is a queue to service disk output and


for network output. Call center phone systems will use a
queue to hold people in line until a service representative
is free

You might also like