I generated a graph from this output:
digraph G {
{ rank = same; 1; 2; 3; 4; 5; 6; 7; 8; 9; };
2 -> 3 [label="hi there"];
}
I would however like the arrow to be from 3 to 2 instead. I tried changing the direction of the arrow (<-) but it didn't work.
Okay I've found it, all I have to do is use [dir=back].
However I still have not figured out how to make the distance between the nodes be the same between all nodes, it's bigger between 2 and 3. See how to generate img with same spacing
Related
I have 2 series - both diffenent size. Screen below show bad summ.
I'm use below code to summ 2 series.
SDIAppForm->Chart4->Series[4]->SetFunction(new TAddTeeFunction(SDIAppForm->Chart4));
SDIAppForm->Chart4->Series[4]->DataSources->Add( SDIAppForm->Chart4->Series[3]);
SDIAppForm->Chart4->Series[4]->DataSources->Add( SDIAppForm->Chart4->Series1);
SDIAppForm->Chart4->Series[4]->CheckDataSource();
Series1 and Series3 are red colour, Black is wrong summ (partially overlap series 3
Do you have any simple idea how I can solve problem? I'm not an expert with TeeChart so I will be appriciate for simple example how to solve this problem.
Problem solved with interpolate function
float InterpolateLineSeries_Power( double XValue, int APMChart4SeriesCounter_)
// FirstIndex, LastIndex: Integer; XValue: Double): Double;
{
int Index=0;
double dx,dy;
for ( Index = SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->FirstDisplayedIndex(); Index < SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->LastValueIndex ; Index++) {
if ((SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->XValues->Value[Index])>XValue) break;
}
//safeguard
if (Index<1) Index=1;
else if (Index>=SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->Count()) Index=SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->Count()-1;
// y=(y2-y1)/(x2-x1)*(x-x1)+y1
dx=SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->XValues->Value[Index] - SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->XValues->Value[Index-1];
dy=SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->YValues->Value[Index] - SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->YValues->Value[Index-1];
if (dx!=0)
return dy/dx*(XValue - SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->XValues->Value[Index-1]) + SDIAppForm->Chart4->Series[APMChart4SeriesCounter+3]->YValues->Value[Index-1] ;
else return 0;
}
So I was looking at a problem and I just cant figure it out. At least not in any manner I am happy with.
how do you rotate values in a 2D array.
So if the array is
1 2 3
4 5 6
7 8 9
After rotating it should be
4 1 2
7 5 3
8 9 6
The input is from a file where the first value is the number of rows/columns. So it can be a 2 x2 matric, a 3x 3 matrix, a 4x4 matrix, etc.
Can anyone help me out here?
Looping in a circle:
int x=0, y=0;
int prev = matr[0][0];
do{
if (x==0 && y<dim){ // move right on top row
y++;
}
else if(y==dim-1 && x<dim){ // move down
x++;
}
else if(x==dim-1 && y>0){ // move left on bottom row
y--;
}
else{
x--;
}
curr = matr[x][y];
matr[x][y] = prev;
prev = curr;
}while (x==0 && y==0);
This isn't so much as an algorithm as it is a simple solution.
I think the answer to this can be made up of 4 sub methods: MoveTopRow(), MoveBottomRow(), MoveLeftColumn() and MoveRightColumn().
Generally speaking, create a new matrix and fill in the values using these methods. For example:
void MoveTopRow(int dimention)
{
new_matrix[0][0] = old_matrix[1][0];
for (int i = 1 ; i < dimention ; i++)
{
new_matrix[0][i] = old_matrix[0][i-1];
}
}
I think the rest of the methods are self explanatory...
It's a common knowledge that coloring vertices of a graph is NP-complete.
It's also known that there are efficient greedy algorithms that can get an approximate solution.
Why not use these randomized greedy algorithms to calculate a coloring with k colors and then use some slower algorithms to reduce k?
In my situation I know the minimal number of colors that are sufficient to color graph G - let's call it K. I've also managed to implement SL algorithm which gave me (K+2)-coloring. One color was used only to color one vertex so I managed to remove it by manually recoloring some other nodes. Therefore, I have (K+1)-coloring and would like to write an algorithm that would reduce K (or rather K+1) by 1.
I've tried to do it manually - I found a color that is used in the minimal number of vertices colored by the same color and reduced this color's uses to 3. I have to recolor only 3 nodes.
One idea is to make 3 recursive calls - one for each badly colored nodes. Let's analyze what the recursive function would have to do for node v. It would have to check every color apart from v's color and the one we'd like to remove. So for each color c it should set v's color to c and make a recursive call for each node which is a neighbour of v and has color c. After checking all colors we should retrieve v's old color and set it again. One more optimisation may be not trying to change v's color to one that more than x of his neighbours has (as the recursion tree would be too deep) - but for too small x it may not be able to change the color at all.
Another idea is to check nodes whose color can be changed (not to a color that we want to remove) so that it wouldn't collide with neighbours' colors. And make recursive calls to change other nodes' colors until one color which we want to remove will be recolored.
Here's my implementation of the first algorithm which was intended to work for n < 90 but doesn't seem to end (500 minutes of execution):
#include<stdio.h>
#include<assert.h>
#include<vector>
using namespace std;
vector<int> graph[99];
int hash[10009], color[99];
const int colors = 9, color_to_change = 7;
void change_color(int v)
{
int tmp = color[v], count;
for(int i = 1; i <= colors; ++i)
{
count = 0;
for(int j = 0; j < graph[v].size(); ++j)
count += color[graph[v][j]] == i;
if(!count)
{
color[v] = i;
return;
}
if(count < 4 && i != color_to_change && i != color[v])
{
color[v] = i;
for(int j = 0; j < graph[v].size(); ++j)
if(color[graph[v][j]] == i)
change_color(graph[v][j]);
}
}
color[v] = tmp;
}
int main()
{
int n, m, a, b, max = 0, j = -1;
scanf("%d%d", &n, &m);
while(m--)
{
scanf("%d%d", &a, &b);
assert(a != b);
if(hash[a*100+b] || hash[b*100+a])
continue;
assert(a*100+b < 10000 && b*100+a < 10000);
hash[a*100+b] = hash[b*100+a] = 1;
graph[a].push_back(b);
graph[b].push_back(a);
}
for(int i = 1; i <= n; ++i)
scanf("%d", &color[i]);
for(int i = 1; i <= n; ++i)
if(color[i] == color_to_change)
change_color(i);
for(int i = 1; i <= n; ++i)
printf("%d ", color[i]);
return 0;
}
Any ideas how to make it faster?
I've only looked at the code briefly, and read your explaination, but it seems you get into an infinite loop with switching back and forth between neighbours. You'll need to store a flag in each node to note that it's currently being recoloured, and only recurse into those neighbours that are not currently being recoloured.
However - this algorithm looks like its exponential in the worst case - and I'm pretty sure there are cases where a K coloured graph can not be recoloured to a K-1 graph without changing some large fraction of the graphs, even if the number of nodes of colour K is only 1.
Here's an example Graph with simple topology. Its clear that it can be two coloured (R,G), and we have a three colour version using (R,G,B). The only way to recolour it correctly is to change approximately 1/2 the nodes colours, ending up with one of the other versions below. () denotes the single node of colour B, and [] denotes the sections that need to get recoloured.
3 colour version : R-G-R-G-R-G-(B)-R-G-R-G-R-G-R
2 colour version 1: [R-G-R-G-R-G- R]-G-R-G-R-G-R-G
2 colour version 2: G-R-G-R-G-R-[G -R-G-R-G-R-G-R]
This means the minimum depth of your (potentially exponential) search may be more than 1/2 the number of nodes. This may kill sensible performance times (or may not depending on the topology of the graphs I guess.)
I am working on implementing a voxel octree raycaster, and the only thing left is rearranging the data to populate the leaf level of the octree, so that the data then can be averaged to build the lower levels of the tree.
I'm thinking in 2D (quadtree) initially for convenience. I have the data ordered like in the left in the drawing, and I am currently able to rearrange it like to the right. Example is 8x8.
However, I realized that I need to order the data in node order, like in the drawing below:
In other words, I want to go from an array where the data correspond to indices like this:
[0 1 2 3 4 5 6 7 8 9 ... 63]
to an array that would have the data in this order:
[0 1 4 5 16 17 20 21 2 3 ... 63]
for an 8x8 quadtree example.
I can't figure out how to do it. My main problem is dealing with an arbitrary tree size. I could probably hard-code a set of nested loops if I knew the size beforehand, but that it obviously not a great or elegant solution. I'm thinking there might be a recursive way achieve it.
This it what my quick and dirty sketch for sorting the data in the way described in picture one. It basically works by keeping track of four positions in the original data, and then stepping these forward as the new array gets filled. As far as I have been able to tell, this works fine but is not extendable to my needs:
int w = 8;
int[] before = new int[w*w*w];
int[] after = new int[w*w*w];
for (int i=0; i<w*w*w; i++) {
before[i] = i;
}
int toFill = 0;
int front = 0;
int back = w;
int frontZ = w*w;
int backZ = w*w + w;
do {
for (int i=0; i<w/2; i++) {
for (int j=0; j<w/2; j++) {
after[toFill++] = front++;
after[toFill++] = front++;
after[toFill++] = back++;
after[toFill++] = back++;
after[toFill++] = frontZ++;
after[toFill++] = frontZ++;
after[toFill++] = backZ++;
after[toFill++] = backZ++;
}
front += w;
back += w;
frontZ += w;
backZ += w;
}
front += w*w;
back += w*w;
frontZ += w*w;
backZ += w*w;
} while (toFill < w*w*w);
for (int i=0; i<w*w*w; i++) {
println("after " + i + " " + after[i]);
}
For the problem I stated, phs hinted me that it is called a Z-order curve. Thanks to that, I found this question: Z-order-curve coordinates where an algorithm for the 2D case is presented. I tried it, and it works.
Here are a few implementations of the 3D case as well: How to compute a 3D Morton number (interleave the bits of 3 ints)
You have a stack of n boxes, with widths wi, heights hi, and depths
di. The boxes cannot be rotated and can only be stacked on top of one
another if each box in the stack larger than or equal to the box above
it in width, height, and depth. Implement a method to build the
tallest stack possible, where the height of a stack is the sum of the
heights of each box.
I know there are a couple of articles to talk about using dynamic programming to solve it. Since I'd like to do practice in writing recursion code, I wrote the following code:
const int not_possible = 999999;
class box{
public:
int width;
int depth;
int height;
box(int h=not_possible, int d=not_possible, int w=not_possible):
width(w), depth(d), height(h) {}
};
bool check_legal(box lower, box upper){
return (upper.depth<lower.depth) &&
(upper.height<lower.height) &&
(upper.width<lower.width);
}
void highest_stack(const vector<box>& boxes, bool* used, box cur_level, int num_boxes, int height, int& max_height)
{
if(boxes.empty())
return;
bool no_suitable = true;
for(int i = 0; i < num_boxes; ++i){
box cur;
if(!(*(used+i)) && check_legal(cur_level, boxes[i])){
no_suitable = false;
cur = boxes[i];
*(used+i) = true;
highest_stack(boxes, used, cur, num_boxes, height+cur.height, max_height);
*(used+i) = false;
no_suitable = true;
}
}
if(no_suitable){
cout << height << endl; //for debug
if(height > max_height)
max_height = height;
return;
}
}
I've tested it using a lot of examples. For example:
boxes.push_back(box(4,12,32));
boxes.push_back(box(1,2,3));
boxes.push_back(box(2,5,6));
highest_stack(boxes, used, cur, boxes.size(), 0, max_height);
In the function highest_stack, there is one line cout << height << endl; for output. If I comment no_suitable = true;
the output is: 1 2 4; 1 2; 1, 1 4;
if I don't comment no_suitable = true;
the output is: 1 2 4; 2 4; 4; 1 2; 2; 1; 1 4; 0
Both of them can give the correct result which is 7.
My question is:
(1) Can anyone help me verify my solution?
(2) Is there any more elegant recursive code for this problem?
I don't think my code is elegant.
Thanks
I would make a directed graph where the nodes are boxes and the edges go from a box to a box that can be put on top of it. Then I'd use use the longest path algorithm to find the solution.
Design the relation as a Set array of boxes.(Set[]) i.e. each position has a array of boxes.
Initialize each box with an index.
For each box check boxes that can be placed above the current box(box[i]) add it to the set in the set array i.e. set[i].add(box)
Run DFS with the boxes that can be placed above (the role of adjacent)
Maintain a marked[], count[] and boxTo[] arrays of boxes.
Go over the count array and find the largest value.
Traverse the way to the bottom box using the boxTo[] array.