Skip to content

Commit fdf86ff

Browse files
authored
Create DP4_GuitarFingering_Tetris_SuperMarioBros.java
1 parent 40026dd commit fdf86ff

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
In the recurrence, we usually take the min of a bunch of options or the max of a bunch of options.
3+
And those options correspond to a guessed feature. We don't know whether the go left or go right, so we try them both.
4+
That's guessing. But there's another way to guess. So two kinds of guessing.
5+
6+
In the previous problems we GUESSED which subproblems to use to solve the bigger problem.
7+
8+
In KNAPSACK problem, we added MORE SUBPROBLEMS to GUESS/REMEMBER more features of the solution.
9+
1. If we look at the suffix - we don't know the capacity we have used up - we don't remember details about the prefix
10+
2. That is why we add MORE SUBPROBLEMS - to REMEMBER how much capacity has been used in the prefix.
11+
3. We generated MORE SUBPROBLEMS by - MULTIPLYING every sub-problem by S different choices, which is how many units of knapsack still remain.
12+
4. Simply put - we are REMEMBERING MORE about the prefix(past).
13+
14+
15+
//PIANO OR GUITAR FINGERING - Single Hand, Single Note
16+
Given sequence of n notes, find fingering for each note(single).
17+
1. Fingers are labelled as - 1,2,....,F
18+
2. Difficulty Measure - d(p,f,q,g)
19+
where, f and g denote finger labels
20+
p and q denote notes
21+
finger f plays the p note and finger g plays the q note
22+
23+
24+
DP Approach 1(WRONG APPROACH)-
25+
1. Subproblems - How to play notes[i:] onwards(suffix)
26+
2. Guess - Which finger to use for note[i]
27+
3. Recurrence -
28+
DP(i) = min{ DP(i+1) + d(i,f,i+1.??????) for each f in 1...F}
29+
d() is the difficulty measure, can be thought of as the transition cost
30+
The above approach is WRONG beause the transition function requires us to know the finger used for the transition.
31+
We need more information about what we are going to do next.
32+
33+
We need to add MORE SUBPROBLEMS.
34+
35+
36+
DP approach 2(Correct) -
37+
1. Subproblems - How to play notes[i:], when we use finger f for notes[i]
38+
2. Guess - finger g for notes[i+1]
39+
3. Recurrence -
40+
DP(i,f) = min{ DP(i+1,g) + d(i,f,i+1,g) for g in 1...F }
41+
4. Topological Order -
42+
Bottom up approach
43+
for i in reversed(range(n)): //go from right to left
44+
for f in 1...F:
45+
5. Original Problem -
46+
Here the subproblems we are solving require us to provide to first finger label, but we don't know that.
47+
We don't know which finger to start with, we consider them all and take the min.
48+
min(DP(0,f) for f in 1...F)
49+
Time = (num of subproblems)*(time/subproblem) = theta(nF)*theta(F) = theta(n . F^2)
50+
when F in small ~ linear time
51+
52+
DAG - complete bipartite graph
53+
notes
54+
0 ..........i............. n-1
55+
1 x1 y1 z1 ....................
56+
2 x2 y2 z2 ....................
57+
f . .............................
58+
. .............................
59+
F .............................
60+
61+
62+
Each cell in the matrix is a node in the BIPARTITE GRAPH ==>
63+
each x in column 0 is connected to each y in column 1
64+
x1 --> (y1,y2,.....yF)
65+
.
66+
.
67+
xF --> (y1,y2,.....yF)
68+
69+
each y in column 1 is connected to each z in column 2
70+
y1 --> (z1,z2,.....zF)
71+
.
72+
.
73+
yF --> (z1,z2,.....zF)
74+
and so on...
75+
76+
We need to find the shortest path in this DAG, the current formulation is not a single source shortest path problem.
77+
We are looking at all sources to find the shortest path.
78+
79+
This can be converted to single source shortest path by using a dummy node and connecting it with all column 0 nodes with weight 0. Now, the source is this dummy node.
80+
*/

0 commit comments

Comments
 (0)