Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
2. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
2 / 116
14. Images/cinvestav-
Recursion ≈ Depth First Search
Actually
This is the classic order when recursion is done!!!
What if...
We need a dierent order?
14 / 116
15. Images/cinvestav-
Recursion ≈ Depth First Search
Actually
This is the classic order when recursion is done!!!
What if...
We need a dierent order?
14 / 116
17. Images/cinvestav-
Queues
Denition 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 implement
a queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST
ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
18. Images/cinvestav-
Queues
Denition 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 implement
a queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST
ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
19. Images/cinvestav-
Queues
Denition 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 implement
a queue?
IMPORTANT
IN A QUEUE, THE FIRST ITEM INSERTED WILL BE THE FIRST
ITEM DELETED: FIFO (FIRST-IN, FIRST-OUT)
16 / 116
20. Images/cinvestav-
Denition
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
21. Images/cinvestav-
Denition
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
22. Images/cinvestav-
Denition
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
34. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
29 / 116
35. Images/cinvestav-
Queue Interface
Code
p u b l i c i n t e r f a c e QueueItem {
p u b l i c b o o l e a n empty ( ) ;
p u b l i c I t e m f r o n t ( ) ;
p u b l i c I t e m r e a r ( ) ;
p u b l i c I t e m Dequeue ( ) ;
p u b l i c v o i d Enqueue ( I t e m t h e O b j e c t ) ;
p u b l i c i n t s i z e ( ) ;
}
30 / 116
36. Images/cinvestav-
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 without
removing it.
Precondition: The queue is not empty.
31 / 116
37. Images/cinvestav-
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 without
removing it.
Precondition: The queue is not empty.
31 / 116
38. Images/cinvestav-
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 without
removing it.
Precondition: The queue is not empty.
31 / 116
39. Images/cinvestav-
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue without
removing 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
40. Images/cinvestav-
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue without
removing 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
41. Images/cinvestav-
Explanation
Example
BC AC
public Item rear()
Return the value of the item at the rear of the queue without
removing 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
42. Images/cinvestav-
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 that
was added immediately after the element just popped or the
queue is empty.
33 / 116
43. Images/cinvestav-
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 that
was added immediately after the element just popped or the
queue is empty.
33 / 116
44. Images/cinvestav-
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 that
was added immediately after the element just popped or the
queue is empty.
33 / 116
45. Images/cinvestav-
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 that
was added immediately after the element just popped or the
queue is empty.
33 / 116
48. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
49. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
50. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
51. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
52. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
53. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
54. Images/cinvestav-
Applications of Queues
Direct applications
Waiting lists
Queue Theory for Networking
Bureaucracy Access to shared resources (e.g., printer)
Multiprogramming
Schedulers
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
35 / 116
55. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
36 / 116
58. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
39 / 116
59. Images/cinvestav-
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signicative number
to most signicative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
60. Images/cinvestav-
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signicative number
to most signicative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
61. Images/cinvestav-
Radix Sort Using Bins
Example
Order ten 2 digit numbers in 10 bins (0-9) from least signicative number
to most signicative number.
Digits
91,06,85,15,92,35,30,22,39
Let us do it
In the board!!!
40 / 116
63. Images/cinvestav-
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 bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
64. Images/cinvestav-
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 bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
65. Images/cinvestav-
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 bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
66. Images/cinvestav-
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 bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
67. Images/cinvestav-
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 bins
determined by the 10's digit (10
1)
Finally
Dequeue the values from the queue 0 to queue 9.
Put values in a list in that order.
42 / 116
69. Images/cinvestav-
Code
We need something else
p u b l i c s t a t i c R a d i x S o r t ( L i n e a r L i s t Element , i n t k ) {
i n t R a d i x = 10
i n t power = 1
i n t d i g i t ;
QueueI n t e g e r [ ] d i g i t Q u e u e = ( QueueI 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 E l e m e n t . s i z e ( ) ; j ++){
d i g i t = E l e m e n t . remove ( 0 ) ;
d i g i t Q u e u e [ ( d i g i t / power ) % 1 0 ] .
e n q u e u e ( d i g i t ) ;
}
f o r ( i n t j =0; j R a d i x ; j ++){
// WHAT?
}
power ∗= 1 0 ;
}
}
43 / 116
70. Images/cinvestav-
Radix Sort: Complexity
Lemma 1
Given n d-digit numbers in which each digit can take on up to k possible
values, RADIX-SORT correctly sorts these numbers in O(d(n +k)) time.
44 / 116
71. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
45 / 116
72. Images/cinvestav-
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
73. Images/cinvestav-
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
74. Images/cinvestav-
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
75. Images/cinvestav-
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 passenger
2 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
76. Images/cinvestav-
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 passenger
2 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
77. Images/cinvestav-
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 passenger
2 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
78. Images/cinvestav-
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 passenger
2 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
79. Images/cinvestav-
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 passenger
2 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
80. Images/cinvestav-
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 passenger
2 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
81. Images/cinvestav-
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 passenger
2 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
82. Images/cinvestav-
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 passenger
2 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
83. Images/cinvestav-
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 passenger
2 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
84. Images/cinvestav-
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 passenger
2 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
85. Images/cinvestav-
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 passenger
2 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
88. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
49 / 116
100. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
59 / 116
103. Images/cinvestav-
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
104. Images/cinvestav-
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
105. Images/cinvestav-
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
106. Images/cinvestav-
Shift the front and rear pointers!!!
We have
Front
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
107. Images/cinvestav-
Shift the front and rear pointers!!!
We have
Front
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
108. Images/cinvestav-
Shift the front and rear pointers!!!
We have
Front
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
109. Images/cinvestav-
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
110. Images/cinvestav-
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
111. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
64 / 116
112. Images/cinvestav-
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 is
the rear!!!
65 / 116
113. Images/cinvestav-
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 is
the rear!!!
65 / 116
114. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
115. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
116. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
117. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
118. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
119. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
120. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
121. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
122. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
123. Images/cinvestav-
We have the following
Operations
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0) No problem here
O(1) time
What about these ones
rear() = get(size()-1)
O(size) time
Enqueue(theObject) = add(0,TheObject)
O(1) time
Dequeue() = remove(size()-1)
O(size) time
66 / 116
124. Images/cinvestav-
Not Good At ALL
Even
If we change the front and the rear we do not get the performance we
want!!!
67 / 116
125. Images/cinvestav-
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()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
126. Images/cinvestav-
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()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
127. Images/cinvestav-
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()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
128. Images/cinvestav-
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()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
129. Images/cinvestav-
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()
This returns the element pointed by the lastNode
append(TheObject)
append elements at the end of the list
68 / 116
130. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
131. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
132. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
133. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
134. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
Queue.empty() = super.isEmpty()
O(1) time
front() = get(0)
O(1) time
69 / 116
135. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
136. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
137. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
138. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
139. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
140. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
141. Images/cinvestav-
Derive From Chain
We have
When the front is the left end of list and the rear is the right
end
rear() = getLast() . . . new method
O(1) time
Enqueue(theObject) = append(theObject) . . . new method
O(1) time
Dequeue() = remove(0)
O(1) time 70 / 116
142. Images/cinvestav-
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
143. Images/cinvestav-
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
144. Images/cinvestav-
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
145. Images/cinvestav-
Outline
1 A little bit more about Recursion
2 The Queues
The Queue Interface
3 Basic Applications
Change the Order of Recursion
Radix Sort
Simulating Waiting Lines
Wire Routing
4 Implementation
Derive From ArrayLinearList
Derive From Chain List
From Scratch
72 / 116
146. Images/cinvestav-
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:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
73 / 116
147. Images/cinvestav-
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:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
firstNode lastNode
73 / 116
148. Images/cinvestav-
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:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
73 / 116
149. Images/cinvestav-
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:
rstNode == at the beginning of the Chain
lastNode == the end of the Chain
Thus...
73 / 116
150. Images/cinvestav-
The Code For this Implementation
Sketch of the Code - Question: What is the problem with private
class Node?
p u b l i c c l a s s LinkedQueueItem i m p l e m e n t s QueueItem
{
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 t N o d e ;
p r i v a t e i n t s i z e ;
p u b l i c L i n k e d Q u e u e ( ) {
f i r s t N o d e = n u l l ;
l a s t N o d e = 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 I t e m d a t a ; // entry in queue
p r i v a t e Node n e x t ; // l i n k to next node
} // end Node
} // end LinkedQueue
74 / 116
159. Images/cinvestav-
Example: Adding to a Non-Empty List
Create New Node
firstNode
lastNode
newNode
Make next from lastNode...
firstNode
lastNode
newNode
78 / 116
161. Images/cinvestav-
Final Code
Code
p u b l i c v o i d e n q u e u e ( I t e m n e w E n t r y )
{
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 t N o d e . s e t N e x t N o d e ( newNode ) ;
l a s t N o d e = newNode ;
} // end enqueue
80 / 116
166. Images/cinvestav-
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
167. Images/cinvestav-
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
171. Images/cinvestav-
How we can simulate this number repetition?
Actually there is function that can help
mod m : N → {0,1,2,...,m −1} (1)
Example for m = 5
n n mod m
0 0
1 1
2 2
3 3
4 4
5 0
6 1
7 2
8 3
etc... ...
87 / 116
172. Images/cinvestav-
How we can simulate this number repetition?
Actually there is function that can help
mod m : N → {0,1,2,...,m −1} (1)
Example for m = 5
n n mod m
0 0
1 1
2 2
3 3
4 4
5 0
6 1
7 2
8 3
etc... ...
87 / 116
173. Images/cinvestav-
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
174. Images/cinvestav-
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
175. Images/cinvestav-
Thus
If we want to add
backIndex = (backIndex + 1)% queue.length
If we want to remove
frontIndex = (frontIndex + 1)% queue.length
89 / 116
176. Images/cinvestav-
Thus
If we want to add
backIndex = (backIndex + 1)% queue.length
If we want to remove
frontIndex = (frontIndex + 1)% queue.length
89 / 116
184. Images/cinvestav-
It looks Cool, but...
Problem 1 when full
frontIndex= 3
backIndex=18%16=2
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
h
i
j
kl
m
n
o
p
q
s
97 / 116
185. Images/cinvestav-
But when we remove all elements
We go...
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15 h
i
j
kl
m
n
o
p
q
s
98 / 116
186. Images/cinvestav-
But when we remove all elements
We go...
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
n
o
p
q
s
99 / 116
187. Images/cinvestav-
This is the main problem
And here is the problem when removing s
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
s
100 / 116
188. Images/cinvestav-
This is the main problem
Then full an empty cases cannot be identied!!!
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
101 / 116
190. Images/cinvestav-
A possible solution
Somewhat simple
Use an extra eld size
Then each time
If frontIndex == (backIndex + 1) % queue.length ⇒ check size
Then do something what?
103 / 116
191. Images/cinvestav-
A possible solution
Somewhat simple
Use an extra eld size
Then each time
If frontIndex == (backIndex + 1) % queue.length ⇒ check size
Then do something what?
103 / 116
192. Images/cinvestav-
Another solution!!!
Assume an space between the frontIndex and backIndex
frontIndex
backIndex
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
c d
e
f
g
h
i
j
kl
m
n
o
p
q
104 / 116
193. Images/cinvestav-
Thus,
When the queue is full
frontIndex == (backIndex + 2) % queue.length
When the queue is empty
frontIndex == (backIndex + 1) % queue.length
105 / 116
194. Images/cinvestav-
Thus,
When the queue is full
frontIndex == (backIndex + 2) % queue.length
When the queue is empty
frontIndex == (backIndex + 1) % queue.length
105 / 116
196. Images/cinvestav-
Class Members
Code
p u b l i c c l a s s ArrayQueueItem i m p l e m e n t s Q u e u e I n t e r f a c e Item
{
p r i v a t e I t e m [ ] queue ; // c i r c u l a r array of 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 b a c k I n d e x ;
p r i v a t e s t a t i c f i n a l i n t DEFAULT_INITIAL_CAPACITY = 5 0 ;
}
107 / 116
197. Images/cinvestav-
Basic Constructors
Code
p u b l i c A r r ay Q u eu e ( )
{
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 A r r ay Q u eu e ( i n t i n i t i a l C a p a c i t y )
{
// the cast i s s a f e because the new
// array 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 entry
I t e m [ ] tempQueue = ( I t e m [ ] ) new O b j e c t [ 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 ;
b a c k I n d e x = 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
198. Images/cinvestav-
At the Beginning
At the Instantiation of the Object Queue
firstIndex==0
bastIndex==15
0
1
2
3 4
5
6
7
8
9
10
1112
13
14
15
109 / 116
199. Images/cinvestav-
Enqueue in a circular array
Code
p u b l i c v o i d e n q u e u e ( I t e m n e w E n t r y )
{
i f ( f r o n t I n d e x == ( ( b a c k I n d e x + 2 ) % queue . l e n g t h ) )
{
S o m e t h i n g Here . . . .
}
b a c k I n d e x = ( b a c k I n d e x + 1 ) % queue . l e n g t h ;
queue [ b a c k I n d e x ] = n e w E n t r y ;
} // end enqueue
110 / 116
200. Images/cinvestav-
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
204. Images/cinvestav-
Enqueue in a circular array
Code
p u b l i c I t e m d e q u e u e ( )
{
I t e m 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 f
r e t u r n f r o n t ;
} // end dequeue
115 / 116
205. Images/cinvestav-
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
206. Images/cinvestav-
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