stacks linear list. one end is called top. other end is called bottom. additions to and removals...

46
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

Post on 22-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Stacks

• Linear list.

• One end is called top.

• Other end is called bottom.

• Additions to and removals from the top end only.

Page 2: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Stack Of Cups

• Add a cup to the stack.

bottom

top

C

A

B

D

E

F

• Remove a cup from new stack.

• A stack is a LIFO list.

bottom

top

C

A

B

D

E

Page 3: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Parentheses Matching

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

• (a+b))*((c+d)– (0,4)– right parenthesis at 5 has no matching left parenthesis– (8,12)– left parenthesis at 7 has no matching right parenthesis

Page 4: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

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, remove matching position from stack

Page 5: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1 2

Page 6: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1

(2,6) (1,13)15

Page 7: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1

(2,6) (1,13) (15,19)21

Page 8: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1

(2,6) (1,13) (15,19) (21,25)27

Page 9: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1

(2,6) (1,13) (15,19) (21,25)(27,31) (0,32)

• and so on

Page 10: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

A B C

1234

• 64 gold disks to be moved from tower A to tower C• each tower operates as a stack• cannot place big disk on top of a smaller one

Page 11: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

123

Page 12: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

12

3

Page 13: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

1 2 3

Page 14: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

1 23

Page 15: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

123

Page 16: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

123

Page 17: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

12

3

Page 18: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• 3-disk Towers Of Hanoi/Brahma

A B C

123

• 7 disk moves

Page 19: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Recursive Solution

A B C

1

• n > 0 gold disks to be moved from A to C using B• move top n-1 disks from A to B using C

Page 20: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Recursive Solution

A B C

1

• move top disk from A to C

Page 21: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Recursive Solution

A B C

1

• move top n-1 disks from B to C using A

Page 22: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Recursive Solution

A B C

1

• moves(n) = 0 when n = 0• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0

Page 23: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Towers Of Hanoi/Brahma

• moves(64) = 1.8 * 1019 (approximately)• Performing 109 moves/second, a computer would take about

570 years to complete.• At 1 disk move/min, the monks will take about 3.4 * 1013 years.

Page 24: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Chess Story

• 1 grain of rice on the first square, 2 for next, 4 for next, 8 for next, and so on.

• Surface area needed exceeds surface area of earth.

Page 25: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Chess Story

• 1 penny for the first square, 2 for next, 4 for next, 8 for next, and so on.

• $3.6 * 1017 (federal budget ~ $2 * 1012) .

Page 26: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Switch Box Routing1 2 3 4 5 6 7 8 9 10

30 29 28 27 26 25 24 23 22 21

11

12

13

14

15

16

17

18

19

20

40

39

38

37

36

35

34

33

32

31

Routing region

Page 27: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

17

Routing A 2-pin Net1 2 3 4 5 6 7 8 9 10

30 29 28 27 26 25 24 23 22 21

11

12

13

14

15

16

18

19

20

40

39

38

37

36

35

34

33

32

31

4

17

Routing for pins 5 through16 is confined to upper right region.

Routing for pins 1-3 and 18-40 is confined to lower left region.

Page 28: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

17

Routing A 2-pin Net1 2 3 4 5 6 7 8 9 10

30 29 28 27 26 25 24 23 22 21

11

12

13

14

15

16

18

19

20

40

39

38

37

36

35

34

33

32

31

4

17

(u,v), u<v is a 2-pin net.

u is start pin.

v is end pin.

Examine pins in clock-wise order beginn-ing with pin 1.

Page 29: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

17

Routing A 2-pin Net1 2 3 4 5 6 7 8 9 10

30 29 28 27 26 25 24 23 22 21

11

12

13

14

15

16

18

19

20

40

39

38

37

36

35

34

33

32

31

4

17

Start pin => push onto stack.

End pin => start pin must be at top of stack.

Page 30: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Method Invocation And Returnpublic void a()

{ …; b(); …}

public void b()

{ …; c(); …}

public void c()

{ …; d(); …}

public void d()

{ …; e(); …}

public void e()

{ …; c(); …}return address in a()return address in b()return address in c()return address in d()return address in e()return address in c()return address in d()

Page 31: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Try-Throw-Catch

• When you enter a try block, push the address of this block on a stack.

• When an exception is thrown, pop the try block that is at the top of the stack (if the stack is empty, terminate).

• If the popped try block has no matching catch block, go back to the preceding step.

• If the popped try block has a matching catch block, execute the matching catch block.

Page 32: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

Page 33: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move order is: right, down, left, up

• Block positions to avoid revisit.

Page 34: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move order is: right, down, left, up• Block positions to avoid revisit.

Page 35: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move backward until we reach a square from which a forward move is possible.

Page 36: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move down.

Page 37: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move left.

Page 38: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move down.

Page 39: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move backward until we reach a square from which a forward move is possible.

Page 40: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move backward until we reach a square from which a forward move is possible.

• Move downward.

Page 41: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move right.• Backtrack.

Page 42: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move downward.

Page 43: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move right.

Page 44: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move one down and then right.

Page 45: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move one up and then right.

Page 46: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only

Rat In A Maze

• Move down to exit and eat cheese.• Path from maze entry to current position operates as a stack.