preparation data structures 08 queues
TRANSCRIPT
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
2 / 116
Images/cinvestav-1.jpg
Recursion ≈ Depth First Search
Actually
This is the classic order when recursion is done!!!
What if...
We need a di�erent order?
14 / 116
Images/cinvestav-1.jpg
Recursion ≈ Depth First Search
Actually
This is the classic order when recursion is done!!!
What if...
We need a di�erent order?
14 / 116
Images/cinvestav-1.jpg
Queues
De�nition of Queues
A queue is a abstract data structure that models/enforces the�rst-come �rst-serve order, or equivalently the First-In First-Out(FIFO) order.
Thus
Using ADT Which is the �rst thing that comes to your mind to implementa queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRSTITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
Images/cinvestav-1.jpg
Queues
De�nition of Queues
A queue is a abstract data structure that models/enforces the�rst-come �rst-serve order, or equivalently the First-In First-Out(FIFO) order.
Thus
Using ADT Which is the �rst thing that comes to your mind to implementa queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRSTITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
Images/cinvestav-1.jpg
Queues
De�nition of Queues
A queue is a abstract data structure that models/enforces the�rst-come �rst-serve order, or equivalently the First-In First-Out(FIFO) order.
Thus
Using ADT Which is the �rst thing that comes to your mind to implementa queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRSTITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
Images/cinvestav-1.jpg
De�nition
We have then
A linear list.
Entry Points
One end is called front.
Other end is called rear.
Insertion and Deletions
Additions are done at the rear only.
Removals are made from the front only.
17 / 116
Images/cinvestav-1.jpg
De�nition
We have then
A linear list.
Entry Points
One end is called front.
Other end is called rear.
Insertion and Deletions
Additions are done at the rear only.
Removals are made from the front only.
17 / 116
Images/cinvestav-1.jpg
De�nition
We have then
A linear list.
Entry Points
One end is called front.
Other end is called rear.
Insertion and Deletions
Additions are done at the rear only.
Removals are made from the front only.
17 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
29 / 116
Images/cinvestav-1.jpg
Queue Interface
Code
p u b l i c i n t e r f a c e Queue<Item>{p u b l i c boo l ean empty ( ) ;p u b l i c I tem f r o n t ( ) ;p u b l i c I tem r e a r ( ) ;p u b l i c I tem Dequeue ( ) ;p u b l i c vo i d Enqueue ( Item theOb j ec t ) ;p u b l i c i n t s i z e ( ) ;
}
30 / 116
Images/cinvestav-1.jpg
Explanation
public boolean empty()
Check whether the queue is empty.
Return TRUE if it is empty and FALSE otherwise.
Example
public Item front()
Return the value of the item at the font of the queue withoutremoving it.
Precondition: The queue is not empty.
31 / 116
Images/cinvestav-1.jpg
Explanation
public boolean empty()
Check whether the queue is empty.
Return TRUE if it is empty and FALSE otherwise.
Example
BC A
public Item front()
Return the value of the item at the font of the queue withoutremoving it.
Precondition: The queue is not empty.
31 / 116
Images/cinvestav-1.jpg
Explanation
public boolean empty()
Check whether the queue is empty.
Return TRUE if it is empty and FALSE otherwise.
Example
public Item front()
Return the value of the item at the font of the queue withoutremoving it.
Precondition: The queue is not empty.
31 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue withoutremoving it.
Precondition: The queue is not empty.
public void Enqueue(Item theObject)
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
32 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue withoutremoving it.
Precondition: The queue is not empty.
public void Enqueue(Item theObject)
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
32 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue withoutremoving it.
Precondition: The queue is not empty.
public void Enqueue(Item theObject)
Insert the argument item at the back of the queue.
Postcondition: The queue has a new item at the back
32 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
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 thatwas added immediately after the element just popped or thequeue is empty.
33 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
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 thatwas added immediately after the element just popped or thequeue is empty.
33 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
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 thatwas added immediately after the element just popped or thequeue is empty.
33 / 116
Images/cinvestav-1.jpg
Explanation
Example
BC A
1Enqueue( )
1
public Item Dequeue()
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 thatwas added immediately after the element just popped or thequeue is empty.
33 / 116
Images/cinvestav-1.jpg
Explanation
Example
B
C
A
Dequeue()
1
public int size()
It returns the size.
34 / 116
Images/cinvestav-1.jpg
Explanation
Example
B
C
A
Dequeue()
1
public int size()
It returns the size.
34 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Applications of Queues
Direct applications
Waiting lists
I Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
I Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
36 / 116
Images/cinvestav-1.jpg
Thus, Using a Trick
and Queue
We can change the direction of the recursion!!!
Look at the board
38 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
39 / 116
Images/cinvestav-1.jpg
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signi�cative numberto most signi�cative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
Images/cinvestav-1.jpg
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signi�cative numberto most signi�cative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
Images/cinvestav-1.jpg
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signi�cative numberto most signi�cative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
Images/cinvestav-1.jpg
Example
Pass 0: Distribute the digits into bins according to the 1's digit (100).
0 1 2 3 4 5 6 7 8 9
41 / 116
Images/cinvestav-1.jpg
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into binsdetermined by the 10's digit (101)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-1.jpg
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into binsdetermined by the 10's digit (101)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-1.jpg
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into binsdetermined by the 10's digit (101)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-1.jpg
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into binsdetermined by the 10's digit (101)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-1.jpg
Finally
Next
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
Next
Pass 1: Take the new sequence and distribute the cards into binsdetermined by the 10's digit (101)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
Images/cinvestav-1.jpg
Code
We need something else
p u b l i c s t a t i c Rad i xSo r t ( L i n e a r L i s t Element , i n t k ){i n t Radix = 10i n t power = 1i n t d i g i t ;Queue<I n t e g e r >[ ] d i g i tQueue = (Queue<I n t e g e r >[ ] )
new Queue [ 1 0 ] ;f o r ( i n t i =0; i<k ; i++)
{f o r ( i n t j =0; j< Element . s i z e ( ) ; j++){
d i g i t = Element . remove ( 0 ) ;d i g i tQueue [ ( d i g i t /power )%10] .enqueue ( d i g i t ) ;
}f o r ( i n t j =0; j< Radix ; j++){
// WHAT?}
power ∗= 10 ;}
}43 / 116
Images/cinvestav-1.jpg
Radix Sort: Complexity
Lemma 1
Given n d-digit numbers in which each digit can take on up to k possiblevalues, RADIX-SORT correctly sorts these numbers in O(d(n+k)) time.
44 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
45 / 116
Images/cinvestav-1.jpg
Simulating Waiting Lines
Simulation is used to study the performance
Of a physical (�real�) system.
By using a physical, mathematical, or computer model of the system.
Simulation allows designers to estimate performance
Before building a system
Simulation can lead to design improvements
Giving better expected performance of the system
46 / 116
Images/cinvestav-1.jpg
Simulating Waiting Lines
Simulation is used to study the performance
Of a physical (�real�) system.
By using a physical, mathematical, or computer model of the system.
Simulation allows designers to estimate performance
Before building a system
Simulation can lead to design improvements
Giving better expected performance of the system
46 / 116
Images/cinvestav-1.jpg
Simulating Waiting Lines
Simulation is used to study the performance
Of a physical (�real�) system.
By using a physical, mathematical, or computer model of the system.
Simulation allows designers to estimate performance
Before building a system
Simulation can lead to design improvements
Giving better expected performance of the system
46 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Queuing Theory
Constraints
We will maintain a simulated clock Counts in integer �ticks�, from 0
At each tick
Frequent �yer (FF) passenger arrives in line
Regular (R) passenger arrives in line
One agent with priorities
1 Agent �nishes, then serves next FF passenger2 Agent �nishes, then serves next R passenger
Agent is idle (both lines empty)
Using some other constraints
Max # FF served between regular passengers
Arrival rate of FF passengers
Arrival rate of R passengers
Service time 47 / 116
Images/cinvestav-1.jpg
Thus
Desired Output
Statistics on waiting times, agent idle time, etc.
Optionally, a detailed trace
48 / 116
Images/cinvestav-1.jpg
Thus
Desired Output
Statistics on waiting times, agent idle time, etc.
Optionally, a detailed trace
48 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
49 / 116
Images/cinvestav-1.jpg
Example
Circuit Board - Label all reachable squares 1 unit from start
51 / 116
Images/cinvestav-1.jpg
Example
Circuit Board - Label all reachable unlabeled squares 2 units fromstart.
52 / 116
Images/cinvestav-1.jpg
Example
Circuit Board - Label all reachable unlabeled squares 3 units fromstart.
53 / 116
Images/cinvestav-1.jpg
Example
Circuit Board - Label all reachable unlabeled squares 4 units fromstart.
54 / 116
Images/cinvestav-1.jpg
Example
Circuit Board - Label all reachable unlabeled squares 5 units fromstart.
55 / 116
Images/cinvestav-1.jpg
Example
Circuit Board - Label all reachable unlabeled squares 6 units fromstart.
56 / 116
Images/cinvestav-1.jpg
What Implementations
Directly Extending Classes
From ArrayLinearList
Note: Not a so good idea!!!
Extending from class but adding some pointers
From Scratch
Circular Array
58 / 116
Images/cinvestav-1.jpg
What Implementations
Directly Extending Classes
From ArrayLinearList
Note: Not a so good idea!!!
Extending from class but adding some pointers
From Scratch
Circular Array
58 / 116
Images/cinvestav-1.jpg
What Implementations
Directly Extending Classes
From ArrayLinearList
Note: Not a so good idea!!!
Extending from class but adding some pointers
From Scratch
Circular Array
58 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
59 / 116
Images/cinvestav-1.jpg
Derive from ArrayLinearList
Here
We do not extend our data structure.
Simply use
Whatever is available in the base class.
60 / 116
Images/cinvestav-1.jpg
Derive from ArrayLinearList
Here
We do not extend our data structure.
Simply use
Whatever is available in the base class.
60 / 116
Images/cinvestav-1.jpg
Derive from ArrayLinearList
We have then
Front
Rear
When the front is the left end of list and the rear is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(0) O(1)
rear() get(size()-1) O(1)
Enqueue(TheObject) add(size(),theObject) O(1)
Dequeue() remove(0) O(size)
61 / 116
Images/cinvestav-1.jpg
Derive from ArrayLinearList
We have then
Front
Rear
When the front is the left end of list and the rear is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(0) O(1)
rear() get(size()-1) O(1)
Enqueue(TheObject) add(size(),theObject) O(1)
Dequeue() remove(0) O(size)
61 / 116
Images/cinvestav-1.jpg
Derive from ArrayLinearList
We have then
Front
Rear
When the front is the left end of list and the rear is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(0) O(1)
rear() get(size()-1) O(1)
Enqueue(TheObject) add(size(),theObject) O(1)
Dequeue() remove(0) O(size)
61 / 116
Images/cinvestav-1.jpg
Shift the front and rear pointers!!!
We haveFront
Rear
When the rear is the left end of list and the front is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(size()-1) O(1)
rear() get(0) O(1)
Enqueue(TheObject) add(0,theObject) O(size)
Dequeue() remove(size()-1) O(1)
62 / 116
Images/cinvestav-1.jpg
Shift the front and rear pointers!!!
We haveFront
Rear
When the rear is the left end of list and the front is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(size()-1) O(1)
rear() get(0) O(1)
Enqueue(TheObject) add(0,theObject) O(size)
Dequeue() remove(size()-1) O(1)
62 / 116
Images/cinvestav-1.jpg
Shift the front and rear pointers!!!
We haveFront
Rear
When the rear is the left end of list and the front is the right end
Operation in Queue Supporting method from parent class Complexity
empty() super.isEmpty() O(1)
front() get(size()-1) O(1)
rear() get(0) O(1)
Enqueue(TheObject) add(0,theObject) O(size)
Dequeue() remove(size()-1) O(1)
62 / 116
Images/cinvestav-1.jpg
Moral of the Story
We have
to perform each operation in O(1) time (excluding array doubling),we need a customized array representation.
We need to extend the data structure
We can do that using the circular idea!!!
63 / 116
Images/cinvestav-1.jpg
Moral of the Story
We have
to perform each operation in O(1) time (excluding array doubling),we need a customized array representation.
We need to extend the data structure
We can do that using the circular idea!!!
63 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
64 / 116
Images/cinvestav-1.jpg
What if we derive directly from the Chain List
We have
What about the operations?
We might decide that the �rst node is the front and the last node isthe rear!!!
65 / 116
Images/cinvestav-1.jpg
What if we derive directly from the Chain List
We have
What about the operations?
We might decide that the �rst node is the front and the last node isthe rear!!!
65 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
We have the following
Operations
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0) No problem here
I O(1) time
What about these ones
rear() => get(size()-1)
I O(size) time
Enqueue(theObject) => add(0,TheObject)
I O(1) time
Dequeue() => remove(size()-1)
I O(size) time
66 / 116
Images/cinvestav-1.jpg
Not Good At ALL
Even
If we change the front and the rear we do not get the performance wewant!!!
67 / 116
Images/cinvestav-1.jpg
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
I This returns the element pointed by the lastNode
append(TheObject)
I append elements at the end of the list
68 / 116
Images/cinvestav-1.jpg
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
I This returns the element pointed by the lastNode
append(TheObject)
I append elements at the end of the list
68 / 116
Images/cinvestav-1.jpg
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
I This returns the element pointed by the lastNode
append(TheObject)
I append elements at the end of the list
68 / 116
Images/cinvestav-1.jpg
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
I This returns the element pointed by the lastNode
append(TheObject)
I append elements at the end of the list
68 / 116
Images/cinvestav-1.jpg
Better Derive From Chain
We need to extend the data structure
To have another pointer to the last node!!!
We need to add other methods to the extended class
getLast()
I This returns the element pointed by the lastNode
append(TheObject)
I append elements at the end of the list
68 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0)
I O(1) time
69 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0)
I O(1) time
69 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0)
I O(1) time
69 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0)
I O(1) time
69 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
Queue.empty() => super.isEmpty()
I O(1) time
front() => get(0)
I O(1) time
69 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
Derive From Chain
We have
When the front is the left end of list and the rear is the rightend
rear() => getLast() . . . new method
I O(1) time
Enqueue(theObject) => append(theObject) . . . new method
I O(1) time
Dequeue() => remove(0)
I O(1) time 70 / 116
Images/cinvestav-1.jpg
But even with this implementation
Problems
We have do still some code that does not belong to the ADT queue!!!
Example
For example the idea of index checking!!!
Moral of the Story
Better to implement from scratch... when possible!!!
71 / 116
Images/cinvestav-1.jpg
But even with this implementation
Problems
We have do still some code that does not belong to the ADT queue!!!
Example
For example the idea of index checking!!!
Moral of the Story
Better to implement from scratch... when possible!!!
71 / 116
Images/cinvestav-1.jpg
But even with this implementation
Problems
We have do still some code that does not belong to the ADT queue!!!
Example
For example the idea of index checking!!!
Moral of the Story
Better to implement from scratch... when possible!!!
71 / 116
Images/cinvestav-1.jpg
Outline
1 A little bit more about Recursion
2 The QueuesThe Queue Interface
3 Basic ApplicationsChange the Order of RecursionRadix SortSimulating Waiting LinesWire Routing
4 ImplementationDerive From ArrayLinearListDerive From Chain ListFrom Scratch
72 / 116
Images/cinvestav-1.jpg
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
I �rstNode == at the beginning of the ChainI lastNode == the end of the Chain
Thus...
73 / 116
Images/cinvestav-1.jpg
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
I �rstNode == at the beginning of the ChainI lastNode == the end of the Chain
Thus...
firstNode lastNode
73 / 116
Images/cinvestav-1.jpg
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
I �rstNode == at the beginning of the ChainI lastNode == the end of the Chain
Thus...
73 / 116
Images/cinvestav-1.jpg
A Linked Implementation of a Queue
From our experience extending the Chain Class
We use two pointer for the front and the back of the chain:
I �rstNode == at the beginning of the ChainI lastNode == the end of the Chain
Thus...
73 / 116
Images/cinvestav-1.jpg
The Code For this Implementation
Sketch of the Code - Question: What is the problem with privateclass Node?
p u b l i c c l a s s LinkedQueue<Item> implements Queue<Item>{
p r i v a t e Node f i r s t N o d e ;p r i v a t e Node l a s tNode ;p r i v a t e i n t s i z e ;p u b l i c LinkedQueue ( ){
f i r s t N o d e = n u l l ;l a s tNode = n u l l ;s i z e = 0 ;
} // end d e f a u l t c o n s t r u c t o r
p r i v a t e c l a s s Node{
p r i v a t e Item data ; // en t r y i n queuep r i v a t e Node next ; // l i n k to next node
} // end Node} // end LinkedQueue
74 / 116
Images/cinvestav-1.jpg
Yes!!!
With private �elds
It is necessary to have public methods to access them!!!
setDataNode(Item Object)
setNextNode(Node newNode);
75 / 116
Images/cinvestav-1.jpg
Yes!!!
With private �elds
It is necessary to have public methods to access them!!!
setDataNode(Item Object)
setNextNode(Node newNode);
75 / 116
Images/cinvestav-1.jpg
Yes!!!
With private �elds
It is necessary to have public methods to access them!!!
setDataNode(Item Object)
setNextNode(Node newNode);
75 / 116
Images/cinvestav-1.jpg
Some Operations: Enqueue
We have always two cases
1 Adding to an empty Queue
2 Adding to a non-empty Queue
76 / 116
Images/cinvestav-1.jpg
Some Operations: Enqueue
We have always two cases
1 Adding to an empty Queue
2 Adding to a non-empty Queue
76 / 116
Images/cinvestav-1.jpg
Example: Adding to an Empty List
Before Inserting
firstNode lastNode
newNode
After Inserting
77 / 116
Images/cinvestav-1.jpg
Example: Adding to an Empty List
Before Inserting
firstNode lastNode
newNode
After Inserting
firstNode lastNode
77 / 116
Images/cinvestav-1.jpg
Example: Adding to a Non-Empty List
Create New Node
firstNode
lastNode
newNode
Make next from lastNode...
78 / 116
Images/cinvestav-1.jpg
Example: Adding to a Non-Empty List
Create New Node
firstNode
lastNode
newNode
Make next from lastNode...
firstNode
lastNode
newNode78 / 116
Images/cinvestav-1.jpg
Example: Adding to a Non-Empty List
Move lastNode
firstNode
lastNode
newNode
79 / 116
Images/cinvestav-1.jpg
Final Code
Code
p u b l i c vo i d enqueue ( Item newEntry ){
Node newNode = new Node ( newEntry , n u l l ) ;i f ( empty ( ) )
f i r s t N o d e = newNode ;e l s e
l a s tNode . setNextNode ( newNode ) ;l a s tNode = newNode ;
} // end enqueue
80 / 116
Images/cinvestav-1.jpg
What about Dequeue?
Point a temporary node to the front node
firstNode lastNode
temp
Point �rstNode to temp.next
81 / 116
Images/cinvestav-1.jpg
What about Dequeue?
Point a temporary node to the front node
firstNode lastNode
temp
Point �rstNode to temp.next
firstNode lastNode
temp
81 / 116
Images/cinvestav-1.jpg
Empty
Make temp.next = null
firstNode lastNode
temp
Return Node
firstNode lastNode
tempReturn Node
82 / 116
Images/cinvestav-1.jpg
Thus...
The Rest of Operations
You can think about them... they are not complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
83 / 116
Images/cinvestav-1.jpg
Thus...
The Rest of Operations
You can think about them... they are not complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
83 / 116
Images/cinvestav-1.jpg
From Scratch Using an Array!!!
If we can do the following...
first last
first
last
84 / 116
Images/cinvestav-1.jpg
A closer Look...
Somebody can notice something?
first
last
0
1
23 4
5
6
7
8
9
101112
13
14
15
85 / 116
Images/cinvestav-1.jpg
Direction of Reading
Repeating numbers in a certain range
first
last
0
1
23 4
5
6
7
8
9
101112
13
14
15
86 / 116
Images/cinvestav-1.jpg
How we can simulate this number repetition?
Actually there is function that can help
modm : N→{0,1,2, ...,m−1} (1)
Example for m = 5
n n modm
0 0
1 1
2 2
3 3
4 4
5 0
6 1
7 2
8 3
etc... ...
87 / 116
Images/cinvestav-1.jpg
How we can simulate this number repetition?
Actually there is function that can help
modm : N→{0,1,2, ...,m−1} (1)
Example for m = 5
n n modm
0 0
1 1
2 2
3 3
4 4
5 0
6 1
7 2
8 3
etc... ...
87 / 116
Images/cinvestav-1.jpg
Thus, we still we have two indexes
frontIndex
We need to know where to remove!!!
backIndex
We need to know where to add!!
88 / 116
Images/cinvestav-1.jpg
Thus, we still we have two indexes
frontIndex
We need to know where to remove!!!
backIndex
We need to know where to add!!
88 / 116
Images/cinvestav-1.jpg
Thus
If we want to add
backIndex = (backIndex + 1)% queue.length
If we want to remove
frontIndex = (frontIndex + 1)% queue.length
89 / 116
Images/cinvestav-1.jpg
Thus
If we want to add
backIndex = (backIndex + 1)% queue.length
If we want to remove
frontIndex = (frontIndex + 1)% queue.length
89 / 116
Images/cinvestav-1.jpg
Example
Adding stu� into the back
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
a
b
c de
90 / 116
Images/cinvestav-1.jpg
Example
Adding stu� into the back
frontIndexbackIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
a
b
c de
f
91 / 116
Images/cinvestav-1.jpg
Example
Adding stu� into the back
frontIndex
backIndex0
1
23 4
5
6
7
8
9
101112
13
14
15
a
b
c de
f
g
92 / 116
Images/cinvestav-1.jpg
Example
Remove stu� from the front
frontIndex
backIndex0
1
23 4
5
6
7
8
9
101112
13
14
15
b
c de
f
g
93 / 116
Images/cinvestav-1.jpg
Example
Remove stu� from the front
frontIndex
backIndex0
1
23 4
5
6
7
8
9
101112
13
14
15
c de
f
g
94 / 116
Images/cinvestav-1.jpg
Example
Keep Adding
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
c de
f
g
h
i
j
klm
n
95 / 116
Images/cinvestav-1.jpg
Example
The modulo helps here
frontIndex
backIndex=16 0
1
23 4
5
6
7
8
9
101112
13
14
15
c de
f
g
h
i
j
klm
n
o
p
96 / 116
Images/cinvestav-1.jpg
It looks Cool, but...
Problem 1 when full
frontIndex= 3
backIndex=18%16=2
0
1
23 4
5
6
7
8
9
101112
13
14
15
c de
f
g
h
i
j
klm
n
o
p
q
s
97 / 116
Images/cinvestav-1.jpg
But when we remove all elements
We go...
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15 h
i
j
klm
n
o
p
q
s
98 / 116
Images/cinvestav-1.jpg
But when we remove all elements
We go...
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
n
o
p
q
s
99 / 116
Images/cinvestav-1.jpg
This is the main problem
And here is the problem when removing s
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
s
100 / 116
Images/cinvestav-1.jpg
This is the main problem
Then full an empty cases cannot be identi�ed!!!
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
101 / 116
Images/cinvestav-1.jpg
A possible solution
Somewhat simple
Use an extra �eld �size�
Then each time
If frontIndex == (backIndex + 1) % queue.length ⇒ check size
I Then do something what?
103 / 116
Images/cinvestav-1.jpg
A possible solution
Somewhat simple
Use an extra �eld �size�
Then each time
If frontIndex == (backIndex + 1) % queue.length ⇒ check size
I Then do something what?
103 / 116
Images/cinvestav-1.jpg
Another solution!!!
Assume an space between the frontIndex and backIndex
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
c de
f
g
h
i
j
klm
n
o
p
q
104 / 116
Images/cinvestav-1.jpg
Thus,
When the queue is full
frontIndex == (backIndex + 2) % queue.length
When the queue is empty
frontIndex == (backIndex + 1) % queue.length
105 / 116
Images/cinvestav-1.jpg
Thus,
When the queue is full
frontIndex == (backIndex + 2) % queue.length
When the queue is empty
frontIndex == (backIndex + 1) % queue.length
105 / 116
Images/cinvestav-1.jpg
A solution!!!
Then for an empty queue
frontIndex
backIndex
0
1
23 4
5
6
7
8
9
101112
13
14
15
106 / 116
Images/cinvestav-1.jpg
Class Members
Code
p u b l i c c l a s s ArrayQueue<Item> implements Queue In t e r f a c e<Item>{
p r i v a t e Item [ ] queue ; // c i r c u l a r a r r a y o f queue e n t r i e s// and one unused l o c a t i o n
p r i v a t e i n t f r o n t I n d e x ;p r i v a t e i n t back Index ;p r i v a t e s t a t i c f i n a l i n t DEFAULT_INITIAL_CAPACITY = 50 ;
}
107 / 116
Images/cinvestav-1.jpg
Basic Constructors
Code
p u b l i c ArrayQueue ( ){
t h i s (DEFAULT_INITIAL_CAPACITY ) ;} // end d e f a u l t c o n s t r u c t o r
p u b l i c ArrayQueue ( i n t i n i t i a l C a p a c i t y ){// the c a s t i s s a f e because the new// a r r a y c o n t a i n s n u l l e n t r i e s// The 1 i s f o r empty e n t r yI tem [ ] tempQueue = ( Item [ ] ) new Object [ i n i t i a l C a p a c i t y + 1 ] ;queue = tempQueue ;
f r o n t I n d e x = 0 ;back Index = i n i t i a l C a p a c i t y ;
} // end c o n s t r u c t o r
108 / 116
Images/cinvestav-1.jpg
At the Beginning
At the Instantiation of the Object Queue
firstIndex==0
bastIndex==15
0
1
23 4
5
6
7
8
9
101112
13
14
15
109 / 116
Images/cinvestav-1.jpg
Enqueue in a circular array
Code
p u b l i c vo i d enqueue ( Item newEntry ){
i f ( f r o n t I n d e x == ( ( back Index + 2) % queue . l e n g t h ) ){
<Something Here . . . . >
}back Index = ( back Index + 1) % queue . l e n g t h ;queue [ back Index ] = newEntry ;
} // end enqueue
110 / 116
Images/cinvestav-1.jpg
What is this �Something Here�?
Expanding Array and Copying values
frontIndex
backIndex
0 1 2 3 4 6 7
0 1 2 3 4 6 7 8 9 11 12 13 14 15
3
1
5
COPY Empty Space
111 / 116
Images/cinvestav-1.jpg
Dequeue
Use a temporary pointer �front�0 1 2 3 4 6 7
frontIndex
backIndex
2
6
front
to be returned
113 / 116
Images/cinvestav-1.jpg
Dequeue
Remove the element0 1 2 3 4 6 7
frontIndex
backIndex
3
6
front
to be returned
null
114 / 116
Images/cinvestav-1.jpg
Enqueue in a circular array
Code
p u b l i c I tem dequeue ( ){
Item f r o n t = n u l l ;i f ( ! empty ( ) ){
f r o n t = queue [ f r o n t I n d e x ] ;queue [ f r o n t I n d e x ] = n u l l ;f r o n t I n d e x = ( f r o n t I n d e x + 1) % queue . l e n g t h ;
} // End I fr e t u r n f r o n t ;
} // end dequeue
115 / 116
Images/cinvestav-1.jpg
Thus...
The Rest of Operations
You can think about them... they are not so complex...
Complexities
Operation in Scratch Queue using Chains Complexity
empty() O(1)
front() O(1)
rear() O(1)
Enqueue(TheObject) O(1)
Dequeue() O(1)
116 / 116