CS1020E Lab 4 (Stack and Queue)

26
CS1020E Lab 4 (Stack and Queue) Problem 1 : Swinging Monkey Problem 2 : Alice

description

Problem 1 Swinging Monkey

Transcript of CS1020E Lab 4 (Stack and Queue)

Page 1: CS1020E Lab 4 (Stack and Queue)

CS1020E Lab 4(Stack and Queue)

Problem 1 : Swinging MonkeyProblem 2 : Alice

Page 2: CS1020E Lab 4 (Stack and Queue)

SWINGING MONKEYProblem 1

Page 3: CS1020E Lab 4 (Stack and Queue)

Swinging Monkey• Problem Description:• Monkey can swing from one tree to another directly

as long as there is no tree in between that is taller than or have the same height as either one of the two trees

• Given the sequence of tree heights, determine the number of pair of trees that the Monkey can swing

• Input:• First line is [INTEGER] N: number of trees• Second line is N x [INTEGER]: sequence of height

• Output:• Single line of [INTEGER]: #trees Monkey can swing

Page 4: CS1020E Lab 4 (Stack and Queue)

Swinging Monkey• Illustration:• 5 trees: 19m – 17m – 20m – 20m – 20m

Output:5

Page 5: CS1020E Lab 4 (Stack and Queue)

Swinging Monkey• Discussions:• What is the simplest algorithm to solve this?

• Given two trees: check if the Monkey can jump• i.e. Check if there are taller trees in between• Loop for all tree pairs

• Algorithm:• fun canSwing (i, j) { for(k=i+1 to j-1) do{ if(height[k] ≥ min(height[i], height[j]){ return false } } return true}

Page 6: CS1020E Lab 4 (Stack and Queue)

Swinging Monkey• Observation:• 5 trees: 19m – 17m – 20m – 20m – 17m

Observe:from the tree

before the Blue tree, the

Monkey cannot jump to the

tree after the Blue tree

Page 7: CS1020E Lab 4 (Stack and Queue)

Swinging Monkey• Idea:• After we process tree i, we can forget all the trees

before i that are shorter than i.• What will be the property of the sequence of trees

that are not forgotten?• Decreasing Order from the tree with smallest index• Why?• Suppose it is not in decreasing sequence• After we process Blue, we can forget

Yellow• Yellow would have been removed

from the sequence

Page 8: CS1020E Lab 4 (Stack and Queue)

Swinging Monkey• Property of the Sequence:

1. Decreasing Order of Height2. At the start of the processing of the next tree,

the Monkey can jump from all tree in the sequence to the next tree

3. If the next tree is higher than some elements,these elements in the sequence cannot jump beyond the next tree

• Discussions:• What Data Structure do we need to implement?• Stack: why?• Because we need to remember the previous

elements in order

Page 9: CS1020E Lab 4 (Stack and Queue)

ALICEProblem 2

Page 10: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Problem Description:• Given a list of pancakes which can be sweet on

either side or both, given the operations:1. Flip the top X pancakes and add syrup to top2. Add new pancake without adding syrup to top

• Count the number of sweet pancakes• Pancake is NOT sweet if none of the sides are sweet

Otherwise, the pancake is sweet• Limitation:• Only use Stack/Queue to solve the problem• STACK: Last In First Out• QUEUE: First In First Out

Page 11: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Input:• First line is [INTEGER] N and [INTEGER] Q

• N = initial #pancakes; Q = #queries/operations• Next Q-lines are [STRING] operations

• If operations is FLIP: contains another [INTEGER] index• Else only the operations is present

• Output:• Only output when COUNT operations is

encountered• Output: [INTEGER] <number of sweet pancakes>

followed by newline character

Page 12: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

Page 13: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

ABCDE

Page 14: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

ABCDE

Page 15: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

CDE

F

Page 16: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

CDE

F

Page 17: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

E

CD

F

Page 18: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

E

CD

F

Page 19: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Simulation:• Input:

• 5 6• FLIP 3 • ADD• COUNT • FLIP 3• FLIP 4• COUNT

• Output:• 2• 5

AB

ECD

F

Page 20: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Discussions:• How do you Store a Pile of Pancake?

• Stack• Why?

• How do you Flip a Pile of Pancakes? Use of auxiliary Queue to reverse the order of a Stack

• How do you Count the Pile of Pancakes in Stack?• You cannot look into arbitrary position (violate Stack

structure)• You can only pop from the top, push to the top• You cannot use any other data structure besides

Stack/Queue Use of auxiliary Stack to preserve the order of a Stack

Page 21: CS1020E Lab 4 (Stack and Queue)

Stack/Queue Primer• Stack:• Last In First Out• Can be simulated using Linked List

• A restricted Linked List• Insert only to the beginning (i.e. push = addFirst)• Remove only from the beginning (i.e. pop =

removeFirst)• Queue:• First in First Out• Can be simulated using Linked List

• A restricted Linked List• Insert only to the end (i.e. enqueue = addLast)• Remove only from the beginning (i.e. dequeue =

removeFirst)

Page 22: CS1020E Lab 4 (Stack and Queue)

Stack/Queue Primer• Stack A Stack B Stack A• Remove all elements from Stack A Insert into B• Remove all elements from Stack B Insert into A• int size = A.size();for (int i=0; i<size; i++) B.addFirst(A.removeFirst());for (int i=0; i<size; i++) A.addFirst(B.removeFirst());

1 2 3 4 5

Page 23: CS1020E Lab 4 (Stack and Queue)

Stack/Queue Primer• Stack A Queue B Stack A• Remove all elements from Stack A Insert into B• Remove all elements from Queue B Insert into A• int size = A.size();for (int i=0; i<size; i++) B.addLast(A.removeFirst());for (int i=0; i<size; i++) A.addFirst(B.removeFirst());

1 2 3 4 5

Page 24: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Algorithms:• Flip the Pancake Pseudo-code:

• function Flip (index) { flip using auxiliary Queue; FOR each element moved back to Stack DO { IF needs sweeten THEN sweeten; } add sweetener to top stack;}

Page 25: CS1020E Lab 4 (Stack and Queue)

Alice the Baker• Algorithms:• Add to the top of the Pancake Pseudo-code:

• function Add() { push into Stack; }• Count the number of Sweet Pancake Pseudo-code:

• function Count() { push into auxiliary Stack; FOR each element moved back to Stack DO { IF sweet THEN increment counter; }}

Page 26: CS1020E Lab 4 (Stack and Queue)

THE ENDAny Questions?