I really have no idea how to do this using dynamic programming:
Problem:
I need to find the 2 largest non overlapping squares of a table
For example:
5 6
R F F R R F
F F F F F F
R R F F F F
F F F F F F
F F F F F F
The numbers 5 and 6 are the number of rows and columns respectively, and “R” means reserved
and “F” means free. In this case the largest square is
F F F F
F F F F
F F F F
F F F F
and the second largest (non-overlapping with the previous one) is
F F
F F
So far I have put the values into a 2D array, but do not know what to do after.
Tried to reference 0-1 knapsack and LCS, but really I have no clue on what values I should put into my table.
Well, your first task when designing a dynamic programming algorithm should be to find a recursive solution to the problem. After that is done, converting it to a dynamic programming algorithm is almost trivial ;).
Some tips that could/could not help:
A possible base case is obvious: any two 1 cell squares are always non-overlapping.
Bear in mind that the largest of the two squares can not cover the entire table (because then you wont have a second largest), so it cannot be of rows:columns size.
You should have an "score" to each solution you evaluate, to see what's the best one.This score is obviously size1 + size2, with the condition that size1 should be the maximum possible.
Good luck!!
This is a variation of the Longest Common Substring Problem not LCS (Longest common subsequence). Picture two strings you are comparing as being sides of a rectangle with their characters as the squares. An "F" in a square would mean a match between two characters in the strings, and thus the largest square is the longest common substring.
Related
There are n positive numbers (A1 , ... An) on a circle, how do we divide this circle into subsegments with sum greater or equal to m so that number of subsegments are maximum in O(n) , or O(nlogn)
eg :
n = 6 m = 6
3 1 2 3 6 3
ANS = 3
since we can divide the array into three subsegments{[2,4],[5,5],[6,1]}
If there is at least one number greater or equal to m in the array, just cut the array into smallest possible pieces starting from one of these numbers. Otherwise (and if sum of numbers is at least 2*m) use a pointer-chasing algorithm.
This algorithm uses 2 additional arrays: L for chain lengths (initially zero) and S for starting indices (initially equal to own indices: 0, 1, 2, ...). And 2 array indices: F and B (initially zero).
Increment F while sum between F and B is less than m. Then increment B while sum between F and B is greater than m (but stop when is is still greater or equal to m).
Update arrays: L[F] = 1 + L[B], S[F] = S[B].
Repeat steps 1,2 while F<n. While incrementing F on step 1, copy most recently updated values to L[F] and S[F].
Reset F to zero.
Increment F while sum of elements before F and after B is less than m. Then increment B while sum before F and after B is greater than m (but stop when is is still greater or equal to m).
If F <= S[B] use L[B] + 1 to update maximum number of subsegments.
Repeat steps 5,6 while B<n.
I have a file with repeating elements. I would like to assign records to an array until the file repeats, at which point I want to create a new array to assign the records to. I would like to do this an arbitrary amount of times.
for example.
$ cat repeat.txt
a
b
c
d
e
f
g
a
b
c
d
e
f
g
a
b
c
d
e
f
g
I want the output to be something like this
0 a a a
1 b b b
2 c c c
3 d d d
4 e e e
5 f f f
6 g g g
right now I am doing this with this hideous code.
awk 'BEGIN{n=0;z=0}
$1~"a" {n=0;z++}
z==1{a[n]=$0}
z==2{b[n]=$0}
z==3{c[n]=$0}
z==4{d[n]=$0}
z==5{e[n]=$0}
z==6{f[n]=$0}
{n++}
END{for (i in a)
print i,a[i],b[i],c[i],d[i],e[i],f[i],g[i],h[i],k[i],j[i]}'
repeat.txt
I would like the assignment of new arrays to be automatic.
I attempted this by the following
echo "abcdefghijklmopqrstuvwxyz" > alphabet.txt
awk 'BEGIN{N=0}
NR==FNR{FS=""}
NR==FNR{for (zz=0;zz<=NF;zz++) a[zz]=$zz; next}
NR!=FNR{FS="\t"}
NR!=FNR{if ($0~a) N++; (a[N])[N]=$0}
END{for (I in (a[N])) print I,(a[N])[I]}' alphabet.txt repeat.txt
but this didn't work because you can't do multidimensional arrays like this in gawk. I can't think of another way to do this.
I have a hard time to understand why the concatenation of two languages over an alphabet, which is in NP, doesn't imply that each of the languages for themselves are in NP. I talked with my Prof about the problem today, but I can't wrap my head around it. Can you help me out?
Here's a counterexample to the claim that if A and B are languages and AB ∈ NP, then A ∈ NP and B ∈ NP. First, consider all subsets of the set 1*. There are countably infinitely many strings in 1*, so there are uncountably many subsets of 1*. Since there are only countably many decidable languages, at least one of these languages is undecidable; let's have that language be A. For B, choose 1*.
My claim is that AB is some language of the form { 1n | n ≥ k } for some natural number k. To see this, let k be the length of the shortest string in A. Then any string in AB has the form 1k+m1r, where 1k+m ∈ A and 1r ∈ B. Such a string necessarily belongs to { 1n | n ≥ k }. Similarly, if we take any string from { 1n | n ≥ k }, we can see that it has the form 1k+m = 1k1m, where 1k ∈ A and 1m ∈ B. Therefore, AB = { 1n | n ≥ k }.
The language AB is regular because it's given by the regular expression 1k1*, but A is not in NP because it's undecidable and all languages in NP are decidable.
Hope this helps!
I have a 2D array of a crossword puzzle:
0 1 2 3 4
0 t h i s q
1 m i l e u
2 a b y w i
3 n b y s c
4 l o o t k
And given a list of words to find: this, quick, loot, mile, by, man
Objective:
Find words in the puzzle in any direction (horizontal, vertical, diagonal)
Output:
Is to list all the points where the word is located.
for example, this: (0,0) , (0, 1), (0, 2), (0,3)
I'm having trouble getting started on this.
Something like this. Might require some fixes but in general:
find_word(x,y,word,index,inc_x,inc_y):
if x,y is not on board return false
if letter[x,y] != word[index] return false
if it is last letter return true
call find_word(x+inc_x,y+inc_y,word,index+1,inc_x,inc_y)
foreach x:
foreach y
foreach word
for x_inc in -1,0,1
for y_inc in -1,0,1
if x_inc + y_inc == 0 continue
find_word(x,y,word,0,x_inc,y_inc)
If you want to make if faster than make a tree of words and you can remove cycle on words.
For example imagine you have: abc, aab, aac.
a
a
b
c
b
Now imagine you have found a letter a.
Call find_word and get letter d for ex. There's no d in tree of a, so no word can be found.
Call find_word and get letter a for ex. Now narrow search of words to subtree of a.
Thus you can go from w*h*num_words*max_length only to w*h*max_length.
The FIND-S algorithm is probably one of the most simple machine learning algorithms. However, I can't find many examples out there.. Just the standard 'sunny, rainy, play-ball' examples that's always used in machine learning. Please could someone help me with this application (its a past exam question in machine learning).
Hypotheses are of the form a <= x <= b, c <= y <= d where x and y are points in an x,y plane and c and d are any integer. Basically, these hypotheses define rectangles in the x,y space.
These are the training examples where - is a negative example and + is a positive example and the pairs are the x,y co-ordinates:
+ 4, 4
+ 5, 3
+ 6, 5
- 1, 3
- 2, 6
- 5, 1
- 5, 8
- 9, 4
All I want to do is apply FIND-S to this example! It must be simple! Either some tips or a solution would be awesome.
Thank you.
Find-S seeks the most restrictive (ie most 'specific') hypothesis that fits all the positive examples (negatives are ignored).
In your case, there's an obvious graphical interpretation: "find the smallest rectangle that contains all the '+' coordinates"...
... which would be a=4, b=6, c=3, d=5.
The algorithm for doing it would be something like this:
Define a hypothesis rectangle h[a,b,c,d], and initialise it to [-,-,-,-]
for each + example e {
if e is not within h {
enlarge h to be just big enough to hold e (and all previous e's)
} else { do nothing: h already contains e }
}
If we step through this with your training set, we get:
0. h = [-,-,-,-] // initial value
1. h = [4,4,4,4] // (4,4) is not in h: change h so it just contains (4,4)
2. h = [4,5,3,4] // (5,3) is not in h, so enlarge h to fit (4,4) and (5,3)
3. h = [4,6,3,5] // (6,5) is not in h, so enlarge again
4. // no more positive examples left, so we're done.