Week11_Lab.pptx

advertisement
CS1020
Week 11: 2nd April 2015
Contents

Sit-in Lab #3

Take-home Lab #5
2
Week 11
Sit-in Lab #3
Set A – Process Scheduling
Set B – Email Service
3
Week 11
Sit-in Lab 3
Objective

Abstract Data Types




Problem Solving



4
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
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
Week 11
Sit-in Lab 3
Set A
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:



5
Execute
Kill
Week 11
Sit-in Lab 3
Set A
Set A – Design (1/2)

execute Chrome 5

execute Word 3

execute Netbeans 4

execute Paint 1

execute Excel 5
Excel
5
Netbeans
4
Process Queue

execute Flash 2

kill 2
6
First In First Out: With Priority
Week 11
Chrome
Flash
Paint
Word
2135
Sit-in Lab 3
Set A
Set A – Design (2/2)
7
Process
ProcessScheduling
- processName : String
- priority : int
- processList : Queue<Process>
- maxProcesses : int
+ toString () - String
+ simulate (Scanner)
+ execute (Process)
+ kill (int)
+ preempt (Process)
+ toString() - String
Week 11
Sit-in Lab 3
Set A
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:
•
8
•
If yes  kill 1, execute <newProcess>
•
If no  do nothing
Week 11
Sit-in Lab 3
Set B
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
9
Week 11
Set B – Design (1/2)
 receive C OS Scheduling
 receive K CS Old
Sit-in Lab 3
Set B
No change.
Since <B,AI>
already on top
Sender: K
Sub: AI
CS
B
C
OS
Content: Old
Prolog
New
Scheduling
 receive K CS New
 receive B AI Prolog
 mark B AI
 mark K CS
 read 2
Inbox
10
Last In First Out
Week 11
Sit-in Lab 3
Set B
Set B – Design (2/2)
Email
EmailService
- sender : String
- subject : String
- Content : String
- inbox : Stack<Email>
+ toString () - String
11
+ simulate (Scanner)
+ addNewMail (Email)
+ readMails (int)
+ markAsImportant (Email)
+ toString() - String
Week 11
Sit-in Lab 3
Set B
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
•
•
•
•
•
12
Week 11
Take-home Lab #5
Exercise 1 – Packing Luggage
13
Week 11
Take-home Lab 5 Ex #1: Packing Luggage
Ex #1: Problem
 The Knapsack problem
 Classic CS problem
 Many variants (go google)
Space = 20
Value = 100
Space = 50
Value = 1000
Space = 40
Value = 800
Space = 20
Value = 10
14
Space = 80
Space = 35
Value = 300
Week 11
Take-home Lab 5 Ex #1: Packing Luggage
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.
15
Week 11
Take-home Lab 5 Ex #1: Packing Luggage
Ex #1: Recursion

Base case


Inductive case:


remaining.isEmpty()
We have finished making decision for all items
remainingSpace < item.size
We do not choose this item and recursively call select() to
consider the next item.
Inductive case:

16
remainingSpace >= item.size
Consider between choosing the item or not; whichever that will
give a preferred result.
Week 11
Take-home Lab 5 Ex #1: Packing Luggage
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
n times
Is there a more efficient way to solve this problem?

17
Yes, but not covered in CS1020
Week 11
Take-home Lab #5
Exercise 2 – Obstacle Course
18
Week 11
Take-home Lab 5 Ex #2: Obstacle Course
Ex #2: Problem

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
19
Week 11
Take-home Lab 5 Ex #2: Obstacle Course
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 course
1. Hop to the adjacent Block
2. Hop to some Block beyond the adjacent Block (may
be multiple possibilities)
20
Week 11
Take-home Lab 5 Ex #2: Obstacle Course
Ex #2: Cases (1/5)
Recursive case, with 2 possibilities
O
4
P
3
P
2
O
1
0
21
#1
#2
#3
#4
Week 11
#5
Take-home Lab 5 Ex #2: Obstacle Course
Ex #2: Cases (2/5)
Recursive case, with 3 possibilities
P
4
P
3
2
P
1
0
22
#1
#2
#3
#4
Week 11
#5
Take-home Lab 5 Ex #2: Obstacle Course
Ex #2: Cases (3/5)
Recursive case, with 1 possibilities
P
4
3
2
O
1
0
23
#1
#2
#3
#4
Week 11
#5
Take-home Lab 5 Ex #2: Obstacle Course
Ex #2: Cases (4/5)
Recursive case, with 1 possibilities
4
3
2
P
1
0
24
#1
#2
#3
#4
Week 11
#5
Take-home Lab 5 Ex #2: Obstacle Course
Ex #2: Cases (5/5)
Base case
4
3
2
1
0
25
#1
#2
#3
#4
Week 11
#5
END OF FILE
26
Week 11
Download