How do I apply the dct values to the formula? - algorithm

I am implementing an algorithm about face detection corresponding to a paper i have found. At the end of the paper it uses the dct values to take out false alarms by using some formulas. One of them is the following:
My question is: I have calculated dct values for MxN, now how do i apply them to the formula?
EDIT: So that is what you mean? (The 0 to 7 inner loops are a random part of the 100x100 dct1 array, which has only y dct, the cb,cr are not needed for the algorith)
for(i = 0; i <= M * N * (4 - 1); i++){
for(m = 0; m <= 7; m++){
for(n = 0; n <= 7; n++){
value += std::pow(dct1.at<float>(m,n),2);
}
}

Related

Given a matrix, cell is call good cell if row divides column

Given a n*m matrix, a cell is called good cell if row number (i) divides column number (j)
Example :
2*3 matrix => cells are {1,1}, {1,2}, {1,3}, {2,1}, {2,2}, {2,3} from which good cells can be defined as {1,1}, {1,2}, {1,3}, {2,2}
So the output is 4
I have figure out the logic for this, row 1 has all cells as good cells, row 2 has m/2 good cells, row 3 has m/3 good cells. (I am using just the integer dividend)
m/1 + m/2 + m/3 + ........ + m/n;
For which my code looks like =>
long count=1;
long answer = 0;
while(count<=n){
answer=answer + m/count;
count++;
}
But this code is getting timed out when I am submitting my solution. Need help to find better approach for this.
PS: Coding challenge is already over.
Try this one,
for(int i = 0 ; i < n; i++)
{
for(int j = 0; j < m; j += i) // j += i will save unwanted loops
{
// rest of your logic
}
}
As the value of i gets higher and higher nested loop will become more efficient
edit
The below code will be more efficient & correct than above. Now nested loop starts with the value of i instead of 0
for(int i = 0 ; i < n; i++)
{
for(int j = i; j < m; j += i) //j = i & j += i will save unwanted loops
{
// rest of your logic
}
}
as noted by n pronouns m this post incorrect since it only considers the case n==m
You may have a look at the Dirichlet divisor problem
One attempt is the formula of Benoit Cloitre:
(below octave implem)
function s = sumit(n)
s = 0;
for i = 1:n
s += floor(n/i);
end
end
n = 1e6
expect = sumit(n)
%A006218
u = floor(sqrt(n));
res = 2*sum(floor(n./(1:u))) - u^2
Where you avoid summing quite some terms.

What is maximum water colledted between two histograms?

I recently came across this problem:
You are given height of n histograms each of width 1. You have to choose any two histograms such that if it starts raining and all other histograms(except the two you have selected) are removed, then the water collected between the two histograms is maximised.
Input:
9
3 2 5 9 7 8 1 4 6
Output:
25
Between third and last histogram.
This is a variant of Trapping rain water problem.
I tried two solutions but both had worst case complexity of N^2. How can we optimise further.
Sol1: Brute force for every pair.
int maxWaterCollected(vector<int> hist, int n) {
int ans = 0;
for (int i= 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans = max(ans, min(hist[i], hist[j]) * (j - i - 1));
}
}
return ans;
}
Sol2: Keep a sequence of histograms in increasing order of height. For every histogram, find its best histogram in this sequence. now, if all histograms are in increasing order then this solution also becomes N^2.
int maxWaterCollected(vector<int> hist, int n) {
vector< pair<int, int> > increasingSeq(1, make_pair(hist[0], 0)); // initialised with 1st element.
int ans = 0;
for (int i = 1; i < n; i++) {
// compute best result from current increasing sequence
for (int j = 0; j < increasingSeq.size(); j++) {
ans = max(ans, min(hist[i], increasingSeq[j].first) * (i - increasingSeq[j].second - 1));
}
// add this histogram to sequence
if (hist[i] > increasingSeq.back().first) {
increasingSeq.push_back(make_pair(hist[i], i));
}
}
return ans;
}
Use 2 iterators, one from begin() and one from end() - 1.
until the 2 iterator are equal:
Compare current result with the max, and keep the max
Move the iterator with smaller value (begin -> end or end -> begin)
Complexity: O(n).
Jarod42 has the right idea, but it's unclear from his terse post why his algorithm, described below in Python, is correct:
def candidates(hist):
l = 0
r = len(hist) - 1
while l < r:
yield (r - l - 1) * min(hist[l], hist[r])
if hist[l] <= hist[r]:
l += 1
else:
r -= 1
def maxwater(hist):
return max(candidates(hist))
The proof of correctness is by induction: the optimal solution either (1) belongs to the candidates yielded so far or (2) chooses histograms inside [l, r]. The base case is simple, because all histograms are inside [0, len(hist) - 1].
Inductively, suppose that we're about to advance either l or r. These cases are symmetric, so let's assume that we're about to advance l. We know that hist[l] <= hist[r], so the value is (r - l - 1) * hist[l]. Given any other right endpoint r1 < r, the value is (r1 - l - 1) * min(hist[l], hist[r1]), which is less because r - l - 1 > r1 - l - 1 and hist[l] >= min(hist[l], hist[r1]). We can rule out all of these solutions as suboptimal, so it's safe to advance l.

Is there an approximation to get meanvalue and standard deviation in one loop

I have a collection of n floating point values: x[n]. When I want to calculate the meanvalue and standard deviation, I need to iterate with two loops over all values:
First loop to sum all values and calculate the meanvalue:
sum = 0
for(i=0; i<n; i++)
sum += x[i]
mean = sum/n
In a second loop I calculate the standard deviation:
sum = 0
for(i=0; i<n; i++)
sum += pow2(x[i] - mean)
sder = sqrt(sum/n)
I am aware that you cannot reduce this complexity if you want to the exact values for meanvalue and standard deviation. But is there a way to calculate them in less time if you just approximate? Favoured in one loop.
Have a look at this section of the wiki on standard deviation, in particular the last formula leads to the following algorithm:
sum = 0;
sumsqrd = 0;
for(i = 0; i < n; i++)
sum += x[i]
sumsqrd += x[i] * x[i]
mean = sum / n
stddev = sqrt(sumsqrd / n - mean * mean)
Here's a version which does the calculations in one pass, and is computationally more stable:
mean = 0.0
sum_sqrs = 0.0
n = 0
loop do
x = get_x()
break if x == nil
delta = x - mean
n += 1
mean += delta / n
sum_sqrs += delta * (x - mean)
end
sample_var = sum_sqrs / (n - 1)
This is based on the formulas found in the bottom half of the Rapid calculation methods section of the Wikipedia page for Standard deviation.

Sum of proper divisor upto N

I have to find sum of proper divisor upto N (N <= 20000000). I have pre-calculate this function with complexity O(N * log(N)) which takes upto 4 second. How can I optimize it or any alternate solution will be greatly accepted.
Example: N = 10 answer is 86, N = 1000 answer is 823080
int f(){
for(LL i = 1; i <= m; i++){
for(LL j = i; j <= m; j += i){
ans[j] += i;
}
}
for(LL i = 2; i <= m; i++) ans[i] += ans[i - 1];
}
I also tried using prime factorization and this formula, but it takes more time than above algorithm.
I can't edit my previous comment, only to say that the problem referenced by my comment is a little bit different, but with a minor adjustment it answers also this question.
Just multiply inside the loop by the divisor itself.

Dynamic Programming: Find the rectangle in a grid that has the largest sum

I came across the following Dynamic Programming problem.
You have a grid of integers (so including negative numbers). Find the rectangle that has the largest sum of numbers.
Any idea how to do it for a whole matrix?
I solved it for a single array, so I pretty much followed what longest increasing subsequnce does, but only for contiguous numbers.
def array_largest_block(sequence)
len = sequence.size
parents = [nil]*len
my_largest = sequence
largest = sequence.max
for index in (1...len)
if my_largest[index] < my_largest[index] + my_largest[index - 1]
my_largest[index] = my_largest[index] + my_largest[index - 1]
parents[index] = index - 1
largest = [largest, my_largest[index]].max
end
end
end_index_of_largest_block = my_largest.find_index(largest)
i = end_index_of_largest_block
res = []
res << sequence[i]
while !parents[i].nil?
i = parents[i]
res << sequence[i]
end
return {l_sum: largest, start: i, end: end_index_of_largest_block}
end
So My thinking is,
find the sum of each square in the matrix (just 1x1 squares)
save the max for a possible answer
Run the same thing starting from smallest possible rectangle and calculate all of them until you find the max. Which is the DB part.
Any ideas? Or if you guys don't knwo the exact solution, which DP type algorithm should i look at?
This can be done in O(N^3), where N is the size of the matrix.
You basically choose the left and right column of the rectangle and then scan through the rows in linear time(using precomputed sums).
int totalBestSum = -10000000;
for (int leftCol = 1; leftCol <= N; leftCol++)
for (int rightCol = leftCol; rightCol <= N; rightCol++)
{
int curSum = 0, curBestSum = -10000000;
for (int row = 1; row <= N; row++) {
int rowSum = sumBetween(leftCol, rightCol, row);
curSum += rowSum;
if (curSum > curBestSum) curBestSum = curSum;
if (curSum < 0) curSum = 0;
}
if (curBestSum > totalBestSum) totalBestSum = curBestSum;
}
sumBetween is a function returning the sum of the numbers on a particular row between two columns. It can be implemented in constant time, using precomputed sums.
int sumBetween(int leftCol, int rightCol, int row)
{
return sum[row][rightCol] - sum[row][leftCol - 1];
}
To compute the sum array:
for (int row = 1; row <= N; row++)
for (int col = 1; col <= N; col++)
sum[row][col] = sum[row][col - 1] + matrix[row][col];
Seems like a duplicate, but still, look here: Getting the submatrix with maximum sum?
It is possible to do in O(N^3).
And why on earth do you use the 'NP-complete' tags?:D

Resources