Group Theory
Group Theory
Introduction
• First in, first out (FIFO) structure (equivalent to Last
in, last out (LILO) structure).
• An ordered list of homogeneous elements in which
– Insertions take place at one end (REAR).
– Deletions take place at the other end (FRONT).
FRONT REAR
Data Data … Data
Element 1 Element 2 Element n
Operations
• Two primary operations:
– enqueue() − Adds an element to the rear of a queue.
– dequeue() − Removes the front element of the queue.
• Other operations for effective functionality:
– isFull() − Check if queue is full. OVERFLOW
– isEmpty() − Check if queue is empty. UNDERFLOW
– size() − Returns the number of elements in the queue.
– peek() – Returns the element at the front of the queue.
Queue – Enqueue
enqueue(9) enqueue(6)
front = -1 9 front = 0 9 6
1 3
rear = -1 rear = 2
0 1 2 3 4 0 1 2 3 4
enqueue(1) enqueue(4)
front = 0 9 1 front = 0 9 4
1 3 6
rear = 0 rear = 3
0 1 2 3 4 0 1 2 3 4
enqueue(3) enqueue(2)
front = 0 9 3 front = 0 9 2
1 1 3 6 4
rear = 1 rear = 4
0 1 2 3 4 0 1 2 3 4
OVERFLOW
The queue is full, no more elements
can be added. OVERFLOW
Queue – Dequeue
dequeue() dequeue()
9 9 front = 0 6 front = 3
1 3 6 4 6 4
rear = 4 rear = 4
0 1 2 3 4 0 1 2 3 4
dequeue() dequeue()
1 front = 1 4 front = 4
1 3 6 4 4
rear = 4 rear = 4
0 1 2 3 4 0 1 2 3 4
dequeue() dequeue()
3 front = 2 Nothing front = 5
3 6 4
rear = 4 rear = 4
0 1 2 3 4 0 1 2 3 4
UNDERFLOW
The queue is empty, no element
can be removed. UNDERFLOW
Queue as an ADT
• A queue is an ordered list of elements of same
data type.
• Elements are always inserted at one end (rear) and
deleted from another end (front).
• Following are its basic operations:
– Q = init() – Initialize an empty queue.
– size() – Returns the number of elements in the queue.
– isEmpty(Q) – Returns "true" if and only if the queue
Q is empty, i.e., contains no elements.
Contd…
– isFull(Q) – Returns "true" if and only if the queue
Q has a bounded size and holds the maximum number
of elements it can.
– front(Q) – Returns the element at the front of the
queue Q.
– Q = enqueue(Q,x) – Inserts an element x at the rear of
the queue Q.
– Q = dequeue(Q) – Removes an element from the front
of the queue Q.
– print(Q) – Prints the elements of the queue Q from
front to rear.
Implementation
• Using static arrays
– Realizes queues of a maximum possible size.
– Front is maintained at the smallest index and
rear at the maximum index values in the array.
Variants
Problem with Simple Queues
dequeue() dequeue()
9 9 front = 0 6 front = 3
1 3 6 4 6 4
rear = 4 rear = 4
0 1 2 3 4 0 1 2 3 4
dequeue() dequeue()
1 front = 1 4 front = 4
1 3 6 4 4
rear = 4 rear = 4
0 1 2 3 4 0 1 2 3 4
dequeue() dequeue()
3 front = 2 Nothing front = 5
3 6 4
rear = 4 rear = 4
0 1 2 3 4 0 1 2 3 4
UNDERFLOW
The queue is empty, still no
element can be added as
REAR = N-1 (Queue Full)
Circular Queues
• The front and rear ends of a queue are joined to
make the queue circular.
• Also known as circular buffer, circular queue, cyclic
buffer or ring buffer.
• Overflow
front == (rear+1) % MAXLEN
• Underflow
(front == rear) && (rear == -1)
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
front = rear = -1
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
front = rear = 0 4
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
rear = 1
1
front = 0 4
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
rear = 2
1 11
front = 0 4
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
1 11
front = 0 4 13 rear = 3
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
1 11
front = 0 4 13
6 rear = 4
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
1 11
front = 0 4 13
8
rear = 5
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
1 11
front = 0 4 13
18 8
rear = 6
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
rear = 1 front = 2
1 11 1 11 1 11
4 13 front = 0 4 13 4 13
20 6 rear = 7 20 6 20 6
18 8 18 8 18 8
front = 6 rear = 5
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
rear = 7 20 6
front = 4
18 8
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
rear = 0 4
20 6
front = 4
18 8
REAR = (REAR+1)%8
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
1 11
rear = 3
4 13
20
front = 7
Contd…
• Overflow
front == (rear+1) % 8
• Underflow
(front == rear) && (rear == -1)
1 11
front = 0 4 13 rear = 3
FRONT = (FRONT+1)%8
Enqueue Operation
• Let,
– QUEUE be an array with MAX locations.
– FRONT and REAR points to the front and rear of the
QUEUE.
– ITEM is the value to be inserted.
1. if (FRONT == (REAR+1)%MAX)
2. Print [Overflow]
3. else
4. Set REAR = (REAR+1)%MAX
5. Set QUEUE[REAR] = element
6. If (FRONT == -1 )
7. Set FRONT = 0
Dequeue Operation
• Let,
– QUEUE be an array with MAX locations.
– FRONT and REAR points to the front and rear of the QUEUE.
– ITEM holds the value to be deleted.
1. if ((FRONT == REAR) && (REAR == -1))
2. Print [Underflow]
3. else
4. ITEM = Q[FRONT]
5. If (FRONT == REAR)
6. FRONT = REAR = -1
7. Else
8. FRONT = (FRONT + 1) % MAX
Deque
• Double-ended queue.
• Generalization of queue data structure.
• Elements can be added to or removed from either
of the two ends.
• A hybrid linear structure that provides all the
capabilities of stacks and queues in a single data
structure.
• Does not require the LIFO and FIFO orderings.
Contd…
Types
• Input-restricted deque.
– Deletion can be made from both ends, but insertion
can be made at one end only.
• Output-restricted deque.
– Insertion can be made at both ends, but deletion can
be made from one end only.
Priority Queues
• Another variant of queue data structure.
• Each element has an associated priority.
• Insertion may be performed based on the priority.
• Deletion is performed based on the priority.
• Elements having the same priority are served or
deleted according to first come first serve order.
• Two types:
– Min-priority queues (Ascending priority queues)
– Max-priority queues (Descending priority queues)