populate n-ary tree using tree.hh STL Like - c++

how can i use tree.hh: an STL-like C++ tree class to populate my tree and obtain the tree below. some help would be appreciate. A and G are root node
thank's
A G
______|____ |
/ | \ |
B C D H
| | | |
| E | |
\_____/ | |
| | |
F | |
|_________|______|
|
I
|
J
In then code above, i'm using depth first search to enumarate item in the list. i have few data formated like this
typedef tree<std::string> TreeNode;
typedef struct
{
int nBases;
char * name;
} BASES;
BASES rgbases[] =
{
{0xB, "J"},
{0xA, "I"},
{0x1, "H"},{0x0, "G"},
{0x5, "F"},{0x2, "E"},{0x1, "C"},{0x0, "A"},
{0x1, "D"},{0x0, "A"},
{0x1, "B"},{0x0, "A"}
};
//here i'm trying to populate my tree
void populateTree(TreeNode &tr, BASES *pBaseArray, int numBase)
{
int n = 0;
while ( n < numBase )
{
BASES *pBase = &pBaseArray[n];
if ( pBase->nBases > 0) // Check for children of the new node
populateTree(tr, pBaseArray + (n + 1),pBase->nBases);
// i suppose i need to insert tree code part here
n += pBase->nBases + 1;
}
}
void BuildTree(TreeNode &tr)
{
populateTree(tr, rgBases, _countof(rgBases));
}

A tree, spanning the original graph presented, could be obtained in removing the edges linking node A with B and D(or possibly the edges linking node A with C and D). The tree class would then be applicable:
A G A G
| | ______| |
| | / |
B C D H B C D H
| | | | | | | |
| E | | | E | |
\______/ | | \______/ | |
| | | | | |
F | | F | |
|_________|______| |__________|______|
| |
I I
| |
J J
Edges in the above tree giving rise to loops, could be recorded separately, that is AB and AD, could be noted in a structure, apart from the tree class. Merging the tree with such a structure would recover the original graph.

Related

Simplify if-elif-else-if-else conditional structure into a single if-else

This would not be a problem for me if this was a "regular" program, however I am working with program synthesis and I have to have a code as compact as possible.
Consider the pseudocode below:
if A:
return 'n'
elif B:
return 'y'
else:
if C:
return 'n'
else:
return 'y'
A, B and C are boolean conditions (functions that returns a boolean in my real problem - their implementations are not important). I need this whole if-elif-else-if-else structure to be condensed into a single if-else structure.
The closest I got was:
if A or C:
return 'n'
else:
return 'y'
However, it fails for a single test case where A = False, B = True, C = True: it returns 'n' instead of 'y'.
The correct truth table is shown below for reference.
|-------|-------|-------|----------|
| A | B | C | Result |
|-------|-------|-------|----------|
| T | T | T | n |
|-------|-------|-------|----------|
| T | T | F | n |
|-------|-------|-------|----------|
| T | F | T | n |
|-------|-------|-------|----------|
| T | F | F | n |
|-------|-------|-------|----------|
| F | T | T | y |
|-------|-------|-------|----------|
| F | T | F | y |
|-------|-------|-------|----------|
| F | F | T | n |
|-------|-------|-------|----------|
| F | F | F | y |
|-------|-------|-------|----------|
if A or ( C and not B):
return 'n'
else:
return 'y'
Start from the logic table, and use boolean properties

difference between size_t (*B)[N] and size_t *B[N]

what's the difference between these two lines of code in c++?
size_t (*B)[N] = new size_t[N][N]; and
size_t *B[N] = new size_t[N][N];
first one compiles correctly but with second line, g++ gives this error
matrixim.cpp:43:20: error: array must be initialized with a brace-enclosed initializer
43 | size_t *B[N] = new size_t[N][N];
size_t *B[N]
Here, B is an array of N pointers to size_t
size_t (*B)[N]
Here, B is a pointer to an array of N size_ts
Both of these constructs could be used to create something approximating a 2-dimensional array, but their layout in memory is very different.
size_t *B[N] would look something like this:
B +-------------+-------------+-----+-------------+
+--------+ | B[0][0] | B[0][1] | ... | B[0][N-1] |
| B[0] +--->+-------------+-------------+-----+-------------+
+--------+
| B[1] +--->+-------------+-------------+-----+-------------+
+--------+ | B[1][0] | B[1][1] | ... | B[1][N-1] |
| | +-------------+-------------+-----+-------------+
| ... |
| |
+--------+
| B[N-1] +--->+-------------+-------------+-----+-------------+
+--------+ | B[N-1][0] | B[N-1][1] | ... | B[N-1][N-1] |
+-------------+-------------+-----+-------------+
B is an array of N pointers to size_t, each of which points to the first element of an array of N size_t.
size_t (*B)[N] would look something like this:
B +---------------------------------------------------+
+----+ | B[0] |
| +--->+ +-------------+-------------+-----+-------------+ |
+----+ | | B[0][0] | B[0][1] | ... | B[0][N-1] | |
| +-------------+-------------+-----+-------------+ |
+---------------------------------------------------+
| B[1] |
| +-------------+-------------+-----+-------------+ |
| | B[1][0] | B[1][1] | ... | B[1][N-1] | |
| +-------------+-------------+-----+-------------+ |
+---------------------------------------------------+
| |
| ... |
| |
+---------------------------------------------------+
| B[N-1] |
| +-------------+-------------+-----+-------------+ |
| | B[N-1][0] | B[N-1][1] | ... | B[N-1][N-1] | |
| +-------------+-------------+-----+-------------+ |
+---------------------------------------------------+
Here, B is a pointer to the first element of an array of N arrays of N size_t.

STL or ranges algorithm to efficiently find n consecutive elements that satisfy predicate

I have the following function(toy example, but good for demonstration):
// finds the iterator pointing to the start of n consectuve 47s or return values.end() if not found
auto find_n_47s(const int n, const std::vector<int>& values){
std::vector<bool> predicate_result;
predicate_result.reserve(values.size());
std::transform(values.begin(), values.end(), std::back_inserter(predicate_result), []
(const auto& val){return val==47; });
std::vector<bool> search_pattern(n, true);
auto it= std::search(predicate_result.begin(), predicate_result.end(),
search_pattern.begin(), search_pattern.end());
return values.begin() + std::distance(predicate_result.begin(), it);
}
I am looking for a nicer and more efficient way to accomplish the same thing.
My problems:
I can not use manual iteration + std::all_of(from current element to
n elements ahead) because it is too slow(in theory for every element
that I do up to n predicate applications).
My solution allocates memory and calculates predicate for every
element although we might find the result in the first 1% of the
elements.
Full code here: https://wandbox.org/permlink/rBVFS64IcOI6gKe6
As Cruz Jean pointed out you can use search_n:
https://wandbox.org/permlink/ENgEi5ZVPDx1D1GQ
The GCC implementation of search_n
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_algo.h
does not check every element:
|---------|---------|---------|----------------|
|  Find first occurence of n=7 consecutive 47  |
|---------|---------|---------|----------------|
|  vector |  step#  |  count  |            |
|---------|---------|---------|----------------|
|  47     |         |         |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  0      |  4      |  3      |            |
|  47     |  3      |  3      |            |
|  47     |  2      |  2      |            |
|  47     |  1      |  1      |  start     |
|  47     |         |         |            |
|  47     |         |         |            |
|  0      |  5      |  0      |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |  6      |  0      |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |  7      |  0      |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |  9      |  1      |            |
|  47     |  8      |  1      |            |
|  47     |  15     |  7      |  success   |
|  47     |  14     |  6      |            |
|  47     |  13     |  5      |            |
|  47     |  12     |  4      |            |
|  47     |  11     |  3      |            |
|  47     |  10     |  2      |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  …      |         |         |            |
|---------|---------|---------|----------------|

check whether if two squares are intersecting with each other [duplicate]

This question already has answers here:
Determine if two rectangles overlap each other?
(21 answers)
Closed 9 years ago.
I would like to check if two square are intersecting with each other. My idea is this
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
bool x = check line(i) of first square intersect with
line (j) of the second square
if (x) return;
}
any idea to optimize this code?
You don't have to loop through all coordinates to check if two squares intersect.
Here's a simple solution which will work as long as the squares are not rotated.
Say that you represent a square by its top left corner coordinate and its side length. Let aX and aY represent the coordinates and aLen the side length of square A, and vice versa for square B.
Then to check if square B intersects with square A evaluate this:
(aX < (bX + bLen) && (aX + aLen) > bX)
&& (aY < (bY - bLen) && (aY - aLen) > bY)
In other words, there are four possible scenarios and you check if either of square B's corners are within the X-range of square A and that either of square B's corners are within the Y-range of square A.
(Y)
^
| 1: 2:
| +--------+ +--------+
| | | | |
| | A +--|-----+ +-----+--+ A |
| | | | | | | | |
| +-----+--+ B | | B +--+-----+
| | | | |
| +--------+ +--------+
|
| 3: 4:
| +--------+ +--------+
| | | | |
| | B +--|-----+ +-----+--+ B |
| | | | | | | | |
| +-----+--+ A | | A +--+-----+
| | | | |
| +--------+ +--------+
|
+-------------------------------------------------> (X)
For more info see this answer of a similar question: Determine if two rectangles overlap each other?

How to get minimum count rectangles that covers another pile of rectangle?

Assume I have a pile of rectangles, some of which intersect, some isolate. E. g.
+--------------- + +-------- +
| | | |
| | | |
| A | | C |
| +---------------- + | |
| | | | +---------+-------- +
| | | | | |
+---------|----- + B | | D |
| | | |
| | +------------------ +
+---------------- +
+------------------ + +-------- +
| | | |
| E | | X |
+-------------------+ | |
| | +-------- +
| | +------------ +
| | | |
| F | | |
| | | Y |
| | | |
+-------------------+ +------------ +
Rect A, B intersect with each other, C, D have one same point, E, F have two same points, X, Y are isolated.
I have two questions:
How to partion these rectangles into rectangles which cover A, B, C, D, E, F, X, Y exactly also have minimum count like this:
+---------+----- + +-------- +
| | | | |
| | | | |
| | | | |
| | +--------- + | |
| | | | +---------+-------- +
| | | | | |
+---------+ | | | |
| | | | |
| | | +-------------------+
+------+----------+
+------------------ + +-------- +
| | | |
| | | |
| | | |
| | +---------+
| | +------------ +
| | | |
| | | |
| | | |
| | | |
+-------------------+ +-------------+
How to cover intersected rectangles with big ones? Like this:
+---------------------------+ +-------------------+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | +-------------------+
+---------------------------+
+-------------------+ +---------+
| | | |
| | | |
| | | |
| | +---------+
| | +------------ +
| | | |
| | | |
| | | |
| | | |
+-------------------+ +-------------+
For Q1, I've no idea at all....
For Q2, I wrote some code in C++ but have poor efficiency. I believe there're better methods/algorithm.
bool intersectRect(const Rect& rect1, const Rect& rect2) {
/* if rect1 and rect2 intersect return true
else return false
*/
}
Rect mergeIntersectRects(const set<Rect>& rects) {
// suppose rects are all intersected. This function only return a smallest Rect that cover all rects.
}
set<Rect> mergeRectToRects(const set<Rect>& rectset, const Rect& rect) {
set<Rect> _intersect_rects;
set<Rect> _unintersect_rects;
for(set<Rect>::const_iterator it = rectset.begin();
it != rectset.end();
++it) {
if(intersectRect(*it, rect))
_intersect_rects.insert(*it);
else
_unintersect_rects.insert(*it);
}
if(!_intersect_rects.empty()) {
_intersect_rects.insert(rect);
return mergeRectToRects(_unintersect_rects,
mergeIntersectRects(_intersect_rects));
}
else {
_unintersect_rects.insert(rect);
return _unintersect_rects;
}
}
First, I'm assuming that your rectangles are all axis-aligned.
For Q1, one option would be to sweep the plane while maintaining a list of line segments along the sweep line that lie in the interior of the rectangles. As you discover each rectangle vertex during the sweep you can check to see if it modifies the current interior segments and if so, start or end a rectangle as necessary.
For example, let's say your sweep line moves left to right:
Current
Interior
|
+-|------------- + +-------- + *
| | | | | |
| | | | | |
| | A | | C | |
| | +---------------- + | | |
| | | | | +---------+-------- + |
| | | | | | | |
+-|-------|----- + B | | D | *
| | | | |
| | | +------------------ +
| +---------------- +
|
+-|---------------- + +-------- + *
| | | | | |
| | E | | X | |
| |-----------------+ | | |
| | | +-------- + |
| | | +------------ + |
| | | | | |
| | F | | | |
| | | | Y | |
| | | | | |
+-|-----------------+ +------------ + *
|
When the sweep line is in the position shown above, there are two interior segments. Namely, that inside A and that inside (E U F). When the sweep line reaches the leftmost edge of B, we output a rectangle for the portion of A lying to the left. We then replace the interior of A in the segment list with the interior of (A U B).
Current
Interior
|
+---------+-|--- + +-------- + *
| | | | | | |
| | | | | | |
| | | | | C | |
| | |-------------- + | | |
| | | | | +---------+-------- + |
| | | | | | | |
+---------+ |--- + B | | D | |
| | | | | |
| | | +------------------ + |
+-|-------------- + *
|
+-----------|------ + +-------- + *
| | | | | |
| | | | X | |
| |-------+ | | |
| | | +-------- + |
| | | +------------ + |
| | | | | |
| | | | | |
| | | | Y | |
| | | | | |
+-----------|-------+ +------------ + *
|
For Q2, the answer could be computed during the same sweep by keeping track of the x-coordinate at which a segment was first added to the list (e.g. "left side of A") as well as the min and max y-coordinates that it spans during its lifetime (e.g. bottom of B to top of A). When the segment is finally removed from the list (e.g. "right side of B"), then output a rectangle using these four coordinates.
Sorting the rectangle vertices lexicographically in a preprocessing step would be O(n * log n). Processing them would be O(log n) since you can do a binary search on the known interior ranges. The total runtime should be O(n * log n).
Q1: this is called partition of rectilinear polygon. Answer from Rob's comment has very good description. I found paper mentioned in the answer useful.
Q2: I suppose that you don't want two covers of non-intersecting regions to intersect. Like cover for 3 rectangle, 2 rectangle producing L and rectangle intersection cover of L but not any L rectangle.
If it is like that, than it is possible to incrementally create covers. Here is a simple algorithm for it.
covers = {A}
for each rectangle R
while there is a cover that intersects R, say C
remove C from covers and set R = cover of R and C
add R to covers
This code is not efficient in standard form. With good structure for covers structure, it can be efficient.
Here's the algorithm: http://goo.gl/aWDJo
You can read about finding the convex hull algorithms: http://ralph.cs.cf.ac.uk/papers/Geometry/boxing.pdf
I'd use the method suggested by #Damon but speed up neighbouring rectangle search with some spatial indexing structure, for example a quadtree or a grid. You'd need two of them, first built over the set of input rectangles, to search for intersecting rectangles to split, and second built over the set of split rectangles obtained in first step, to search adjacent rectangles to merge. That should speed things up considerably compared to the naive approach.