Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview...

24
Adapted from Data Structures with C++ using STL: Ford, Topp Dr. Nazli Mollah CS 362: Queues CS 362: Queues Overview of Lecture Introduction The Queue ADT The Radix Sort Efficiency of the Radix Sort The miniQueue Class

Transcript of Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview...

Page 1: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Overview of Lecture

Introduction

The Queue ADT

The Radix Sort

Efficiency of the Radix Sort

The miniQueue Class

Page 2: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Introduction

Queue: sequential storage structure that permits access only at two ends of the sequence

A queue inserts elements at the back and removes elements from the front

Follows FIFO/ FCFS order: items are retrieved in order of their appearance

Applications: Process/CPU scheduling – FCFS, RR Sorting Algorithms Simulations studies involve queues (study of discrete evens in a system over a time interval)

Page 3: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

The Queue ADT

Queue ADT provides essentially the same interface as does the stack

This is consistent with STL’s philosophy of using a common interface for its classes

Note the similar ADT operations in Queue and Stacks push() pop() size()

The abstract concept of a queue allows for an arbitrarily large sequence of data, hence the push() operation has no preconditions

The same is not the case for the function pop() and front() which assumes that the queue is not empty and has at least one element

1st 4th3rd2nd last

Front Back

……………………

Removes/ pop() Inserts/ push()

Page 4: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Class Queue: Constructor and Operations <queue>

queue(); // create an empty queue

bool empty() const; // return a reference to the value of the item at the front of the queue // precondition: the queue is not empty

T& front() // constant version of front()

const T& front() const; // constant version of front()

Page 5: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Class Queue: Constructor and Operations <queue>

void pop(); // remove the item from the front of the queue // precondition: the queue is not empty // postcondition: the element at the front of the queue is the element that had been added // immediately after the element that has just been popped, or the queue is empty

void push(const T&item); // insert the argument item at the back of the queue // postcondition: the queue has a new item at the back

int size() const; // return the number of elements in the queue

Page 6: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Queue Push and Pop Operations

A

BA

CBA

CB

C

frontback

front back

front back

front back

frontback

Push A

Pop A

Push C

Push B

Pop B

Page 7: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Application: Scheduling Queues schedule1.cpp

// program outputs the interview schedule for a Personnel Director

// the Executive Assistant constructs a queue of appointment times by

// reading the times from the keyboard

// cycling through the queue

// EA outputs the time at which each appointment begins

// and the available time duration of that interview

#include <iostream>

#include <iomanip> // declares the predefined parameterized manipulators and provides macros for user-defined

parameterized manipulators

#include <queue>

#include “d_time24.h”//see pg 13 [ addTime(), duration(), readTime(), writeTime(), getHour(), getMinute()…]

using namespace std;

int main()

{

Page 8: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Application: Scheduling Queues schedule1.cpp

Time24 interviewTime;// queue to hold appointment time for job applicant

queue<time24> apptQ;// create the queue

cout<<“First interview of the day: ”;

cin>>interviewTime;

while (interviewTime<time24(17,0)) //constructing the queue until the input is 5:00 PM or later

{

apptQ.push(interviewTime);// push the interview time on the queue

cout <<“Next interview: “;

cin >>interviewTime;// prompt for the next interview time and read as “interviewTime”

}

9:00

push

interviewTime

9:00

push

10:15

interviewTime

Construction

Page 9: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Application: Scheduling Queues schedule1.cpp

cout <<endl <<“Appointment Available Interview Time”<< endl;// output the day’s appointment schedule

// pop the next applicant appointment time

// determine available time for interview by checking time for applicant

// at front of queue

while (!apptQ.empty())

{

interviewTime = apptQ.front();

apptQ.pop();

//output available time

// if queue is empty, the interview ends at 5:00 PM

cout<<“ “<< interviewTime<<setw(17)<<“ “;

if (apptQ.empty())

cout<<(time24(17,0) – interviewTime) << endl;

else

cout<<(apptQ.front() – interviewTime) << endl;

}

return 0;

}

Output

9:00

cin/ read

10:15

difference/ duration

pop

interviewTime = apptQ.front()

16:30

pop

9:00

duration10:15 – 9:00

1:15

16:30

Duration17:00 – 16:30

0:30

Page 10: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

First interview of the day: 9:00

Next interview: 10:15

Next interview: 11:15

Next interview: 13:00

Next interview: 13:45

Next interview: 14:30

Next interview: 15:30

Next interview: 16:30

Next interview: 17:00 // 17:00 terminates input

Appointment Available Interview Time

9:00 1:15

Application: Scheduling Queues schedule1.cpp

Run

NB: remember this for process scheduling in CSCI 380

Page 11: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

The Radix Sort

Introduction:

Early days, data were stored on punched cards

To order data An operator Ran the cards through a mechanical sorter For integer data the machine dropped each card into one of 10 bins (0 – 9) Each bin was a queue in which a card entered at the backa nd exited in the fron

This mechanical sorter implemented the radix sort algorithm Assume the cards contain 2-digit numbers in the range 00-99 The numbers (cards) pass through the machine twice to separate the data – first by the

ones digit and then by the tens digit Each pass involves first distributing the cards into the bins and then collecting tem back into

a sequence

Page 12: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

The Radix Sort: Pass 0

Initial Sequence: 91 6 85 15 92 35 30 22 39 Distribute the cards into bins according to the ones digit (10^0)

9 130

0

39

987

6

6

3 51 58 5

543

2 29 2

21

Sequence after Pass 0: 30 91 92 22 85 15 35 6 39

Page 13: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

The Radix Sort: Pass 1

Sequence after Pass 0: 30 91 92 22 85 15 35 6 39 Distribute the cards into bins according to the ones digit (10^1)

Sequence after Pass 1: 6 15 22 30 35 39 85 91 92

6

0

9 29 1

9

8 5

87654

3 93 53 0

3

2 2

2

1 5

1

Page 14: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Radix Sort Algorithm

Pass 0: sorts ones digit (power 10^0)

Pass 1: sorts tens digit (power 10^1)

Pass 2: sorts hundreds digit (power 10^2)

3 main functions

radixSort(vector<int>& v, int d);

distribute (const vector<int>& v, queue<int> digitQueue[ ], int power)

collect(queue<int> digitQueue[], vector,int.& v)

queue<int> digitQueue[10];

// an array of 10 queues simulates the sorting bins 0 to 9

Page 15: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Radix Sort Algorithm: distribute( )

void distribute (const vector<int>& v, queue<int> digitQueue[ ], int power)

{

// the function implements the distribution of the numbers into 10 queues

// include vector of elements

// include queue containers

// include the power that designates the queue

int i;

for (i=0; i <v.size(); i++)

digitQueue[(v[i] / power)%10].push(v[i]);

// loop through the vector, inserting each element into the queue (v[i]/ power)% 10

}

Identifying the queue into which the digit needs to be pushed

Example: 56343/1000 = 56 56 % 10 = 6

digitQueue [ (v[i] / power) % 10].push(v[i]);

Page 16: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Radix Sort Algorithm: collect( )

void collect(queue<int> digitQueue[], vector,int.& v)// this function scans the array of queues in the order 0 to 9

// then gathers elements from the queue and copies back to the vector

{

int i = 0, digit;// scan the vector of queue using indices 0, 1, 2, etc,

for (digit = 0; digit < 10; digit ++)

while (!digitQueue[digit].empty( ))

{

v[i] = digitQueue[digit].front();

digitiQueue[digit].pop( );

i++// collect items until queue is empty

// then copy items back to the vector

}

}

Page 17: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Radix Sort Algorithm: radixSort( )

void radixSort(vector<int>&v, int d)// this function calls distribute(), followed by collect(), for power = 1, 10,…10^d

// implements the algorithm

// sorts vector v using the radix sort

// performs d iterations for each digit in the integer

{

int i;

int power = 1;

queue<int>digitQueue[10];

for (i = 0;i<d;i++)

{

distribute (v, digitQueue, power);

collect(digitQueue, v);

power * = 10;

}

}

Next Steps: place the functions: radixSort(), distribute(), collect() into a the header file “d_sort.h”

See Program 8-2 on page 393 for the .cpp portion

Page 18: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Queue Implementations compared to Stack Implementations

Stacks can be Implemented by:

Vectors(miniStack)

Linked Lists

Queues can be Implemented by:

Vectors(radixSort)

Linked Lists(miniQueue)

Page 19: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Implementing the miniQueue Class (implementation using list object)

Compare to miniStack example where the member functions such as size( ) and empty( ) of a miniStack object actually reflect the status of the underlying vector. To access or modify the top of the stack or to add and remove an element at the top, the miniStack class uses the functions back( ), push_back( ) and pop_back ( ) from the vector class

A similar approach is used in implementing the miniQueue Class – however instead of using a vector to store the data, we use a list object. Why??

while a vector has operations that access the back of the sequence, but it does not efficiently remove an element from the front

The list class operations prove to be an ideal interface for miniQueue class because its functions:

front( ) accesses the front of the sequence pop_front( ) efficiently removes it push_back( ) efficiently inserts an element at the back of the sequence

Page 20: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Implementing the miniQueue Class (implementation using list object)

think of the elements of the queue as organized horizontally in a waiting line

initially the queue is empty and the size of the list is 0

we can add items to the back of the list (push_back( )), which increased the size by 1

we can also remove an item from the front of the list (pop_front( )), which decreases

the size by 1

at all times we know the element at the front of the list (front( )) and

at all times we know the size of the queue and whether it is empty, by extracting this information fro m the underlying list object

Page 21: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

Implementing the miniQueue Class (implementation using list object)

m iniQ .pus h(1 0)

m iniQ .pus h(2 5 )

m iniQ .pus h(5 0 )

n = m iniQ .fro nt() // n = 1 0

m iniQ .po p()

Q u e u e S ta te m e n t L is tL is t S ta te m e n tQ u e u e1 0

ba c kfro ntqlis t.pus h_ ba c k (1 0 ) 1 0

ba c kfro nt

1 0 2 5

ba c kfro ntqlis t.pus h_ ba c k (2 5 ) 1 0 2 5

ba c kfro nt

5 01 0 2 5

ba c kfro nt

q list.push _ b a ck (5 0 )1 0 2 5

fro nt

5 0

ba c k

re turn qlis t.fro nt() // re turn 1 0

2 5 5 0

ba c kfro ntqlis t.po p_ fro nt() 2 5 5 0

m in iQ u e u e < in t> m in iQ ; // d e c la re a n e m p ty q u e u e

Page 22: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

CLASS miniQueue Partial Declaration “d_queue.h”

Template<typename T>

Class miniQueue

{

public;

miniQueue( );//constructor; create an empty queue

……………………..// member functions push( ), pop( ), front( ), size( ), empty( )

private:

list<T> qlist;// a list object maintains the queue items and size

};

Page 23: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

CLASS miniQueue Push( )

// insert item into the queue by inserting it at

// the back of the list

template<typename T>

void miniQueue<T>: :push(const T& item)

{

qlist.push_back(item);

}

Page 24: Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.

Adapted from Data Structures with C++ using STL: Ford, Topp

Dr. Nazli Mollah

CS 362: QueuesCS 362: Queues

CLASS miniQueue Pop( )

// remove the element front the front of the queue

// pop( ) and front( ) require the additional logic to test the “queue empty” condition

// if the condition is true, the functions throw the underflowError exception specified in the header file “d_except.h”

// front( ) uses the corresponding list function

template<typename T>

void miniQueue<T>: :pop( )

{ // if queue is empty, throw uderflowError

if (q.list.size( ) = = 0)

throw underflowError(“miniQueue pop( ): empty queue”);//erase the front

qlist.pop_front( );

}