cscs-200 data structure and algorithms
TRANSCRIPT
What is a stack?
It is an ordered group of homogeneous items of elements.
Elements are added to and removed from the top of the stack (the most
recently added items are at the top of the stack).
The last element to be added is the first to be removed (LIFO: Last In, First
Out).
Stacks
AL4
Stacks in real life: stack of books, stack of plates
Add new items at the top
Remove an item at the top
Stack data structure similar to real life: collection of
elements arranged in a linear order.
Can only access element at the top
Stack Specification
Definitions: (provided by the user)
MAX_ITEMS: Max number of items that might be on the stack
ItemType: Data type of the items on the stack
Operations
MakeEmpty
Boolean IsEmpty
Boolean IsFull
Push (ItemType newItem)
Pop (ItemType& item)
Stack Operations
AL6
Push(X) – insert X as the top element of the stack
Pop() – remove the top element of the stack and
return it.
Top() – return the top element without removing it
from the stack.
Push (ItemType newItem)
Function: Adds newItem to the top of the stack.
Preconditions: Stack has been initialized and is
not full.
Postconditions: newItem is at the top of the
stack.
Pop (ItemType& item)
Function: Removes topItem from stack and returns it in item.
Preconditions: Stack has been initialized and is not empty.
Postconditions: Top element has been removed from stack and
item is a copy of the removed element.
Stack Operations
AL9
push(2)
top 2
push(5)
top
2
5
push(7)
top
2
5
7
push(1)
top
2
5
7
1
1 pop()
top
2
5
7
push(21)
top
2
5
7
21
21 pop()
top
2
5
7
7 pop()
2
5top
5 pop()
2top
Stack Operation
AL10
The last element to go into the stack is the first to come out: LIFO – Last In First Out.
What happens if we call pop() and there is no element?
Have IsEmpty() boolean function that returns true if stack is empty, false otherwise.
Throw StackEmpty exception: advanced C++/JAVA concept.
Stack Implementation: Array
AL11
Worst case for insertion and deletion from an array
when insert and delete from the beginning: shift
elements to the left.
Best case for insert and delete is at the end of the
array – no need to shift any elements.
Implement push() and pop() by inserting and deleting
at the end of an array.
Stack using an Array
AL13
In case of an array, it is possible that the array may
“fill-up” if we push enough elements.
Have a boolean function IsFull() which returns
true if stack (array) is full, false otherwise.
We would call this function before calling push(x).
Stack Operations with Array
AL14
public int pop()
{
return A[current--];
}
public void push(int x)
{
A[++current] = x;
}
Stack Operations with Array
AL15
public int top(){
return A[current];} public int IsEmpty(){
return ( current == -1 );}public int IsFull(){
return ( current == size-1);}
A quick examination shows that all five operations take constant time.
Use of Stack
16
Example of use: prefix, infix, postfix expressions.
Consider the expression A+B: we think of applying the operator “+” to the operands A and B.
“+” is termed a binary operator: it takes two operands.
Writing the sum as A+B is called the infix form of the expression.
Prefix, Infix, Postfix
17
Two other ways of writing the expression are
+ A B prefixA B + postfix
The prefixes “pre” and “post” refer to the position of
the operator with respect to the two operands.
Prefix, Infix, Postfix
18
Consider the infix expression
A + B * C
We “know” that multiplication is done before addition.
The expression is interpreted as
A + ( B * C )
Multiplication has precedence over addition.
Prefix, Infix, Postfix
20
Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
Prefix, Infix, Postfix
21
Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
A ( B C * ) + convert addition
Prefix, Infix, Postfix
22
Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
A ( B C * ) + convert addition
A B C * + postfix form
Prefix, Infix, Postfix
24
Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
Prefix, Infix, Postfix
25
Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
( A B + ) C * convert multiplication
Prefix, Infix, Postfix
26
Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
( A B + ) C * convert multiplication
A B + C * postfix form
Precedence of Operators
27
The five binary operators are: addition, subtraction,
multiplication, division and exponentiation.
The order of precedence is (highest to lowest)
Exponentiation
Multiplication/division *, /
Addition/subtraction +, -
Precedence of Operators
28
For operators of same precedence, the left-to-right rule applies:
A+B+C means (A+B)+C.
For exponentiation, the right-to-left rule applies
A B C means A ( B C )
Infix to Postfix
29
Infix Postfix
A + B A B +
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) A B + C D – *
A B * C – D + E/F A B C*D – E F/+
Infix to Postfix
30
Note that the postfix form an expression does not
require parenthesis.
Consider „4+3*5‟ and „(4+3)*5‟. The parenthesis are
not needed in the first but they are necessary in the
second.
The postfix forms are:
4+3*5 435*+
(4+3)*5 43+5*
Evaluating Postfix
31
Each operator in a postfix expression refers to the
previous two operands.
Each time we read an operand, we push it on a
stack.
When we reach an operator, we pop the two
operands from the top of the stack, apply the
operator and push the result back on the stack.
Evaluating Postfix
32
Stack s;
while( not end of input ) {
e = get next element of input
if( e is an operand )
s.push( e );
else {
op2 = s.pop();
op1 = s.pop();
value = result of applying operator „e‟ to op1 and op2;
s.push( value );
}
}
finalresult = s.pop();
Evaluating Postfix
35
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
Evaluating Postfix
36
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
Evaluating Postfix
37
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
Evaluating Postfix
38
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
Evaluating Postfix
39
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
Evaluating Postfix
40
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
Evaluating Postfix
41
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
Evaluating Postfix
42
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
Evaluating Postfix
43
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
Evaluating Postfix
44
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
Evaluating Postfix
45
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
Evaluating Postfix
46
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
3 7 2 49 49,3
Evaluating Postfix
47
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
Evaluating Postfix
48
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
Converting Infix to Postfix
49
Consider the infix expressions „A+B*C‟ and „
(A+B)*C‟.
The postfix versions are „ABC*+‟ and „AB+C*‟.
The order of operands in postfix is the same as the
infix.
In scanning from left to right, the operand „A‟ can be
inserted into postfix expression.
Converting Infix to Postfix
50
The „+‟ cannot be inserted until its second operand has been scanned and inserted.
The „+‟ has to be stored away until its proper position is found.
When „B‟ is seen, it is immediately inserted into the postfix expression.
Can the „+‟ be inserted now? In the case of „A+B*C‟ cannot because * has precedence.
Converting Infix to Postfix
51
In case of „(A+B)*C‟, the closing parenthesis
indicates that „+‟ must be performed first.
Assume the existence of a function „prcd(op1,op2)‟
where op1 and op2 are two operators.
Prcd(op1,op2) returns TRUE if op1 has precedence
over op2, FASLE otherwise.
Converting Infix to Postfix
52
prcd(„*‟,‟+‟) is TRUE
prcd(„+‟,‟+‟) is TRUE
prcd(„+‟,‟*‟) is FALSE
Here is the algorithm that converts infix expression
to its postfix form.
The infix expression is without parenthesis.
Converting Infix to Postfix
53
1. Stack s;
2. While( not end of input ) {
3. c = next input character;
4. if( c is an operand )
5. add c to postfix string;
6. else {
7. while( !s.empty() && prcd(s.top(),c) ){
8. op = s.pop();
9. add op to the postfix string;
10. }
11. s.push( c );
12. }
13. while( !s.empty() ) {
14. op = s.pop();
15. add op to postfix string;
16. }
Converting Infix to Postfix
58
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
Converting Infix to Postfix
59
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC * +
Converting Infix to Postfix
60
Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC * +
ABC * +
Converting Infix to Postfix
61
Handling parenthesis
When an open parenthesis „(„ is read, it must be
pushed on the stack.
This can be done by setting prcd(op,„(„ ) to be
FALSE.
Also, prcd( „(„,op ) == FALSE which ensures that an
operator after „(„ is pushed on the stack.
Converting Infix to Postfix
62
When a „)‟ is read, all operators up to the first „(„ must
be popped and placed in the postfix string.
To do this, prcd( op,‟)‟ ) == TRUE.
Both the „(„ and the „)‟ must be discarded: prcd( „(„,‟)‟ )
== FALSE.
Need to change line 11 of the algorithm.
Converting Infix to Postfix
63
if( s.empty() || symb != „)‟ ) s.push( c );
elses.pop(); // discard the „(„
prcd( „(„, op ) = FALSE for any operator
prcd( op, „)‟ ) = FALSE for any operator other than „)‟
prcd( op, „)‟ ) = TRUE for any operator other than „(„
prcd( „)‟, op ) = error for any operator.
Lab Exercise
Create a stack of integers with ten elements. Program should
output as follows:
Print the elements of the stack.
Remove 5 elements from the stack.
Print the elements of stack again.
Insert 2 more elements.
Print the elements of stack.
Increase each element by 1 and print again.
What is a queue? It is an ordered group of homogeneous items of elements.
Queues have two ends:
Elements are added at one end.
Elements are removed from the other end.
The element added first is also removed first (FIFO: First In,
First Out).
Queue
Initially q.rear = -1
When is queue empty?
Q.rear < q.front
Total Number of Elements = q.rear –q.front + 1
An Implementation Problem
Consider a 5 element queue
Initially q.front = 0 and q.rear = -1
After Inserting 3 elements delete two elements and again
insert two elements.
Can you insert any more?
One Solution
After one removal move all elements one step up and fix
front = 0.
In this case no front variable is needed.
But not an efficient solution. Why?
Queue Specification
Definitions: (provided by the user)
MAX_ITEMS: Max number of items that might be on the queue
ItemType: Data type of the items on the queue
Operations MakeEmpty
Boolean IsEmpty
Boolean IsFull
Enqueue (ItemType newItem)
Dequeue (ItemType& item)
Enqueue (ItemType newItem) Function: Adds newItem to the rear of the queue.
Preconditions: Queue has been initialized and is not full.
Postconditions: newItem is at rear of queue.
Dequeue (ItemType& item)
Function: Removes front item from queue and returns it in
item.
Preconditions: Queue has been initialized and is not empty.
Postconditions: Front element has been removed from queue
and item is a copy of removed element.
Implementation issues Implement the queue as a circular structure.
How do we know if a queue is full or empty?
Initialization of front and rear.
Testing for a full or empty queue.
Queue using Array
73
If we use an array to hold queue elements, both insertions and removal at the front (start) of the array are expensive.
This is because we may have to shift up to “n” elements.
For the stack, we needed only one end; for queue we need both.
To get around this, we will not shift upon removal of an element.
Queue using Array
79
front
25
rear
65 7
2
0 1 32 4
front
5 2
7
rear
enqueue(9)
enqueue(12)
66
88
99
1212
enqueue(21) ??
Queue using Array
80
We have inserts and removal running in constant
time but we created a new problem.
Cannot insert new elements even though there are
two places available at the start of the array.
Solution: allow the queue to “wrap around”.
Queue using Array
81
Basic idea is to picture the array as a circular array.
front
25
rear2
front
7
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
268
9
12
Queue using Array
82
public void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
front
25
rear2
front
0
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
268
9
12
enqueue(21)
21
218
size
7
noElements
Queue using Array
83
public int isFull()
{
return noElements == size;
}
public int isEmpty()
{
return noElements == 0;
}
front
25
rear2
front
1
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
268
9
12
enqueue(7)
21
218
size
8
noElements
7
7
Queue using Array
84
public int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
front rear4
front
1
rear
6 8 9 12
6
5
7
0 1
3
2
4
689
12
dequeue()
21
218
size
6
noElements
7
7
Use of Queues
85
Out of the numerous uses of the queues, one of the
most useful is simulation.
A simulation program attempts to model a real-world
phenomenon.
Many popular video games are simulations, e.g.,
SimCity, FlightSimulator
Each object and action in the simulation has a
counterpart in real world.
Uses of Queues
86
If the simulation is accurate, the result of the
program should mirror the results of the real-world
event.
Thus it is possible to understand what occurs in the
real-world without actually observing its occurrence.
Let us look at an example. Suppose there is a bank
with four tellers.
Stack Using Linked List
We can avoid the size limitation of a stack
implemented with an array by using a linked list to
hold the stack elements.
As with array, however, we need to decide where to
insert elements in the list and where to delete them
so that push and pop will run the fastest.
Stack Using Linked List
For a singly-linked list, insert at start or end takes
constant time using the head and current
pointers respectively.
Removing an element at the start is constant
time but removal at the end required traversing
the list to the node one before the last.
Make sense to place stack elements at the start
of the list because insert and removal are
constant time.
Stack Operation: Listpublic int pop()
{
int x = head.get();
ListNode p = head;
head = head.getNext();
return x;
}
top
2
5
71 7 5 2
head
Stack Operation: List
public void push(int x)
{
ListNode newNode = new Node();
newNode.set(x);
newNode.setNext(head);
head = newNode;
}
top
2
5
7
9
7 5 2
head
push(9)
9
newNode
Stack Operation: List
public int top(){
return head->get();} public int IsEmpty(){
return ( head == NULL );}
• All four operations take constant time.
We implement IsFull() function only in array implementationbecause we expect enough memory will be available.
Stack: Array or List
• Since both implementations support stack operations in constant time, any reason to choose one over the other?
• Allocating and deallocating memory for list nodes does take more time than preallocated array.
• List uses only as much memory as required by the nodes; array requires allocation ahead of time.
• List pointers (head, next) require extra memory.
• Array has an upper limit; List is limited by dynamic memory allocation.
Implementing Queue
Using linked List: Recall
Insert works in constant time for either end of a
linked list.
Remove works in constant time only.
Seems best that head of the linked list be the front of
the queue so that all removes will be from the front.
Inserts will be at the end of the list.
Implementing Queue
Using linked List:
front
2571 1 7 5 2
frontrear rear
front
257 1 7 5 2
frontrear rear
dequeue()
Implementing Queue
Using linked List:
front
2571 1 7 5 2
frontrear rear
front
257 97 5 2
frontrear rear
enqueue(9)
9
Implementing Queuepublic int dequeue(){
int x = front.get();ListNode p = front;front = front.getNext();return x;
}public void enqueue(int x){
ListNode newNode = new ListNode();newNode.set(x);newNode.setNext(NULL);rear.setNext(newNode);rear = newNode;
}
Implementing Queue
public int front()
{
return front->get();
}
public int isEmpty()
{
return ( front == NULL );
}
Simulation of a Bank
A customer enters the bank at a specific time (t1) desiring to conduct a transaction.
Any one of the four tellers can attend to the customer.
The transaction (withdraw, deposit) will take a certain period of time (t2).
If a teller is free, the teller can process the customer‟s transaction immediately and the customer leaves the bank at t1+t2.
Simulation of a Bank
A customer enters the bank at a specific time (t1) desiring to conduct a transaction.
Any one of the four tellers can attend to the customer.
The transaction (withdraw, deposit) will take a certain period of time (t2).
If a teller is free, the teller can process the customer‟s transaction immediately and the customer leaves the bank at t1+t2.
Simulation of a Bank
It is possible that none of the four tellers is free in
which case there is a line of customers are each
teller.
An arriving customer proceeds to the back of the
shortest line and waits for his turn.
The customer leaves the bank at t2 time units after
reaching the front of the line.
The time spent at the bank is t2 plus time waiting in
line.
Simulation Models
Two common models of simulation are time-based
simulation and event-based simulation.
In time-based simulation, we maintain a timeline or a
clock.
The clock ticks. Things happen when the time
reaches the moment of an event.
Timeline based Simulation
Consider the bank example. All tellers are free.
Customer C1 comes in at time 2 minutes after
bank opens.
His transaction (withdraw money) will require 4
minutes.
Customer C2 arrives 4 minutes after the bank
opens. Will need 6 minutes for transaction.
Customer C3 arrives 12 minutes after the bank
opens and needs 10 minutes.
Timeline based Simulation
Events along the timeline:
10 11108765432 15141312
C2 in
C1 in C1 out
C2 out
C3 in
Time (minutes)
Timeline based Simulation
We could write a main clock loop as follows:
clock = 0;
while( clock <= 24*60 ) { // one day
read new customer;
if customer.arrivaltime == clock
insert into shortest queue;
check the customer at head of all four queues.
if transaction is over, remove from queue.
clock = clock + 1;
}
Event based Simulation
Don‟t wait for the clock to tic until the next event.
Compute the time of next event and maintain a list of
events in increasing order of time.
Remove a event from the list in a loop and process
it.
Event based Simulation
Events
10 11108765432 15141312
C2 in
C1 in C1 out
C2 out
C3 in
Time (minutes)
Event 1: 2 mins C1 in
Event 2: 4 mins C2 in
Event 3: 6 mins C1 out
Event 4: 10 mins C2 out
Event 5: 12 mins C3 in
Event based Simulation
Maintain a queue of events.
Remove the event with the earliest time from the
queue and process it.
As new events are created, insert them in the queue.
A queue where the dequeue operation depends not
on FIFO, is called a priority queue.
Event based Bank Simulation
Development of the C++ code to carry out the
simulation.
We will need the queue data structure.
We will need the priority queue.
Information about arriving customers will be placed
in an input file.
Each line of the file contains the items (arrival
time,transaction duration)
Arriving Customers‟ File
Here are a few lines from the input file.
00 30 10 <- customer 1
00 35 05 <- customer 2
00 40 08
00 45 02
00 50 05
00 55 12
01 00 13
01 01 09
“00 30 10” means Customer 1 arrives 30 minutes after bank opens and will need 10 minutes for his transaction.
“01 01 09” means customer arrives one hour and one minute after bank opens and transaction will take 9 minutes.
Simulation Procedure
The first event to occur is the arrival of the first customer.
This event placed in the priority queue.
Initially, the four teller queues are empty.
The simulation proceeds are follows:
When an arrival event is removed from the priority queue, a node representing the customer is placed on the shortest teller queue.
Simulation Procedure
If that customer is the only one on a teller queue, a event for his departure is placed on the priority queue.
At the same time, the next input line is read and an arrival event is placed in the priority queue.
When a departure event is removed from the event priority queue, the customer node is removed from the teller queue.
Simulation Procedure
• The total time spent by the customer is computed: it is the time spent in the queue waiting and the time taken for the transaction.
• This time is added to the total time spent by all customers.
• At the end of the simulation, this total time divided by the total customers served will be average time spent by customers.
• The next customer in the queue is now served by the teller.
• A departure event is placed on the event queue.