Chapter 5: Queues: Bcs1223: Data Structures & Algorithms
Chapter 5: Queues: Bcs1223: Data Structures & Algorithms
OBJECTIVE
To introduce:
Queues concept
Queues operations
Queue Simulation
INTRODUCTION
INTRODUCTION
INTRODUCTION
Basic operations:
1) Add insert a given element at the back of the queue.
Figure 2: Add
INTRODUCTION
2) Remove if the queue is not empty, delete and return the element
that is at the front of the queue.
Figure 3: Remove
INTRODUCTION
3) First if the queue is not empty, return the element that is at the front
the queue.
Figure 4: First
of
INTRODUCTION
Example:
Given queue Q with list of operations as below. Illustrate the queue operations step
by step:
Q.add(green), Q. add(blue), Q.remove( ), Q.add (red), frontItem = Q.first( ), lastItem =
Q.last(), Q.remove( ), Q.remove().
rear
Array Implementation
front
rear
// to show no data
0
2
rear
Q.add(A)
// add(A)
rear++;
// increase first
data[rear] = A;
// and assignment
-1 0
front
rear
-1 0
A B
front
rear
A B U
front
rear
5
Output : A
front
rear
0 1 2 3
A B U
5
Output : AB
front
rear
0 1 2 3
A B U
5
Output : ABU
front
rear
Q.add(B)
rear++;
data[rear] = B;
-1
front
0 1 2 3 4
A B U B
rear
rear++;
data[rear] = O;
-1
front
0 1 2 3 4 5
A B U B O
rears
rear++;
data[rear] = L;
-1
front
0 1 2 3 4 5
A B U B O L
rear
-1
front
0 1 2 3 4 5
A B U B O L
rear
-1
0 1 2 3 4 5
A T O B O L
front
rear
How to differentiate whether queue is full or empty?
If front == rear, is not enough, we need another variable called ItemInQ of
type bool. Assign ItemInQ as false if queue is empty and true if queue is
full.
Queue Program:
}
public Object remove() {
if (size()==0) throw new IllegalStateException("queue is empty");
Object object=a[front];
a[front++] = null;
return object;
}
public int size() {
return back - front;
}
private void resize() {
Object[] aa = a;
a = new Object[2*aa.length];
System.arraycopy(aa, front, a, 0, size());
}
}
QUEUE APPLICATION:
QUEUE SIMULATION
Consider the real-world example of cars arriving at a station of toll
booths. The clients are the cars and the servers are the toll booths
(or their operators).
The client/server system is pictured as below with three toll booths,
labeled A, B and C. The cars are numbered. Cars 24, 21 and 22 are
being served, while cars 25-28 are waiting in the queue.
QUEUE APPLICATION:
QUEUE SIMULATION
The objects are:
Clients (cars)
Servers (toll booths)
A queue
The events are:
A client arrives at the queue
A server begins serving a client
A server finishes serving a client
QUEUE APPLICATION:
QUEUE SIMULATION
Algorithm Client/Server Simulation
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
QUEUE APPLICATION:
QUEUE SIMULATION
Client/Server Simulation
for (int t=0; ; t++) { //step 1
if (t==nextArrivalTime) { //step 2
Client client = clients[i++] = new SimClient(i,t); //step 3
queue.add(client); //step 4
nextArrivalTime = t + randomArrival.nextInt(); //step 5
}
for (int j=0; j<numServers; j++) { //step 6
Server server = servers[j];
if (t==server.getStopTime()) server.stopServing(t); //step 7
if (server.isIdle() && !queue.isEmpty()) { //step 8
Client client = (SimClient)queue.remove(); //step 9
server.startServing(client,t); //step 10
}
}
}
QUEUE APPLICATION:
QUEUE SIMULATION
Server Interface
public interface Server {
public int getMeanServiceTime();
public int getStopTime();
public boolean isIdle();
public void startServing(Client client, int t);
public void stopServing(int t);
}
Client Interface
public interface Client {
public void setStartTime(int t);
public void setStopTime(int t);
}
QUEUE APPLICATION:
QUEUE SIMULATION
Server and Client objects
QUEUE APPLICATION:
QUEUE SIMULATION
A Server class
public class SimServer implements Server {
private Client client;
QUEUE APPLICATION:
QUEUE SIMULATION
public boolean isIdle() {
return client==null; }
public void startServing(Client client, int t) {
this.client = client;
this.client.setStartTime(t);
this.stopTime = t + random.nextInt();
System.out.println(this + " started serving " + client
+ " at time " + t + " and will finish at time " + stopTime); }
}
public String toString() {
String s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return "Server " + s.charAt(id); }
}
QUEUE APPLICATION:
QUEUE SIMULATION
A Client class
public class ExponentialRandom extends java.util.Random {
private double mean;
public ExponentialRandom(double mean) {
super(System.currentTimeMillis());
this.mean = mean;
}
public double nextDouble() {
return -mean*Math.log(1.0-super.nextDouble());
}
public int nextInt() {
return (int)Math.ceil(nextDouble());
}
}
QUEUE APPLICATION:
QUEUE SIMULATION
Exponential Distribution
public class SimClient implements Client {
int id, arrivalTime=-1, startTime=-1, stopTime=-1;
QUEUE APPLICATION:
QUEUE SIMULATION
A Simulation class
public class Simulation {
QUEUE APPLICATION:
QUEUE SIMULATION
static void init(String[] args) {
if (args.length<4) {
System.out.println("Usage: java Simulation <numServers> "
QUEUE APPLICATION:
QUEUE SIMULATION
for (int j=0; j<numServers; j++)
servers[j] = new SimServer(j,randomService.nextInt());
System.out.println("
System.out.println("
System.out.println("
QUEUE APPLICATION:
QUEUE SIMULATION
Simulation objects
QUEUE APPLICATION:
QUEUE SIMULATION
Arrivals and Departures
QUEUE APPLICATION:
QUEUE SIMULATION
Output
Number of servers = 3
Number of clients = 20
Mean service time = 30
QUEUE APPLICATION:
QUEUE SIMULATION
Server A started serving Client 1 at time 0 and will finish at time 39
Client 2 arrived at time 6
The queue has 1 clients
The queue has 0 clients
Server B started serving Client 2 at time 6 and will finish at time 28