Enumerating and indexing all possible trees of n vertices [closed] - c++

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

Related

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.

How does a projection Matrix work?

I have to write a paper for my A-Levels about 3D-Programming. But I got a serious problem understanding the perspective projection Matrix and I need to fully explain the Matrix in detail. I've searched a lot of websites and youtube videos on this topic but very little even try to answer the question why the Matrix has these values at that place. Based on this http://www.songho.ca/opengl/gl_projectionmatrix.html I was able to find out how the w-row works, but I don't understand the other three.
I decided to use the "simpler" version for symmetric viewports only (right-handed Coord.):
I am very thankful for every attempt to explain the first three rows to me!
The core reason for the matrix is to map the 3D coordinates to a 2D plane and have more distant objects be smaller.
For just this a much simpler matrix suffices (assuming your camera is at origin and looking at the Z axis):
1 0 0 0
0 1 0 0
0 0 0 0
0 0 1 0
After multiplying with this matrix and then renormalizing the w coordinate you have exactly that. Each x,y,z,1 point becomes x/z,y/z,0,1.
However there is no depth information (Z is 0 for all points) so a depth buffer/filter won't work. For that we can a a parameter to the matrix so the depth information remains available:
1 0 0 0
0 1 0 0
0 0 0 1
0 0 1 0
Now the resulting point contains the inverse depth in the Z coordinate. Each x,y,z,1 point becomes x/z,y/z,1/z,1.
The extra parameters are the result of mapping the coordinates into the (-1,-1,-1) - (1,1,1) device box (the bounding box where if you are outside of it the point won't get drawn) using a scale and a translate.

Algorithm for toggling values in a matrix [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
There's a matrix of NxM, with each element as 0 or 1. We select a specific r, c (such that 1<=r<=N and 1<=c<=M) and starting from (0,0) to (r-1,c-1), we toggle the values. That is, 0 becomes 1 and 1 becomes 0.
Basically, each "move" is to toggle all the values in an arbitrary top-left sub-matrix of our original matrix.
I need to write a function to calculate the minimum number of moves such that all elements of the matrix end up at 1, but not getting how to do it. Please help.
For example, in the following matrix (N == 2 and M == 4):
0 1 0 0
1 0 0 1
after doing the move (2, 1) we'll end up with this matrix: (note the toggled values in the first column and unchanged values in the rest of the matrix.)
1 1 0 0
0 0 0 1
Following should give you an idea.
If you start from the right bottom corner and traverse the matrix backwards, you can get the number of 'moves'.
Lets call move(r,c) as pressing the button at r,c.
So for example, if the N-1,M-1 entry is a zero, then you will have to press a button at N-1,M-1. Subsequently all entries before it shall get toggled.
Now you check this for each entry in the last row backwards. Subsequently check this for each entry in the last column backwards.
Instead of actually toggling all the entries, you can keep a count for the number of times a 'column' is toggled while traversing a row and the number of times a row is toggled while traversing a column.
Now decrease N by 1 and M by 1 and repeat. The present value of each entry shall be:
Original value ^ number of times its column has been toggled ^ number of times its row has been toggled & 1.

Run parallel code for a matrix dependent on previous values

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.

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.