MODULE 1 - ARRAYS AND SEARCHING
Polynomial representation using Arrays, Sparse matrix, Stacks, Queues - Circular Queues,
Priority Queues, Double Ended Queues, Evaluation of Expressions, Linear Search and Binary
Search
DATA STRUCTURE
It is a representation of logical relationship between individual elements of data. It is also defined
as a mathematical model of particular organization of data items. It is also called building block
of a program.
Classification of data structure
1. Linear data structure
● All the elements form a sequence or maintain a linear ordering.
2. Non linear data structure
● Elements are distributed over a plane.
1. POLYNOMIAL REPRESENTATION USING ARRAYS
● A polynomial is a sum of terms where each term has the form axe ,
Where x is the variable, a is the coefficient and e is the exponent.
Polynomial representation using Arrays
Polynomial Addition Example
S
teps of Polynomial Addition
2. SPARSE MATRIX
● 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 values, then it is
called a sparse matrix.
● Sparse matrix is a matrix which contains very few non-zero elements.
● When a sparse matrix is represented with a 2-dimensional array, we waste a lot of space to
represent that matrix.
● 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. Totally we allocate 100 X 100 X 2 = 20000 bytes of space to store this
integer matrix. To access these 10 non-zero elements we have to make scanning for 10000
times.
● Sparse Matrix Representations can be done in many ways following are two common
representations:
1. Array representation
⮚ Three tuple form
2. Linked list representation
● 2D array is used to represent a sparse matrix in which there are three columns 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)
Triplets
(0,2,3)
(0,4,4)
(1,2,5)
(1,3,7)
(3,1,2)
(3,2,6)
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.
3. STACK
● It is a linear data structure in which elements are placed one above another.
● A stack is an ordered collection of homogeneous data elements where the insertion and
deletion operations take place only at one end called Top of the stack.
● LIFO - In stack elements are arranged in Last-In-First-Out manner (LIFO). So it is
also called LIFO lists.
● Anything added to the stack goes on the “top” of the stack.
● Anything removed from the stack is taken from the “top” of the stack.
● Things are removed in the reverse order from that in which they were inserted
Operations of Stack
● Two basic operations of stack:
⮚ PUSH : Insert an element at the top of stack
⮚ POP: Delete an element from the top of stack
● An element in the stack is termed as ITEM.
● Initially top is set to -1, to indicate an empty stack. (Top = -1)
● The maximum no. of elements that a stack can accommodate is termed MAX_SIZE.
● If stack is full Top = MAX_SIZE - 1
Array representation of stack
● Stack can be represented using a linear array.
● There is a pointer called TOP to indicate the top of the stack
top
● Overflow: If we try to insert a new element in the stack top (push) which is already full,
then the situation is called stack overflow.
● Underflow: If we try to delete an element (pop) from an empty stack, the situation is
called stack underflow.
Basic Operations
● push() Pushing (storing) an element on the stack.
● pop() Removing (accessing) an element from the stack.
● peek() get the top data element of the stack, without removing it.
int peek() {
return stack[top];
}
● isFull() check if stack is full.
bool isfull() {
if (top == MAX_SIZE)
return true;
else
return false;
}
● isEmpty() check if stack is empty.
bool isempty() {
if(top == -1)
return true;
else
return false;
}
Algorithm: PUSH()
● Let A be an array with Maximum size as MAX_SIZE. Initially, top= -1
POP Operation
Start
if top < MAX_SIZE – 1
set top=top+1
Set A[top]=item
else
print “OVERFLOW”
exit
Algorithm: POP()
Start
if top= -1 then
print “UNDERFLOW”
else
set item=A[top]
Set top=top-1
exit
Applications of stack
● Reversing an array
⮚ ABCD
⮚ Pushing to stack A B C D
⮚ Popping from stack D C B A
● Undo operations
● Infix to prefix, infix to postfix conversion
● Tree Traversal
● Evaluation of postfix expressions
4. QUEUES
● A queue is an ordered collection of homogeneous data elements. In which insertion is
done at one end called REAR and deletion is done at another end called FRONT.
● FIFO - In queue elements are arranged in First-In-First-Out manner (FIFO).
● First inserted element is removed first
● Two basic operations of queue:
1. Enqueue -> Insert an element at the rear end of queue.
2. Dequeue-> Delete an element from the front end of queue.
● Initial case rear = -1 and front = 0, MAX SIZE is the size of the queue.
● If rear = front then queue contains only a single element
● If rear < front then queue is empty
● Queue full : rear = n-1 and front =0
● Whenever an element is deleted from the queue, the value of FRONT is increased by 1.
● i.e. FRONT=FRONT+1
● Similarly, whenever an element is added to the queue, the REAR is incremented by 1 as,
● REAR=REAR+1
Array Representation of Queue
Basic Operations
● enqueue() add (store) an item to the queue.
● dequeue() remove (access) an item from the queue.
● peek()
Gets the element at the front of the queue without removing it.
int peek()
{
return queue[front];
}
● isfull() Checks if the queue is full
bool isfull()
{
If (rear == MAXSIZE - 1)
return true;
else
return false;
}
● isempty() Checks if the queue is empty.
bool isempty()
{
if(front < 0 || front > rear)
return true;
else
return false;
}
Algorithm : Enqueue
Start
if rear = MAX_SIZE – 1 then
print “OVERFLOW”
else
set rear = rear + 1
Set A[rear]=item
exit
Algorithm : Dequeue Start
if rear < front then
print “UNDER FLOW”
else
set item = A[front}
set front = front + 1
exit
Type of Queues
⮚ Circular Queue
⮚ Priority Queue
⮚ Doubly ended Queue
5. CIRCULAR QUEUE
● To utilize space properly, circular queue is derived.
● In this queue the elements are inserted in circular manner.
● So that no space is wasted at all.
● Circular queue empty:
FRONT= -1
REAR= -1
● Circular queue full:
(rear + 1) % max_size = Front
● It is a modification of simple queue in which the rear pointer is set to the initial location,
whenever it reaches the location max_size – 1.
Insertion Algorithm (ENQUEUE)
if (front == -1 & rear == -1)
set front =0 and rear = 0
Set a[rear]=item
else if (front = (rear+1) % max_size) then
Print over flow
else
set rear = (rear + 1)% max_size
Set a[rear] = item
Deletion Algorithm (DEQUEUE) if front = -1 and rear = -1 then
print underflow and exit
else if front = rear
set item= a[front]
set front = -1 and rear = -1
else
set item= a[front]
set front = (front + 1) % max_size
Exit
7. PRIORITY QUEUE
● Regular queue follows a First In First Out (FIFO) order to insert and remove an item.
Whatever goes in first, comes out first.
● In a priority queue, an item with the highest priority comes out first.
● Therefore, the FIFO pattern is no longer valid.
● Every item in the priority queue is associated with a priority.
● It does not matter in which order we insert the items in the queue
● The item with higher priority must be removed before the item with the lower priority.
● If two elements have the same priority, they are served according to their order in the
queue.
Operations on a priority queue
1. EnQueue: EnQueue operation inserts an item into the queue. The item can be inserted
at the end of the queue or at the front of the queue or at the middle. The item must
have a priority.
2. DeQueue: DeQueue operation removes the item with the highest priority from the
queue.
3. Peek: Peek operation reads the item with the highest priority.
1. Enqueue Operation
1. IF((Front == 0)&&(Rear == N-1))
2. PRINT “Overflow Condition”
3. Else IF(Front == -1& rear == -1)
4. Front = Rear =0
5. Queue[Rear] = Data
6. Priority[Rear] = Priority
7. ELSE IF(Rear ==N-1)
8. FOR (i=Front;i<=Rear;i++)
9. FOR(i=Front;i<=Rear;i++)
10. Q[i-Front] =Q[i]
11. Pr[i-Front] = Pr[i]
12. Rear = Rear-Front
13. Front = 0
14. FOR(i = r;i>f;i–)
15. IF(p>Pr[i])
16. Q[i+1] = Q[i] Pr[i+1] = Pr[i]
17. ELSE
18. Q[i+1] = data Pr[i+1] = p
19. Rear++.
2. Dequeue operation
1. IF(Front == -1)
2. PRINT “Queue Under flow condition”
3. ELSE
4. PRINT”Q[f],Pr[f]”
5. IF(Front==Rear)
6. Front = Rear = -1
7. ELSE
8. FRONT++
Applications of Priority Queue
1. CPU Scheduling
2. Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum Spanning
Tree, etc
3. All queue applications where priority is involved.
4. For load balancing and interrupt handling in an operating system
8. DOUBLY ENDED QUEUE
It is a list of elements in which insertion and deletion are perform at both ends
● It has 4 operations
1. Insertion at rear end
2. Insertion at front end
3. Deletion at rear end
4. Deletion at front end
1. Algorithm : Insertion at rear end
Start
if rear = MAX_SIZE – 1 then
print “OVERFLOW”
2. Insertion at front end
Start
if front = 0 then
print “OVERFLOW” and exit
Else
set front = front - 1
Set A[front]=item
exit
3. Deletion at front end
Start
if front = 0 and rear = -1 then
print “UNDER FLOW” and exit
set item = A[front]
if front = rear then
set front = 0 and rear = -1
Else set front = front + 1
exit
4. Deletion at rear end
Start
if front = 0 and rear = -1 then
print “UNDER FLOW” and exit
set item = A[rear]
if front = rear then
9. CONVERSION & EVALUATION OF EXPRESSIONS
● Infix Expression: The operator occurs between the operands
<operand> <operator> <operand>
Eg: a+b
● Prefix Expression (Polish notation): The operators occurs before the operand
<operator> <operand> <operand>
Eg : +ab
● Postfix Expression (Reverse Polish notation): The operators occurs after the operand
<operand> <operand> <operator>
Eg : ab+
A. Postfix Expression Evaluation
Given P is the postfix expression, the following algorithm uses a stack to hold operands.
It finds the value of the arithmetic expression P, Written in postfix notation.
Algorithm:
Step 1: Add “ ) “ at the end of P
Step 2: Scan P from left – right & repeat the steps 3 & 4
Step 3: If an operand occurs, PUSH it to stack.
Step 4: If an operator occurs, then
A: Remove the top elements of the stack.
When A is the top element and B is the next top element
B: Evaluate B A
C: Place the result of step B back to stack
Step 5: Set the value equals to TOP element of the stack.
1. Evaluate the expression 5 * ( 6 + 2 ) – 12 / 4
Ans : Convert to postfix notation
5 * 6 2 + - 12 / 4
5 6 2 + * - 12 4 /
=5 6 2 + * 12 4 / -
Scanned Symbol Stack
Add “ ) “ at the end of P
P=562+ 5 5 * 12 4 / - )
6 5, 6
2 5, 6, 2
+ 5, 8
* 40
12 40, 12
4 40, 12, 4
/ 40, 3
- 37
2. Evaluate the expression ( 6 + 2 ) / ( 4 – 2 * 1 )
Ans: Convert to postfix notation
6 2 + / ( 4 – 2 1 *)
62+/421*-
62+421*-/
P=62+421*-/)
Scanned Symbol Stack
6 6
2 62
+ 8
4 84
B. Infix to 2 842 Postfix
conversion
1 8421
Here the operators
used are ^ , * 842 * , / , + , -.
The following algorithm
- 82
converts an Infix
expression Q / 4 to postfix
expression P. This
algorithm also uses a stack which holds the left parenthesis and operators. We begin by
pushing a Left parenthesis to stack and adding a right parenthesis at the end of Q.
Algorithm
Step 1: PUSH left parenthesis “(“ into stack and add right parenthesis “ ) ” at the end of
Q.
Step 2: Scan the expression Q from Left – Right and repeat the step 3 to 6 for each
element of Q until this stack is empty.
Step 3: If an operand occurs add it to P.
Step 4: If a Left parenthesis occurs then PUSH it to stack
Step 5: If an operator occurs then
A: Repeatedly POP the stack and add to P, each operator which has same or
higher precedence than
B: add to stack
Step 6: If a Right parenthesis occurs then
A: Repeatedly POP from stack and add to P each operator until a left parenthesis
occurs.
B: Remove the left parenthesis
Step 7: Exit
1. Q = A + ( B * C - ( D / E ^ F ) * G ) * H
Ans : Add right parenthesis at the end of the expression
Q=A+(B*C-(D/E^F)*G)*H)
Symbol Scanned Stack p
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF ^ /
* (+(- * ABC*DEF ^ /
G (+(- * ABC*DEF ^ /G
) (+ ABC*DEF ^ /G * -
* (+* ABC*DEF ^ /G * -
H (+* ABC*DEF ^ /G * - H
) ABC*DEF ^ /G * - H * +
2. Q = ( ( A + B ) * C – ( D – E ) ) ^ ( F + G )
Ans:
Q=((A+B)*C–(D–E))^(F+G))
Symbol Scanned Stack p
0 (
( ((
( (((
A ((( A
+ (((+ A
B (((+ AB
) (( AB+
* ((* AB+
C ((* AB+C
- ((- AB+C*
( ((-( AB+C*
D ((-( AB+C*D
- ((-(- AB+C*D
E ((-(- AB+C*DE
) ((- AB+C*DE-
) ( AB+C*DE--
^ (^ AB+C*DE--
( (^( AB+C*DE--
F (^( AB+C*DE--F
+ (^(+ AB+C*DE--F
G (^(+ AB+C*DE--FG
) (^ AB+C*DE--FG+
) AB+C*DE—FG+^
3. Q = ( A + B ) * C / D + E ^ F / G
Ans :
Q=(A+B)*C/D+E^F/G)
Symbol Scanned Stack p
( ((
A (( A
+ ((+ A
B ((+ AB
) ( AB+
* (* AB+
C (* AB+C
/ (/ AB+C*
D (/ AB+C*D
+ (+ AB+C*D/
E (+ AB+C*D/E
^ (+^ AB+C*D/E
F (+^ AB+C*D/EF
/ (+/ AB+C*D/EF^
G (+/ AB+C*D/EF^G
) AB+C*D/EF^G/+
10. LINEAR SEARCH AND BINARY SEARCH
1. Linear search: Small & unsorted arrays
2. Binary search : Large arrays & sorted arrays
1. Linear Search
● It means looking at each element of the array, in turn, until you find the target value.
Algorithm
Start
Read the ITEM to be
searched
Set flag=0
Repeat for i=0 to N
if A[i]= =ITEM
print “item found”
flag=1
If flag= =0
print “item not found”
● In the best case, the target value is in the first element of the array. So the search
takes some tiny, and constant, amount of time. Computer scientists denote this O(1)
In real life, we don’t care about the best case, because it so rarely actually happens.
● In the worst case, the target value is in the last element of the array. So the search
takes an amount of time proportional to the length of the array. Computer scientists
denote this O(n)
● In the average case, the target value is somewhere in the array. So on average, the
target value will be in the middle of the array. So the search takes an amount of time
proportional to half the length of the array – also proportional to the length of the
array – O(n) again
2. Binary Search
● The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. If you’ve found your target, stop.
4. If your target is less than the middle data value, the new search region is the lower
half of the data.
5. If your target is greater than the middle data value, the new search region is the
higher half of the data.
6. Continue from Step 2.
Algorithm
Let A be a sorted array with N elements
Start
Read the ITEM to be searched
Set beg=0, end=n-1, mid=(beg+end)/2
Repeat steps 5 to 9 while(beg<=end and A[mid]≠ ITEM)
if ITEM< A[mid] then
set end=mid-1
else
beg=mid+1
mid=(beg+end)/2
If A[mid]=ITEM then
print “item found”
Else print “element not found”
● Binary search reduces the work by half at each comparison