6 December 2010

advertisement
6 December 2010

Will be due tonight @ 11:59 p.m.
Take a look at my “Review Notes” if you still
have no idea how to approach the problem
Stars in cells with no dependencies

Runtime issue


◦ We will not impose runtime constraints, but you
have to use backtracking
◦ Be aware that we will do less bug-fixing



On Monday, 13 December @ 4:00 p.m. – 6:50
p.m. (NOT 4:30 p.m.)
You are allowed 3 sheets of letter-sized
paper with notes
Review Sessions:
1. Tuesday, 7 December @ 1:00 p.m. – 3:00 p.m. in
HEC 101 with Remo
2. Wednesday, 8 December @ 1:00 p.m. – 3:00 p.m.
in HEC 101 with Chris


Network Flow
Proposed Solution:
Books
b1
w2
wn
w1
x1
w1
w1
s
Boxes
b2
.
.
.
bn
c1
x2
w1
.
.
.
xk
c2
ck
t

Counter Example:
x1
10
s
10
b1
8
10
x2
6
t
◦ Maximum Flow will be 10
◦ BUT: book is “split up” between boxes x1 and x2


Edit Distance
Remember:
Y
b
a
repl
X
c
del
ins
n
b

n
min(a  1, c  1, b  1)
, if X  Y
, otherwise

Backtracking
◦ You are given a set of letters, in random order, e.g.
“OFCEFI”
◦ You have to determine whether or not there exists a
way to reorder those letters to form a valid word.
◦ Given: A function “startswith(a)”, that
determines if there are any words that start with the
string “a”


Backtracking function jumbledLetters(a, b)
takes in a set of letters, a, already ordered, and
set of letters, b, that needs to be added
Base Cases:
◦ If startswith(a) == FALSE, return FALSE
◦ If b is empty string, return startswith(a)



Recursively call jumbledLetters() with next set of
letters
If any of recursive calls return TRUE, return TRUE
If all return FALSE, return FALSE

Example:
◦ Call to jumbledLetters(“OF”, “CEFI”)
(“OF”, “CEFI”)
(“OFC”, “EFI”)
(“OFE”, “CFI”)
FALSE
FALSE
(“OFF”, “CEI”)
(“OFFC”, “EI”)
(“OFFE”, “CI”)
.
.
.
(“OFI”, “CEF”)
(“OFFI”, “CI”)
FALSE


Single Room Scheduling (Lecture 11)
Greedy Solution:
1) Sort the requests by finish time.
2) Go through the requests in order of finish time,
scheduling them in the room if the room is
unoccupied at its start time.
Show
Time
Show
Time
Cops
6:30 – 7:30
Cops
6:30 – 7:30
CSI
7:30 - 8:30
CSI
7:30 - 8:30
30 Rock
7:00 - 9:00
Survivor
8:45 - 9:45
Office
8:00 - 9:00
Law & Order
9:50 - 10:50
Idol
6:00 - 9:15
Law & Order
9:50 - 10:50
Survivor
8:45 - 9:45
24
7:30 - 10:30
Law & Order
9:50 - 10:50
SportsCenter
9:15 - 11:00

MakeHeap (Lecture 4)
1) Place all the unsorted elements in a complete
binary tree.
2) Going through the nodes in backwards order, and
skipping the leaf nodes, run Percolate Down on
each of these nodes.


If one of the children is less than the node, swap the
lowest.
Continue until node has only larger (or equal)
children or until leaf node

Initial Heap:
19
17
3
22
4
87
1
26
14
13
67
35
12
6

Final Heap:
1
4
3
17
19
13
22
26
12
87
67
6
14
35

S     2i  1 2i 
n
i 1

Solution:
S  3  21  5  22  7  23 
2S 
3  2 2  5  23 
 S  3  21  2  22  2  23 
 2(20  21  22 
 (2n  3)  2 n 1  (2n  1)  2 n  (2 n  1)  2 n 1
 2  2 n 1
 2n )  (2n  1)  2 n 1
 2(2n 1  1)  (2n  1)  2n 1
S  (2n  1)  2n 1  2(2 n 1  1)
 (2n  1)  2n 1  2
 (2n  1)  2 n 1  (2n  1)  2 n
 2  2n
 (2 n  1)  2 n 1

Usually a good idea to break down initial
equation as much as possible before
proceeding
S     2i  1 2i 
n
i 1
   2i  2    2i
n
n
i
i 1
2
i 1
n
 i2
i
i 1
use S-Trick with x  2

n
2
i
i 1
finite geometric sum

Code Analysis
a) What problem does this code solve?
Matrix Chain Multiplication
b) Symbolically, what does the variable “m”
represent?
At which matrix you want to split your
multiplication a the top level.
c) Explain conceptually what is stored in the
variables “one”, “two”, and “three”.
• “one” stores number of multiplications to do the
left-side product
• “two” stores number of multiplications to do the
right-side product
• “three” stores number of multiplications to take
the two products from two recursive calls and
multiply them
d) Is the method “function” efficient? Justify.
No. The exact same recursive call gets made
multiple times.




Index
0
1
2
3
4
5
6
7
8
9
Value
2
3
6
3
8
1
7
3
5
7
Index
0
1
2
3
4
5
6
7
8
9
Value
2
3
6
3
8
1
7
3
5
7
a)
b)
Which row (0 – 9) is this in the path array?
3
Determine the vertices traversed in the shortest path from
vertex 3 to vertex 0.
37620
c)
From the information given, can we determine which vertex
is farthest away from vertex 3? Why or why not?
No. Because of insufficient weight info.
3
10
2
-5
0

CHEESE!
Download