Pmscs 623p Lecture 6
Pmscs 623p Lecture 6
Algorithm
(PMSCS 623P)
Lecture 6
Data Structures
Stacks
A stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack.
This means that elements are removed from a stack in the reverse order of
that in which they were inserted into the stack.
2
An Example of a Stack
top 2
top 8 8
top 1 1 1
Push(8) Push(2)
7 7 7
2 2 2
pop()
top 8
top 1
1
top 7 7 pop()
2
pop() 7
2
2
Data Structures
Procedure 6.1: Write a procedure that pushes an ITEM onto a stack.
4
Data Structures
Procedure 6.2: Write a procedure that deletes the top element of a stack.
5
Data Structures
Solved Problem 6.1: Consider the following stack of characters, where
STACK is allocated N=8 memory cells:
STACK: A, C, D, F, K, -, -, -
Describe the stack as the following operations take place:
i) POP (STACK, ITEM) v) POP (STACK, ITEM)
ii) POP (STACK, ITEM) vi) PUSH(STACK, R)
iii) PUSH(STACK, L) vii) PUSH(STACK, S)
iv) PUSH(STACK, P) viii) POP (STACK, ITEM)
Solution:
i) STACK: A, C, D, F, -, -, -, - v) STACK: A, C, D, L, -,
-, -, -
ii) STACK: A, C, D, -, -, -, -, - vi) STACK: A, C, D, L, R,
-, -, -
iii) STACK: A, C, D, L, -, -, -, - vii) STACK: A, C, D, L, R,
S, -, -
iv) STACK: A, C, D, L, P, -, -, - viii) STACK: A, C, D, L, 6
R, -, -, -
Data Structures
ARITHMETIC EXPRESSIONS; POLISH NOTATION
Infix notation: The operator symbol is placed between its two operands.
Example: A+B, A-B, A*B, A/B
Prefix notation: The operator symbol is placed before its two operands.
Example: +AB, -AB, *AB, /AB
This notation is also known as Polish notation, named after the
Polish mathematician Jan Lukasiewicz.
The fundamental property of Polish notation is that the order in
which the operations are to be performed is completely determined
by the positions of the operators and operands in the expression.
Accordingly, one never needs parentheses when writing expressions
in Polish notation.
7
Data Structures
ARITHMETIC EXPRESSIONS; POLISH NOTATION
Postfix notation: The operator symbol is placed after its two operands.
Example: AB+, AB-, AB*, AB/
8
Data Structures (Operator Precedence)
9
Data Structures
Example 6.7: Transform the following arithmetic infix expression Q
into its equivalent postfix expression P:
Q: A + (B*C - (D /E ↑ F)*G)*H
Solution:
Push “(” onto stack and then add “)” to the end of Q. Thus, Q becomes
Q: A + ( B * C - ( D / E ↑ F ) * G ) * H )
Start scanning. The following table shows the status of STACK and of the
string P as each element of Q is scanned.
10
Data Structures
Example 6.7: Transform the following arithmetic infix expression Q
into its equivalent postfix expression P:
Q: A + (B*C - (D /E ↑ F)*G)*H)
Solution:
11
Data Structures
Example 6.7: Transform . . . . . postfix expression P:
Q: A + (B*C - (D /E ↑ F)*G)*H)
Symbol Scanned STACK Expression, P
D (+( - ( A B C * D
/ (+( - ( / A B C * D
E (+( - ( / A B C * D E
↑ (+( - ( / ↑ A B C * D E
F (+( - ( / ↑ A B C * D E F
) (+( - A B C * D E F ↑ /
* (+( - * A B C * D E F ↑ /
G (+( - * A B C * D E F ↑ / G
) (+ A B C * D E F ↑ / G * -
* (+ * A B C * D E F ↑ / G *-
H (+ * A B C * D E F ↑ / G * - H
) A B C * D E F ↑ / G * - H * +
12
Data Structures
Solved Problem 6.10: Transform the following arithmetic infix
expression Q into its equivalent postfix expression P:
Q: ((A + B)*D) ↑ (E-F)
Solution:
Push “(” onto stack and then add “)” to the end of Q. Thus, Q becomes
Q: ((A + B) * D) ↑ (E-F) )
Start scanning. The following table shows the status of STACK and of the
string P as each element of Q is scanned.
13
Data Structures
Solved Problem 6.10: Transform . . . postfix expression P:
Q: ((A + B)*D) ↑ (E-F))
14
Data Structures
Algorithm 6.6: Write an algorithm that transforms the infix expression
into its equivalent postfix expression.
POLISH (Q, P)
Suppose Q is an arithmetic expression written in infix notation. This
algorithm finds the equivalent postfix expression P.
15
Data Structures
Algorithm 6.6: Write an algorithm that transforms the infix expression
into its equivalent postfix expression.
5. If an operator is encountered, then
(a) Repeatedly pop from STACK and add to P each
operator (on the top of STACK) which has the same
precedence as or higher precedence than .
(b) Add to STACK.
[End of if structure]
6. If a right parenthesis is encountered, then
(a) Repeatedly pop from STACK and add to P each
operator (on the top of STACK) until a left parenthesis
is encountered.
(b) Remove the left parenthesis.
[End of if structure]
[End of Step 2 loop]
7. Exit.
16
Application of Stacks - Evaluating Postfix Expression
Solution:
- First we add a sentinel right parenthesis at the end of P:
P: 5, 6, 2, +, *, 12, 4, /, -, )
(1)scanning
- Start (2) (3)
from(4)
left to(5)right.
(6)The(7) (8) table
following (9) shows
(10) the
contents of STACK as each element of P is scanned. The final number in
STACK, 37, which is assigned to VALUE when the sentinel ")" is scanned, is
the value of P.
18
Data Structures
Example 6.6: Find the value of the following arithmetic expression P
written in postfix notation:
P: 5, 6, 2, +, *, 12, 4, /, -, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Solution:
19
Data Structures
Algorithm 6.5: Write an algorithm that finds the value of an arithmetic
expression written in postfix notation.
This algorithm finds the VALUE of an arithmetic expression P written in postfix
notation.
20
Data Structures
Recursion
21
Data Structures
Recursion
Example 6.9: Calculate 4! using the recursive definition.
Solution: This calculation requires the following nine steps:
1) 4! = 4. 3!
2) 3! = 3. 2!
3) 2! = 2 . 1!
4) 1! = 1 . 0!
5) 0! = 1
6) 1! = 1 . 1 = 1
7) 2! = 2 . 1 =2
8) 3! = 3. 2 = 6
9) 4!=4 . 6 = 24
22
Data Structures
Procedure 6.9A: Write a procedure that calculates N!
FACTORIAL(FACT, N)
This procedure calculates N! and returns the value in the variable FACT.
23
Data Structures
Procedure 6.9B: Write a recursive procedure that calculates N!
FACTORIAL(FACT, N)
This procedure calculates N! and returns the value in the variable FACT.
24
Data Structures
Fibonacci Sequence
The Fibonacci sequence (usually denoted by F0, F1, F2, . …….) is as
follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ....
That is, F0 = 0 and F1 = 1 and each succeeding term is the sum of the
two preceding terms.
25
Data Structures
Fibonacci Sequence
Procedure 6.10: Write a recursive procedure that finds the nth
Fibonacci number.
FIBONACCI(F1B, N)
This procedure calculates FN and returns the value in the first
parameter FIB.
1. If N = 0 or N = 1, then: Set FIB := N, and Return.
2. Call FIBONACCI(F1BA, N - 2).
3. Call FIBONACCI(FIBB, N - 1).
4. Set FIB := F1BA + FIBB.
5. Return.
26
Data Structures
Recursion vs. Iteration
Roughly speaking, recursion and iteration perform the same kinds of
tasks:
Solve a complicated task one piece at a time, and combine the
results.
Emphasis of iteration:
keep repeating until a task is “done”
Emphasis of recursion:
Solve a large problem by breaking it up into smaller and smaller
pieces until you can solve it; combine the results. Example: recursive
factorial function.
27
Data Structures
Recursion vs. Iteration
The function calls itself recursively on a smaller version of the input (n - 1). The
solution to the problem is then devised by combining the solutions obtained from
the simpler versions of the problem.
28
Data Structures
Queues
A queue is a linear list of elements in which deletions can take place only
at one end, called the front, and insertions can take place only at the other
end, called the rear.
Queues are also called first-in first-out (FIFO) lists, since the first
element in a queue will be the first element out of the queue. In other
words, the order in which elements enter a queue is the order in which
they leave.
Example:
A line of people waiting at a bus stop form a queue, where the first
person in line is the first person to board the bus.
A line of people waiting to be served by an ATM machine.
Collection of documents sent to a shared printer.
29
Data Structures
Representation of Queues
Queues may be represented in the computer by a linear array QUEUE and
two pointer variables: FRONT, containing the location of the front element
of the queue; and REAR, containing .the location of the rear element of the
queue.
FRONT = NULL indicates that the queue is empty.
The following Figure indicates the way elements will be deleted from the
queue and the way new elements will be added to the queue. Whenever an
element is deleted from the queue, the value of FRONT is increased by 1.
FRONT := FRONT + 1
Similarly, whenever an element is added to the queue, the value of REAR is
increased by 1.
REAR := REAR + 1
30
Data Structures
Representation of Queues
31
Data Structures
Procedure 6.13: Write a procedure that inserts an ITEM onto a queue.
QINSERT (QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue.
1. If FRONT = 1 and REAR = N, or if FRONT = REAR + 1, then
Write: OVERFLOW, and Return.
2. If FRONT = NULL, then
Set FRONT = 1 and REAR = 1
Else if REAR = N, then
Set REAR = 1
Else
Set REAR = REAR + 1
[End of If structure.]
3. Set QUEUE [REAR] = ITEM
4. Return.
32
Data Structures
Procedure 6.14: Write a procedure that deletes an element from a queue.
QDELETE(QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to the
variable ITEM.
1. If FRONT:= NULL, then: Write: UNDERFLOW, and Return.
2. Set ITEM:= QUEUE[FRONT].
3. If FRONT= REAR, then:
Set FRONT := NULL and REAR := NULL.
Else if FRONT = N, then:
Set FRONT := 1.
Else:
Set FRONT := FRONT + 1
[End of If structure.]
4. Return.
33
Data Structures
Solved Problem 6.22: Consider the following queue of characters, where
QUEUE is a circular array which is allocated six memory cells:
FRONT=2, REAR=4 QUEUE: -, A, C, D, -, -
Describe the queue as the following operations take place:
i) F is added to the queue. vi) Two letters are deleted.
ii) Two letters are deleted. vii) S is added to the queue.
iii) K, L, and M are added to the queue. viii) Two letters are deleted.
iv) Two letters are deleted. ix) One letter is deleted.
v) R is added to the queue. x) One letter is deleted.
Solution:
i) FRONT=2, REAR=5 QUEUE: -, A, C, D, F, -
ii) FRONT=4, REAR=5 QUEUE: -, -, -, D, F, -
iii) FRONT=4, REAR=2 QUEUE: L, M, -, D, F, K 34
Data Structures
Solved Problem 6.22: Consider the following queue of characters, where
QUEUE is a circular array which is allocated six memory cells:
FRONT=2, REAR=4 QUEUE: -, A, C, D, -, -
Describe the queue as the following operations take place:
i) F is added to the queue. vi) Two letters are deleted.
ii) Two letters are deleted. vii) S is added to the queue.
iii) K, L, and M are added to the queue. viii) Two letters are deleted.
iv) Two letters are deleted. ix) One letter is deleted.
v) R is added to the queue. x) One letter is deleted.
Solution:
iv) FRONT=6, REAR=2 QUEUE: L, M, -, -, -, K
v) FRONT=6, REAR=3 QUEUE: L, M, R, -, -, K
vi) FRONT=2, REAR=3 QUEUE: -, M, R, -, -, - 35
Data Structures
Solved Problem 6.22: Consider the following queue of characters, where
QUEUE is a circular array which is allocated six memory cells:
FRONT=2, REAR=4 QUEUE: -, A, C, D, -, -
Describe the queue as the following operations take place:
ii) Two letters are deleted. vii) S is added to the queue.
iii) K, L, and M are added to the queue. viii) Two letters are deleted.
iv) Two letters are deleted. ix) One letter is deleted.
v) R is added to the queue. x) One letter is deleted.
Solution:
vii) FRONT=2, REAR=4 QUEUE: -, M, R, S, -, -
viii) FRONT=4, REAR=4 QUEUE: -, -, -, S, -, -
ix) FRONT=0, REAR=0 QUEUE: -, -, -, -, -, -
x) Queue is empty. Underflow has occurred. 36
Data Structures
DEQUES
37
Data Structures
DEQUES
38
Data Structures
DEQUES
An output-restricted deque
An output-restricted deque is a deque which allows deletions at
only one end of the list but allows insertions at both ends of the list.
39
Data Structures
Solved Problem 6.24: Consider the following deque of characters, where
DEQUE is a circular array which is allocated six memory cells:
LEFT=2, RIGHT=4 DEQUE: -, A, C, D, -, -
Describe the deque as the following operations take place:
i) F is added to the right of the deque.
ii) Two letters on the right are deleted.
iii) K, L, and M are added to the left of the deque.
iv) One letter on the left is deleted.
v) R is added to the left of the deque.
vi) S is added to the right of the deque.
vii) T is added to the right of the deque.
40
Data Structures
Solved Problem 6.24: . . . LEFT=2, RIGHT=4 DEQUE: -, A, C, D, -, -
i) F is added to the right of the deque.
ii) Two letters on the right are deleted.
iii) K, L, and M are added to the left of the deque.
iv) One letter on the left is deleted.
v) R is added to the left of the deque.
Solution:
i) LEFT=2, RIGHT=5 DEQUE: -, A, C, D, F, -
ii) LEFT=2, RIGHT=3 DEQUE: -, A, C, -, -, -
iii) LEFT=5, RIGHT=3 DEQUE: K, A, C, -, M, L
iv) LEFT=6, RIGHT=3 DEQUE: K, A, C, -, -, L
v) LEFT=5, RIGHT=3 DEQUE: K, A, C, -, R, L 41
Data Structures
Solved Problem 6.24: . . . LEFT=5, RIGHT=3 DEQUE: K, A, C, -, R, L
Solution:
vi) LEFT=5, RIGHT=4 DEQUE: K, A, C, S, R, L
vii) Since LEFT = RIGHT + 1, the deque is full.
That is, OVERFLOW has occurred.
42
Data Structures
Priority Queues
43
Data Structures
One-Way List Representation of a Priority Queues
44
Data Structures
One-Way List Representation of a Priority Queues
Start
PRN
AAA 1 BBB 2 CCC 2
45
Data Structures
Sample Q.
Q: For the following expressions, trace the conversion of infix to postfix form in tabular form:
i. A + B * C / D - E + F / G / ( H + I )
Ans: Postfix expression is: A B C * D / + E – F G / H I + / +
ii. ( A + B ) * C + D / ( B + A * C ) + D
Ans: Postfix expression is: A B + C * D B A C * + / + D +
Q: Evaluate the following expression showing every status of stack in tabular form.
i. 5 4 6 + * 4 9 3 / + *
Ans: 350
ii. 7 5 2 + * 4 1 1 + / -
Ans: 47
46
Data Structures
Sample Q.
47