CS1020 Week 11: 2 nd April 2015. Contents Sit-in Lab #3 Take-home Lab #5 Week 112.

26
CS1020 Week 11: 2 nd April 2015

Transcript of CS1020 Week 11: 2 nd April 2015. Contents Sit-in Lab #3 Take-home Lab #5 Week 112.

Page 1: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

CS1020Week 11: 2nd April 2015

Page 2: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 112

ContentsSit-in Lab #3Take-home Lab #5

Page 3: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 113

Sit-in Lab #3

Set A – Process SchedulingSet B – Email Service

Page 4: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 114

Objective Abstract Data Types

Use of Linked List, Stack, and Queue Getting familiar with Java APIs for Stack and Queue Handle insertion and removal from stack and queue

data structures with additional constraints

Problem Solving Understand the basic requirements of a problem Figuring out thde most suitable data structure given the

nature of the problem. Using the basic operations of a data structure (e.g.

push/pop) appropriately to suit the needs of the problem at hand

Sit-in Lab 3

Page 5: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 115

Set A – Problem Problem:

Simulate a process scheduling system

Properties: Process with the lowest priority gets killed first Among processes with equal priority, process that started

the earliest gets killed first Part 2: If the system is already executing maximum

number of processes and a new process arrives, if the new process has priority greater than at least one process in the system it starts executing by killing a process.

Operations: Execute Kill

Sit-in Lab 3 Set A

Page 6: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 116

execute Chrome 5

execute Word 3

execute Netbeans 4

execute Paint 1

execute Excel 5

execute Flash 2

kill 2

Set A – Design (1/2)

Process Queue

Chrome5

Word3

Netbeans4

Paint1

Excel5

Flash2

First In First Out: With Priority

Sit-in Lab 3 Set A

Page 7: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 117

Set A – Design (2/2)

Process

- processName : String- priority : int

+ toString () - String

ProcessScheduling

- processList : Queue<Process>

- maxProcesses : int

+ simulate (Scanner)+ execute (Process)+ kill (int)+ preempt (Process)+ toString() - String

Sit-in Lab 3 Set A

Page 8: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 118

Set A – Operations execute : add a process to the process queue

• Remove all processes in the process queue, having lesser or equal priority to the new process to a temporary queue one by one

• Add the new process to the temporary queue• Remove rest of the processes from process queue and add to temporary

queue one by one• Remove processes one by one from temporary queue and add back to

process queue

kill : kill specified number of processes• Remove given number of processes from queue

preempt: if number process queue has maximum number of processes, add a new process by removing a process from the queue only if priority of arriving process is greater than priority of killed process• Check if priority of the first process in queue is less than arriving process:

• If yes kill 1, execute <newProcess>

• If no do nothing

Sit-in Lab 3 Set A

Page 9: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 119

Set B – Problem Email Service: Simulate an Email Service application

Properties Read always the newest email. If a new email arrives and there are older emails of the same

thread in inbox, all emails of that thread are brought to the top of inbox

Part 2: Mark an email thread as important, which will bring all emails belonging to the thread to top of inbox. Note: Relative order among emails belonging to a thread should be maintained

Operations: addNewMail readMails markAsImportant

Sit-in Lab 3 Set B

Page 10: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1110

Set B – Design (1/2)

Sender: CSub: OSContent: Scheduling

Sender: KSub: CSContent: Old

Sender: KSub: CSContent: New

Sender: BSub: AIContent: Prolog

receive C OS Scheduling

receive K CS Old

receive K CS New

receive B AI Prolog

mark B AI

mark K CS

read 2

No change. Since <B,AI> already on top

Inbox Last In First Out

Sit-in Lab 3 Set B

Page 11: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1111

Set B – Design (2/2)

Email

- sender : String- subject : String- Content : String

+ toString () - String

EmailService

- inbox : Stack<Email>

+ simulate (Scanner)+ addNewMail (Email)+ readMails (int)+ markAsImportant (Email)+ toString() - String

Sit-in Lab 3 Set B

Page 12: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1112

Set B – Operations addNewMail: add an email to the inbox

• Pop all mails from the inbox and push in a temporary stack until a mail with the same thread (as of the new mail) is found

• Pop all mails of the same thread from inbox and push to another temporary stack• Pop all remaining mails and push to the first temporary stack• Push all mails from the first temporary stack back to inbox• Push all mails from the second temporary stack to the inbox• Push the new mail to inbox

readMails: read and delete specified number of emails from inbox• Remove given number of emails from stack

markAsImportant: bring an email thread on top inbox• Pop all mails from the inbox and push in a temporary stack until a mail with the

specified thread is found• Pop all mails of the same thread from inbox and push to another temporary stack• Pop all remaining mails and push to the first temporary stack• Push all mails from the first temporary stack back to inbox• Push all mails from the second temporary stack to the inbox

Sit-in Lab 3 Set B

Page 13: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1113

Take-home Lab #5

Exercise 1 – Packing Luggage

Page 14: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1114

Ex #1: Problem

Space = 80

Space = 50Value = 1000

Space = 20Value = 10

Space = 40Value = 800

Space = 35Value = 300

Space = 20Value = 100

Take-home Lab 5 Ex #1: Packing Luggage

The Knapsack problem Classic CS problem Many variants (go

google)

Page 15: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1115

Ex #1: Decision Making For each item we can either choose it or not to

choose it if remaining space is greater than the space this item occupies.

Optimization: We prefer greater total value, or same total value and greater free space.

Take-home Lab 5 Ex #1: Packing Luggage

Page 16: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1116

Ex #1: Recursion Base case

remaining.isEmpty()We have finished making decision for all items

Inductive case: remainingSpace < item.sizeWe do not choose this item and recursively call select() to consider the next item.

Inductive case: remainingSpace >= item.sizeConsider between choosing the item or not; whichever that will give a preferred result.

Take-home Lab 5 Ex #1: Packing Luggage

Page 17: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1117

Ex #1: Food for thought How efficient is this algorithm? What is the

complexity? O(2n), which is exponential – very very slow! Reason: each item has 2 possibilities: to be chosen

or not to be chosen, hence the total number of possibilities = 2 × 2 × … × 2 = 2n

Is there a more efficient way to solve this problem? Yes, but not covered in CS1020

n times

Take-home Lab 5 Ex #1: Packing Luggage

Page 18: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1118

Take-home Lab #5

Exercise 2 – Obstacle Course

Page 19: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

19

Ex #2: Problem

Week 11

We traverse an obstacle course by hopping from one Block to the next Each Block has a height and a hopping range

We can hop from Block X to Block Y if either… X and Y are adjacent, ie. index of X is i and

index of Y is i + 1 X and Y are not adjacent, and …

Height of Y ≤ hopping range of X Height of each Block between X and Y ≤ hopping range

of X

The task is to compute the smallest number of hops to get from the first Block to the last Block

Take-home Lab 5 Ex #2: Obstacle Course

Page 20: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1120

Ex #2: Implementation Base case = We are on the last Block of the

obstacle course Question: What should our method return?

Recursive case = We are not on the last Block of the obstacle course1. Hop to the adjacent Block2. Hop to some Block beyond the adjacent Block

(may be multiple possibilities)

Take-home Lab 5 Ex #2: Obstacle Course

Page 21: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1121

Ex #2: Cases (1/5)

#1

4

3

2

1

0#2 #3 #4 #5

PP

O

O

Recursive case, with 2 possibilities

Take-home Lab 5 Ex #2: Obstacle Course

Page 22: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1122

Ex #2: Cases (2/5)

#1

4

3

2

1

0#2 #3 #4 #5

PP

P

Recursive case, with 3 possibilities

Take-home Lab 5 Ex #2: Obstacle Course

Page 23: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1123

Ex #2: Cases (3/5)

#1

4

3

2

1

0#2 #3 #4 #5

P

O

Recursive case, with 1 possibilities

Take-home Lab 5 Ex #2: Obstacle Course

Page 24: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1124

Ex #2: Cases (4/5)

#1

4

3

2

1

0#2 #3 #4 #5

P

Recursive case, with 1 possibilities

Take-home Lab 5 Ex #2: Obstacle Course

Page 25: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1125

Ex #2: Cases (5/5)

#1

4

3

2

1

0#2 #3 #4 #5

Base case

Take-home Lab 5 Ex #2: Obstacle Course

Page 26: CS1020 Week 11: 2 nd April 2015. Contents  Sit-in Lab #3  Take-home Lab #5 Week 112.

Week 1126

END OF FILE