iti 1121. introduction to computing ii - university of ottawa · iti 1121. introduction to...

39
ITI 1121. Introduction to Computing II * Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract Queues ArrayQueue * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

Upload: others

Post on 30-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

ITI 1121. Introduction to Computing II ∗

Marcel TurcotteSchool of Electrical Engineering and Computer Science

Version of March 10, 2014

Abstract

• Queues– ArrayQueue

∗These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

Page 2: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Definitions

A queue is a linear abstract data type such that insertions are made at one end,called the rear, and removals are made at the other end, called the front.

Queues are sometimes called FIFOs: first-in first-out.

enqueue() ⇒ Queue ⇒ dequeue()

The two basic operations are:

enqueue: adds an element to the rear of the queue;

dequeue: removes and returns the element at the front of the queue.

⇒ Software queues are similar to physical ones: queuing at the supermarket, atthe bank, at cinemas, etc.

Page 3: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

ADT

public interface Queue<E> {

public abstract boolean isEmpty();

public abstract void enqueue( E o );

public abstract E dequeue();

}

Page 4: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Using an array

public class ArrayQueue<E> implements Queue<E> {

private E[] elems;

public boolean isEmpty() { ... }

public void enqueue( E o ) { ... }

public E dequeue() { ... }

}

Page 5: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Array implementation of a queue

Implementation 1: Let’s set the front of the queue to a fix location, say 0, anduse an instance variable to indicate the location of the rear element.

6 70 1 2 3 4 5 8 9

queue

rear = 4

a b c d e

⇒ Contrary to the array-based implementation of a stack, using an array toimplement a queue will create some problems, which we will circumvent, ofcourse!

Page 6: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Array implementation of a queue

public class ArrayQueue<E> implements Queue<E> {

private E[] elems;

private int rear;

public boolean isEmpty() { ... }

public void enqueue( E o ) { ... }

public E dequeue() { ... }

}

Page 7: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Remarks

• Just like stacks,

– rear can be made to designate the first free location (instead of the elementitself);

– a queue could be implemented in the upper part of the array.

⇒ Without effects on the efficiency.

Page 8: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Insertion (enqueue)

Inserting an element into a queue, when the front location is fixed, is similar toadding an element onto a stack.

6 70 1 2 3 4 5 8 9

queue

rear = 4

a b c d e

6 70 1 2 3 4 5 8 9

queue a b c d e

rear = 5

6 70 1 2 3 4 5 8 9

queue a b c d e

rear = 5

f

⇒ (i) the variable rear is incremented by 1, then (ii) the new element is storedat the position designated by rear.

Page 9: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Removal (dequeue)

What about removing an element?

All the elements must be moved one position toward the front of the queue sothat the front element remains at a fixed location, here 0.

1. Save the value to be returned;2. For i from 1 to (rear - 1) move the element at position i to i− 1;3. Set rear cell to null;4. Decrement rear by 1;5. Return the saved value.

Page 10: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Removal (dequeue)

6 70 1 2 3 4 5 8 9

queue

rear = 4

a b c d e

6 70 1 2 3 4 5 8 9

queue

rear = 4

b c d e

6 70 1 2 3 4 5 8 9

queue

rear = 4

b c d e

6 70 1 2 3 4 5 8 9

queue b c d e

rear = 3

Page 11: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Removal (dequeue)

For each element that’s removed, n− 1 elements must be moved!

The more elements there are in the queue, the more costly the removal operationbecomes.

Doubling the number of elements in the queue means doubling the cost of theremoval operations.

On the other hand, for a fixed-size array, the cost of inserting an element remainsconstant.

⇒ What improvements can be made?

Page 12: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Array-based implementation

Implementation 2: the front and the rear of the queue are now moving, thiswill require two instance variables, one that points at the front element andone that points at the rear.

6 70 1 2 3 4 5 8 9

queue c d e f

rear = 5front = 2

Page 13: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Removal (dequeue)

General case:To remove an element, (i) save the value located at the position designated byfront to a (local) temporary variable, (ii) set the cell designated by front to null ,(iii) increment front and (iv) return the saved value.

Removal is now done in constant time, always the same four steps above.

Mission accomplished?

Any particular problem with this implementation?

Page 14: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Inserting (enqueue)

What about insertions?

Just like before:

1. Increment by one the variable rear,

2. Insert the new value at the position designated by rear.

Page 15: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

This implementation has a special case, when the queue has reached the upperpart of the array, and there are free cells in its lower part, all the elements mustbe moved.

6 70 1 2 3 4 5 8 9

queue g jh if

rear = 9front = 56 70 1 2 3 4 5 8 9

queue g jh if

rear = 9front = 56 70 1 2 3 4 5 8 9

queue g hf i j

front = 0 rear = 4

6 70 1 2 3 4 5 8 9

queue g hf i j

front = 0 rear = 5

k

Page 16: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

This new implementation allows to remove elements efficiently, i.e. does notdepend on the size of the queue.

The price to pay is that whenever the queue reaches the limit of the array andfront is not 0, i.e. the queue is not full, then the queue must be re-positioned tothe lower part of the array, i.e. all the elements are moved.

The larger the queue the more costly this operation becomes.

For implementation 1, adding an element is done efficiently but removal is slow.

For implementation 2, it is the contrary, adding an element now becomes theslow operation (because it needs to re-position the queue from time to time) butremoval is always fast.

⇒ Can both operations be implemented efficiently?

Page 17: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Array-based implementation

Implementation 3: to make both operations fast this implementation uses acircular array. Two instance variables will be needed, front and rear.

f

01

2

3

4

5

6

7

13

14

1716

15

12

11

109 8

g hi

j

front = 16 rear = 2queue

Page 18: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

10

3

2

5

4

7

6

14

13

1617

12

15

10

11

89

front = queue rear =

Page 19: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Insertion: (i) increment the value of rear , (ii) insert the element at the positiondesignated by rear.

Removal: save the element found at position front, set the position to null,increment front and return the saved value.

Page 20: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Memory diagram

elems

q

B

CA

front

rear

0 1 72 3 4 5 6

2

4

Page 21: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Memory diagram

elems

q

front

rear

0 1 72 3 4 5 6

Page 22: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Circular array

How to implement a circular array?

The idea is as follows, once the queue reaches the end of the array, the free cellsat the start of the array are re-used (if they are free, of course).

6 70 1 2 3 4 5 8 9

queue g jh if

front = 5

rear = 9

6 70 1 2 3 4 5 8 9

queue g jh if

front = 5

k

rear = 0

Page 23: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

6 70 1 2 3 4 5 8 9

queue g jh if

front = 5

k l

rear = 1

Page 24: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Enqueue could be implemented as follows:

rear = rear + 1;

if ( rear == MAX_QUEUE_SIZE ) {

rear = 0;

}

What other technique can be used to implement a wraparound?

We can use modulo arithmetic 1:

rear = ( rear + 1 ) % MAX_QUEUE_SIZE;

1Just like increasing the number of seconds in the class Time.

Page 25: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Insertion (enqueue)

1. rear = ( rear+1 ) % MAX QUEUE SIZE,2. add the element at the position designated by rear.

Page 26: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Removal (dequeue)

1. save the value found at the position designated by front,2. set that location to null,3. front = ( front+1 ) % MAX QUEUE SIZE,4. return the saved value.

⇒ What occurs when the queue is empty?

Page 27: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A
Page 28: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

6 70 1 2 3 4 5 8 9

queue gf

front = 5

rear = 6

6 70 1 2 3 4 5 8 9

queue g

rear = 6

front = 6

Page 29: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

6 70 1 2 3 4 5 8 9

queue

rear = 6

front = 7

Page 30: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

6 70 1 2 3 4 5 8 9

queue

rear = 6

front = 7

6 70 1 2 3 4 5 8 9

queue

rear = 6

front = 7

a b cd h i je f g

We can see that it is no longer possible to distinguish the empty queue and theone which is full on the basis of the values of rear and front alone.

Solutions?

Page 31: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

There are many implementation techniques:

• use a boolean instance variable (isEmpty);• use a sentinel value (-1) for rear and/or front;• count the number of elements (size)

Page 32: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

For our implementation, we will use a sentinel for the value of rear, here -1.

public class CircularQueue<E> implements Queue<E> {

private static final int MAX_QUEUE_SIZE = 100;

private E[] q;

private int front, rear;

public CircularQueue() {

q = (E[]) new Object[ MAX_QUEUE_SIZE ];

front = 0;

rear = -1; // represents the empty queue

}

// ...

Page 33: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

public void enqueue( E o ) {

rear = ( rear+1 ) % MAX_QUEUE_SIZE;

q[ rear ] = o;

}

Page 34: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

public boolean isEmpty() {

return ( rear == -1 ) ;

}

public boolean isFull() {

}

Page 35: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

public E peek() {

return q[ front ];

}

public E dequeue() {

}

Page 36: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Insertion (enqueue)

An alternative implementation would be to use an instance variable to count thenumber of elements.

1. rear = ( rear+1 ) % MAX QUEUE SIZE;2. add the new element at the position designated by rear;3. increment count.

Page 37: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

Removal (dequeue)

1. save the front value;2. set that location to null;3. front = ( front+1 ) % MAX QUEUE SIZE;4. decrement count;5. return the saved value.

Page 38: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

empty()

1. count == 0

Page 39: ITI 1121. Introduction to Computing II - University of Ottawa · ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science ... A

isFull()

1. count == MAX QUEUE SIZE