Run parallel code for a matrix dependent on previous values - c++

I have a matrix that has to be calculated from the previous values inside the matrix in parallel. It will be nice if anyone of you can give me a Hint of how it can be done. Suppose i have a matrix like
| 4 5 6 7 8|
| 5 5 5 5 5|
| 6 6 6 6 6|
| 9 9 9 9 9|
The value here will be computed as the position (1,1) will be computed from (0,0), (0,1) and (1,0) three neighboring elements. It will be the minimum of its values and so on. Every element is dependent on its previous three neighbors for the computation of its value. Can anyone give me a hint how it can be done in parallelism. thank you.

For that kind of dependency you can compute the elements of anti-diagonals in parallel. You have to initialize the topmost row and leftmost column, then proceed, for each anti-diagonal step by step:
0 0 0 0 ..
0 1 2 3 ..
0 2 3 ..
0 3 ...
..
i denoted in the schematic the pass number
0 = init, 1 = first step, 2=second step..
For example, you can compute each cell in step 2 in parallel, then compute each cell in step 3 in parallel and so on like a wave-front sweeping through the matrix (it's a known technique)
Unfortunately since there is data dependency between cells, you need to wait the step to finish before proceed to the next one.
Also since the number of elements are variable, some processors will be underutilized by this method.

Related

Enumerating and indexing all possible trees of n vertices [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 5 years ago.
Improve this question
We have been trying to find a way to enumerate and index all possible trees with n labelled vertices. By Cayley's theorem there shall be nn−2 number of trees from n labelled vertices. Is there a way, in C/C++, to index all the possible trees so that when a user inputs an integer/number a unique tree will generated in real time?
A quick glance at the Wikipedia article on Cayley's formula (the nn−2 formula you mention) pointed me to Prüfer sequences, which is a sequence of length n−2 consisting of (possibly repeated) node labels. It's obvious that there are nn−2 such sequences, and each sequence can be represented as an n−2 digit base n number. It's less obvious that every Prüfer sequence corresponds to a unique tree with n labeled nodes, but that fact is sufficient to demonstrate Cayley's formula.
The Wikipedia article on Prüfer sequences explains how to turn a sequence into its corresponding tree; which is equivalent to turning an integer into a tree.
I haven't tried any of this, but it looks convincing.
I'm not well-versed in C or C++, but I think I can provide the theory such that enumerating every tree shouldn't be too hard. Comment if I need to clarify anything.
Think Binary.
Take an adjacency matrix. To describe whether one vertex is connected to another, we use 1 or 0. So to find all the graphs using adjacency matrices, we would fill up a matrix with all the combos of 1 and 0. The only constraint is that for trees, a node can't be its own parent, and can't have multiple parents. Example with three vertices:
0 1 1 0 1 1 0 0 1
1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 etc.
What we can do is to lay the rows out side-by-side such that a binary sequence describes every matrix. Example:
0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 etc.
So given nine bits, we can describe all graphs with three vertices. This translates to one tree for every number 1-2^9, minus the numbers which are rotations of each other.
To turn a number into a tree, you just convert the number to binary, and turn the binary into a matrix. To fix the self-connections, for every "1" that is on or past the diagonal, move it further by one. So then:
1 0 0 1 0 1
0 1 0 -> 0 0 0
0 0 1 0 1 0

Given 2d array of 0s and 1s, find all the squares in it using backtracking

in this 2d array 1 represents a point and 0 represents blank area.
for example this array:
1 0 0 0 1
0 0 1 0 0
0 0 0 0 0
0 0 0 0 1
my answer should be 2, because there are 2 squares (or rectangles) in this array like this
all the points should be used, and you can't make another square | rectangle if all its points are already used (like we can't make another square from the point in the middle to the point in the top right) because they are both already used in other squares, you can use any point multiple times just if at least one corner is not used point.
I could solve it as an implementation problem, but I am not understanding how backtracking is related to this problem.
thanks in advance.
Backtracking, lets take a look at another possible answer to your problem, you listed:
{0,0} to (2,1}
{0,0} to {4,0}
As one solution another solution is (With respect to the point can be used multiple times as long as one point is unused):
{4,0} to {2,1} (first time 4,0 and 2,1 is used)
{0,0} to {2,1} (first time 0,0 is used)
{0,0} to {4,4} (first time 4,4 is used)
Which is 3 moves, with backtracking it is designed to show you alternative results using recursion. In this equation if you start the starting location for calculating the squares at different areas of the array you can achieve different results.
for an example iterating starts from 0,0, and going right across each row trying to find all possible rectangles starting with [0,0] will give the solution you provided, iteratings starting from 4,0 and going left across each row trying to find all possible solutions will give my result.

The Concept of Bilinear Interpolation by Accessing 2-d array like 1-d array does

In 2-d array, there are pixels of bmp files. and its size is width(3*65536) * height(3*65536) of which I scaled.
It's like this.
1 2 3 4
5 6 7 8
9 10 11 12
Between 1 and 2, There are 2 holes as I enlarged the original 2-d array. ( multiply 3 )
I use 1-d array-like access method like this.
array[y* width + x]
index
0 1 2 3 4 5 6 7 8 9...
1 2 3 4 5 6 7 8 9 10 11 12
(this array is actually 2-d array and is scaled by multiplying 3)
now I can patch the hole like this solution.
In double for loop, in the condition (j%3==1)
Image[i*width+j] = Image[i*width+(j-1)]*(1-1/3) + Image[i*width+(j+2)]*(1-2/3)
In another condition ( j%3==2 )
Image[i*width+j] = Image[i*width+(j-2)]*(1-2/3) + Image[i*width+(j+1)]*(1-1/3)
This is the way I know I could patch the holes which is so called "Bilinear Interpolation".
I want to be sure about what I know before implementing this logic into my code. Thanks for reading.
Bi linear interpolation requires either 2 linear interpolation passes (horizontal and vertical) per interpolated pixel (well, some of them only require 1), or requires up to 4 source pixels per interpolated pixel.
Between 1 and 2 there are two holes. Between 1 and 5 there are 2 holes. Between 1 and 6 there are 4 holes. Your code, as written, could only patch holes between 1 and 2, not the other holes correctly.
In addition your division is integer division, and does not do what you want.
Generally you are far better off writing a r=interpolate_between(a,b,x,y) function, that interpolates between a and b at step x out of y. Then test and fix. Now scale your image horizontally using it, and check visually you got it right (especially the edges!)
Now try using it to scale vertically only.
Now do both horizontal, then vertical.
Next, write the bilinear version, which you can test again using the linear version three times (will be within rounding error). Then try to bilinear scale the image, checking visually.
Compare with the two-linear scale. It should differ only by rounding error.
At each of these stages you'll have a single "new" operation that can go wrong, with the previous code already validated.
Writing everything at once will lead to complex bug-ridden code.

Error control coding for a practical application

I’m doing a project where a device is built to measure the girth of a rubber tree in a rubber plantation.
I need to give an identity to each tree to store the measurements of each tree.
The ID of each tree contains 33bits (in binary). For error detection and correction I’m hoping to encode this 33bit word in to a code word (Using a error control coding technique) and generate a 2D matrix (Color matrix-With red and cyan squares representing 1’s and 0’s). The 2D matrix will represent the coded word. This matrix will be pasted on the trunk of the tree. And a camera (the device) will be used to take the image of the 2D matrix and the code will be decoded then the ID of the tree will be taken.
I’m looking for the best scheme to implement this. I thought of cyclic codes for coding, but since the data word is 33 bits cyclic codes seems to be bit complicated.
Can someone please suggest the best way (at least a good way) to implement this???
Additional info: The image is taken in a forest environment (low light condition), Color matrix is to be used considering the environment.(The bark of the tree is dark so black and white matrix would be not appropriate)
One way to do it is to use 2D-parity check codes. The resulted codeword is a matrix, and it has single error correction (SEC) capability.
Since your information part (tree ID) has 33 bits, you may need to add a few dummy bits to make the codeword a 2D rectangle (say, a 6x6 information part). If a tree's ID is 1010_1010_1010_1010_1010_1010_1010_1010_1, then by adding 3 more 0 we have it as:
1 0 1 0 1 0 | 1
1 0 1 0 1 0 | 1
1 0 1 0 1 0 | 1
1 0 1 0 1 0 | 1
1 0 1 0 1 0 | 1
1 0 1 0 0 0 | 0
—————————————
0 0 0 0 1 0 1
Then you get a (n, k, d) = (49, 36, 3) code, which correct 1 bit errors.

Edge Detection Techniques

Does anyone know what the differences between the Prewitt, Sobel and Laplacian operators in edge detection algorithms?
Are some better than others?
Are different operators used in different situations?
The laplace operator is a 2nd order derivative operator, the other two are 1st order derivative operators, so they're used in different situations. Sobel/Prewitt measure the slope while the Laplacian measures the change of the slope.
Examples:
If you have a signal with a constant slope (a gradient):
Gradient signal: 1 2 3 4 5 6 7 8 9
a 1st derivative filter (Sobel/Prewitt) will measure the slope, so the filter response is
Sobel result: 2 2 2 2 2 2 2
The result of a lapace filter is 0 for this signal, because the slope is constant.
Example 2: If you have an edge signal:
Edge: 0 0 0 0 1 1 1 1
The sobel filter result has one peak; the sign of the peak depends on the direction of the edge:
Sobel result: 0 0 0 1 1 0 0 0
The laplace filter produces two peaks; the location of the edge corresponds with the zero crossing of the laplace filter result:
Laplace result: 0 0 0 1 -1 0 0 0
So if you want to know the direction of and edge, you'd use a 1st order derivative filter. Also, a Laplace filter is more sensitive to noise than Sobel or Prewitt.
Sobel and Prewitt filters on the other hand are quite similar and are used for the same purposes. Important differences between 1st order derivative filters are
Sensitivity to noise
Anisotropy: Ideally, the filter results for X/Y should be proportional to sin α and cos α, where α is the angle of the gradient, and the sum of the two squares should be the same for every angle.
Behavior at corners
These properties can be measured with artificial test images (like the famous Jähne test patterns, found in "Image Processing" by Bern Jähne). Unfortunately, I didn't find anything about the Prewitt operator in that book, so you'd have to do your own experiments.
In the end, there's always a trade-off between these properties, and which of them is more important depends on the application.