Applications of Stack Applications of Stack Parentheses Matching by Stack Parentheses Matching by Stack
Applications of Stack Applications of Stack Parentheses Matching by Stack Parentheses Matching by Stack
Applications of Stack Applications of Stack Parentheses Matching by Stack Parentheses Matching by Stack
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
Output pairs (u,v) such that the left parenthesis at
position u is matched with the right parenthesis at v.
(2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
Parentheses
P
th
M
Matching
t hi
Towers Of Hanoi/Brahma
System Stack
R In
Rat
I AM
Maze
(a+b))*((c+d)
(0,4)
(0
4)
right parenthesis at 5 has no matching left parenthesis
(8,12)
left parenthesis at 7 has no matching right parenthesis
Parentheses Matching
scan expression from left to right
when a left parenthesis is encountered, add its
position to the stack
when a right parenthesis is encountered,
encountered remove
matching position from stack
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
2
1
0
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
6
2
1
0
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
13
1
0
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
15
1
0
(2,6) (1,13)
(2,6)
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
21
1
0
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
27
1
0
Example
(((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
(((a+b)*c+d e)/(f+g) (h+j)*(k l))/(m n)
1
0
and so on
Recursion
Recursion is a fundamental programming
technique
h i
that
h can provide
id an elegant
l
solution
l i for
f
certain kinds of problems.
A recursive definition
f
is one which uses the word
or concept being defined in the definition itself.
=
=
=
1
1 * 0!
N * (N-1)!
In class Exercise:
Fibonacci Numbers
1, 1, 2, 3, 5, 8, 13, 21,
Using a recursive formula to define Fibonacci
Numbers
Homework
iterative program
input:n
n
Outputfib(0) ~fib(n)
Towers Of Hanoi/Brahma
4
3
2
1
A
B
C
64 gold disks to be moved from tower A to tower C
eachh tower operates as a stackk
cannot place big disk on top of a smaller one
Towers Of Hanoi/Brahma
3
2
1
3
3-disk
disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
2
1
33-disk
disk Towers Of Hanoi/Brahma
3
3-disk
disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
3
2
33-disk
disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
3
2
3
3-disk
disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
33-disk
disk Towers Of Hanoi/Brahma
3-disk
3 disk Towers Of Hanoi/Brahma
7 disk moves
3
3-disk
disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
2
1
Recursive Solution
3
2
1
Recursive Solution
Recursive Solution
move top n
n-1
1 disks from B to C using A
Recursive Solution
In Class Exercise
Show that
moves(n) = 2*moves(n-1) + 1 = 2n-1 when n
>0
moves(n) = 0 when n = 0
moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
System Stack
Towers Of Hanoi/Brahma
moves(64) = 1.8 * 1019 (approximately)
Performing 109 moves/second,
moves/second a computer would take
about 570 years to complete.
At 1 disk move/min
move/min, the monks will take about 3.4
3 4 * 1013
years.
fp
local variables
f
fp
d
c=2
2
previous frame pointer
return address.
Back to OS
main
b=1
a=1
previous frame pointer
return address.
d
c=2
2
previous frame pointer
return address.
Back to OS
main
Rat In A Maze
d=2
c=2
2
previous frame pointer
return address.
Back to OS
Rat In A Maze
Rat In A Maze
Rat In A Maze
Rat In A Maze
0,0
Move down.
down
Rat In A Maze
Move left
left.
Rat In A Maze
Move down.
down
Rat In A Maze
Rat In A Maze
Rat In A Maze
Rat In A Maze
Move right
right.
Backtrack.
Move downward
downward.
Rat In A Maze
Move right
right.
Rat In A Maze
Rat In A Maze
Rat In A Maze
Homework
N
[i-1][j-1]
[i-1][j]
NE
NW
[i][j-1]
[i-1][j+1]
[i][j+1]
[i][j]
[i+1][j-1]
[i+1][j]
[i+1][j+1]
SW
SE