Counting the number of group in a matrix [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have found an interesting problem.
A n*m matrix is given, with a such form:
11111111
11111001
11111001
10111111
10111111
11100111
11111111
The goal of the problem is to find the number of '0' blocks. On the previous example, there were 3 '0' blocks.
I don't understand how to solve this problem. I don't ask for any code, I would like to get some hints about how to solve this problem.

You can use depth-first search to find connected components in a graph where vertices are cells with 0 and an edge between two vertices is present if two cell are adjacent.

Given your definition of block:
For every row you check if there are two (or more) contiguous zeros if that is the case you increase the 0's block count by 1 for each one of these occurrences.
You repeat the same procedure for the columns of the matrix.
I am not sure from your description of the problem how you should count bigger blocks like:
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
Is this a single block?

Related

Using FFT to find all possible sum [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given array A[] and B[], to find how frequently each possible sums A[i] + B[j] appears we can use FFT
For example
A = [1,2,3], B = [2,4].
The sum 3 can be obtained in 1 way, sum 4 : 1 way, sum 5 : 2 ways, sum 6 : 1 way, sum 7 : 1 way
The way we can do this is to construct two polynomials, P and Q with their power corresponding to the element of the array. And apply the regular FFT.
So is there an efficient way of backtracking the numbers that forms the above. To elaborate, we know 3 can be formed in 1 way. But how do we know which two numbers form it?
One way to do it would be the classic two sum algorithm, i.e given an array and a target sum. Find the pairs that create the sum. Which is O(n)
Given we can have N different targets, the resulting algorithm is O(n^2). But I want to keep it under O(nlogn).

What is the requirement for bubble sort to complete in 1 pass? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am working on a problem where you're given n distinct numbers, and you want to find the number of permutations such that it takes bubble sort at most 1 pass to complete.
e.g., if n=3, then the following permutations would only require 1 pass
1 2 3
1 3 2
3 1 2
2 1 3
But
3 2 1
2 3 1
would require more than 1 pass. Apparently the answer is 2^{n - 1}, but I am not sure how to prove this for the general n case.
My question is, what are the general constraints for a sequence to allow for it to be sorted with 1 pass of bubble sort?
It is difficult for me to come up with a generic formula to generate the permutations for larger n.

Intersection of trinary matrices [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Consider 3 matrices of binary variables b0,b1,b2,b3. All these matrices have same number of column but can be different number of rows. Each element of the matrix can have three values 1,0 or 2 where 2 represent don't care. I have to find binary strings that presents in all three matrices. For example consider the following 3 matrices:
matrix1:
1 0 2 2
2 2 0 0
1 2 1 1
matrix2:
2 2 0 2
1 0 1 2
matrix3:
2 2 1 2
1 2 2 1
2 2 2 1
So, for this example string b0=1,b1=0,b2=1,b3=1 is present in all matrices. Because, in matrix1, b0=1,b1=2,b2=1,b3=1 is same as 1011. In matrix2, b0=1,b1=0,b2=1,b3=2 is same as 1011 and in matrix3, b0=2,b1=2,b3=2,b3=1 is same as 1011.
How to find all binary strings that exists in all 3 matrices?
The idea is to "expand" each row to its set of possibilities, so for example 1022 gets expanded to:
1000
1001
1010
1011
Then, it's convenient to convert each string to an integer (a single byte integer since the "strings" are 4 bit long) and place in a sorted array, or even a set.
Next step is to sort groups by length, from the smallest to the largest, then iterate the smallest group values and see that it exists in all other groups, this is very fast because of the preparation work in the "parsing" step.
Every value that passes for all groups is a match.
I suppose that simplest and reasonably efficient algorithm will be to brute-force check all possible combinations. Start with 0000 then 0001, then 0010 etc. With each of them, iterate each matrix and compare values. On first match, go to next matrix, on non-match, immediately reject.
You will have to iterate each matrix maximum 16 times, which is still O(N) from size of matrix.
If you want to optimize actual comparison, you can precompute lookup strings for each matrix. Create reverse bitmasks for 0-allowed and 1-allowed and AND them with bitmasks of has-0 and has-1 of query string. If any of two results is non-zero (you can just add or OR them and check result), string won't be matching.
In any case, it should be very fast with any kind of comparison implementation, as you will be doing only 16*(1000+1000+1000) rather than (1000*1000*1000) operations you probably were considering.

Counting Like Circular Linked List [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to count down with in a limit but in circular manner.
Let us say The limit is 12 and the current iteration is 10 then I would like to the result as 10 - 2 = 8
similar way if the current iteration is 0 the I should have result as follows 0 - 2 = 11 not -2.
The main think I would like to have this as an algorithm / formula.
Thanks.
Use modolus operator and Zn group:
(i < 0 ? n + i : i) % n
Where n is the number - 12 in your example, and i is the iteration number.
(assuming -n <= i, otherwise you might want to subtract k*n - i for some natural k to make sure the result is positive. If you do the above step iteratively, this should not be an issue.)
As a side note, in pure mathematical concept -i == n-i in the Zn group, but most programming languages I am aware of does not do this calculation, and after modolus calculation the sign of the left operand remains the same.
For this we first check the sign, and make sure it is positive.

logical problem for finding the probability [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
hi to all
I want to know how to find the solution for this problem. consider in a Jar there are 10 papers numbered 1 to 10. we have to take 2 papers from that. what is the probability that 2 numbers are consecutive numbers.
It's a probability to choose one of 9 consecutive pairs from C(10, 2) possible pairs. Thus its 9 / (10*9 / 2) = 1 /5
The probability of getting 2-8 is 8/10. Once we have a given number in that range, the probability of getting one of the two adjacent numbers is 2/9. 8/10 * 2/9 = 16/90.
The probability of getting either a 1 or a 10 is 2/10. If we have one of those two endpoint numbers, the probability of getting the one adjacent number is 1/9. 2/10 * 1/9 = 2/90.
Adding these together, we have a total probability of 18/90 or 1/5.
Your first choice has ten possible numbers. 8 of those 10 will have two consecutive numbers, while the other two only have 1. Your second choice will be out of nine numbers. So
(8/10 * 2/9) + (2/10 * 1/9) = 9/45 = 1/5

Resources