0% found this document useful (0 votes)
22 views47 pages

Pmscs 623p Lecture 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views47 pages

Pmscs 623p Lecture 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Data Structure and

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.

Two basic operations associated with stacks:


“Push” is the term used to insert an element into a stack.
“Pop” is the term used to delete an element from a 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.

PUSH (STACK, TOP, MAXSTK, ITEM)


This procedure pushes an ITEM onto a stack.

1. If TOP = MAXSTK, then: Print: OVERFLOW and


Return.
2. Set TOP = TOP + 1.
3. Set STACK[TOP] = ITEM.
4. Return.

4
Data Structures
Procedure 6.2: Write a procedure that deletes the top element of a stack.

POP (STACK, TOP, ITEM)


This procedure deletes the top element of STACK and
assigns it to the variable ITEM.

1. If TOP = 0, then: Print: UNDERFLOW, and Return.


2. Set ITEM = STACK[TOP].
3. Set TOP = TOP – 1.
4. Return.

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/

This notation is also known as Reverse Polish notation.


One never needs parentheses to determine the order of the operations
in any arithmetic expression written in reverse Polish notation.
The computer usually evaluates an arithmetic expression written in infix
notation in two steps. First, it converts the expression to postfix notation,
and then it evaluates the postfix expression. In each step, the stack is
the main tool that is used to accomplish the given task.

8
Data Structures (Operator Precedence)

 Two operators of same priority can’t stay together.


 Higher priority operator will not stay in the stack when lower priority
operator will be inserted.
 (…..) => pop all the operators from stack and place them in the
postfix.

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:

Symbol Scanned STACK Expression, P


A ( A
+ (+ A
( (+( A
B (+( A B
* (+( * A B
C (+( * A B C
- (+( - A B C *
( (+( - ( A B C *

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))

Symbol Scanned STACK Expression, P


( ((
( (((
A ((( A
+ (((+ A
B (((+ A B
) (( A B+
* ((* A B+
D ((* A B+D
) ( A B+D*
↑ (↑ A B+D*
( (↑( A B+D*
E (↑( A B+D*E
- (↑(- A B+D*E
F (↑(- A B+D*EF
) (↑ A B+D*EF-
) A B+D*EF-↑

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.

1. Push “(” onto STACK and add “)” to the end of Q.


2. Scan Q from left to right and repeat steps 3 to 6 for each element
of Q until the STACK is empty.
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto
STACK.

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

• Example: Consider the postfix expression, 2 10 + 9 6 - /,


which is (2 + 10) / (9 - 6) in infix, the result of which is 12 / 3 =
4.
• The following is a trace of the postfix evaluation algorithm for
the postfix expression:
Data Structures
Example 6.6: Find the value of the following arithmetic expression P
written in postfix notation:
P: 5, 6, 2, +, *, 12, 4, /, -

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:

Symbol Scanned STACK


(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
(4) + 5, 8
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
(8) / 40, 3
(9) - 37
(10) )

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.

1. Add a right parenthesis “)” at the end of P.


2. Scan P from left to right and repeat Steps 3 and 4 for each element of P until the
sentinel ")" is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator  is encountered, then:
a) Remove the two top elements of STACK, where A is the top
element and B is the next-to-top element.
b) Evaluate B A.
 the result of (b) back on STACK.
c) Place
[End of If structure]
[End of Step 2 loop.]
5. Set VALUE equal to the top element on STACK.
6. Exit.

20
Data Structures
Recursion

A procedure is called a recursive procedure if it contains a Call statement


to itself.
A recursive procedure must have the following two properties:
There must be certain criteria, called base criteria, for which the
procedure does not call itself.
Each time the procedure does call itself, it must be closer to the base
criteria.

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.

1. If N = 0, then: Set FACT := 1, and Return.


2. Set FACT := 1.
3. Repeat for K = 1 to N.
Set FACT := K*FACT.
[End of loop.]
4. Return.

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.

1. If N = 0, then: Set FACT := 1, and Return.


2. Call FACTORIAL(FACT, N — 1).
3. Set FACT := N*FACT.
4. Return.

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.

Definition: (Fibonacci Sequence)


a) If n = 0 or n = 1, then Fn = n.
b) If n > 1, then Fn = Fn-2 + Fn-1

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.

Use of recursion in an algorithm has both advantages and disadvantages.


The main advantage is usually simplicity.
The main disadvantage is often that the algorithm may require large amounts
of memory if the depth of the recursion is very large.
Recursive isn’t always better. This takes O(2n) steps. Unusable for large n.
Iterative approach is “linear”; it takes O(n) steps.

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

A deque is a linear list in which elements can be added or removed at


either end but not in the middle. The term deque is a contraction of the
name double-ended queue.
Deque is maintained in memory by a circular array DEQUE with pointers
LEFT and RIGHT, which point to the two ends of the deque.
Elements extend from the left end to the right end in the array.
Figure pictures two deques, each with 4 elements maintained in an array
with N = 8 memory locations.
LEFT = NULL indicates that a deque is empty.

37
Data Structures
DEQUES

38
Data Structures
DEQUES

There are two variations of a deque:


An input-restricted deque
An input-restricted deque is a deque which allows insertions at
only one end of the list but allows deletions at both ends of the list.

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

vi) S is added to the right of the deque.


vii) T is added to the right of the deque.

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

A priority queue is a collection of elements such that each


element has been assigned a priority and such that the order in which the
elements are deleted and processed comes from the following rules:

An element of higher priority is processed before any element of


lower priority.
Two elements with the same priority are processed according to the
order in which they were added to the queue.

43
Data Structures
One-Way List Representation of a Priority Queues

One way to maintain a priority queue in memory is by means of a


one-way list as follows:

a) Each node contains three items of information:


An information field INFO, a priority number PRN and a link
number
LINK.
b) A node X proceeds a node Y in the list:
When X has higher priority than Y OR
When both have the same priority but X was added to the list
before Y.

44
Data Structures
One-Way List Representation of a Priority Queues

Start

PRN
AAA 1 BBB 2 CCC 2

DDD 4 EEE 5 FFF 6 ×

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.

Q: Consider the following arithmetic expression P, written in postfix notation. Translate it in


infix notation and evaluate. P: 12, 7, 3, -, /, 2, 1, 5, +, *, +

Ans: In infix notation is: ( 12 / ( 7 – 3 ) ) + ( ( 5 + 1 ) * 2 )


Value = 15

47

You might also like