Chapter 05
Chapter 05
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)]
=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;
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++
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
peep()
end procedure
ALGORITHM OF ISFULL() FUNCTION −
end procedure
ALGORITHM OF ISEMPTY() FUNCTION
−
exit.
Step 3 − If stack is not full, increment top to
and exit.
Step 3 − If stack is not empty, access the
top
6
LIST STACK EXAMPLE
top
6
LIST STACK EXAMPLE
6
LIST STACK EXAMPLE
6
LIST STACK EXAMPLE
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
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
66
WORKING OF QUEUE
67
Queue : FIFO
A AB
rear
front front
rear
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…;
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
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).
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.