|
Hello,
I have a class Tree
public class Tree{
private int tree_height;
private int tree_width;
private int nodes_count;
private List<Node> tree_nodes;}
and class node
public class Node{
private object state;
private int ordering;
private int height;
private int parent;}
I would like to create a method to add a branch to a tree
here is my code:
public void AddBranch(Tree branch, int node_num)
{
if (nodes_count >= node_num && node_num > 0)
{
int last_el_ordering = nodes_count,
first_parent_height = tree_nodes[node_num - 1].Height,
first_parent_ordering = tree_nodes[node_num - 1].Ordering;
tree_nodes.Add(new Node(branch.Tree_nodes.First().State, last_el_ordering + 1, first_parent_ordering, first_parent_height + 1));
foreach (Node el in branch.Tree_nodes.Skip(1))
tree_nodes.Add(new Node(el.State, el.Ordering + last_el_ordering, el.Parent + last_el_ordering, el.Height + first_parent_height));
tree_nodes = tree_nodes.OrderBy(match => match.Height).ToList();
int i = 1;
foreach (Node el in tree_nodes)
{
List<Node> temp = tree_nodes.ToList().FindAll(match => match.Parent == el.Ordering).ToList();
el.Ordering = i++;
if (temp.Count() > 0)
foreach (Node el2 in temp)
el2.Parent = el.Ordering;
}
}
}
I have a little problem when I add new node after connecting two trees eg: to tree
first
a b c
false false false
i want to add a branch
d
false
to the first node
The label "false" which is child of "d" is getting misplaced and I cannot figure out why. Please help.
the input
Tree testing_tree1 = new Tree(new Node("start"));
List<string> temp = new List<string> { "a", "b", "c" };
foreach(string el in temp)
testing_tree1.AddBranch(new Tree(new Node(el)),1);
for (int i = 0; i < 3; i++)
testing_tree1.AddBranch(new Tree(new Node("false")), i+2);
Tree testing_tree2 = new Tree(new Node("d"));
testing_tree2.AddBranch(new Tree(new Node("false")), 1);
testing_tree1.AddBranch(testing_tree2, 1);
testing_tree1.DisplayTree();
Console.Read();
the output
tree depth: 3 tree width: 4 nodes count: 9
Node number:1, Node parent:-1, Node height:1, Node value:start
Node number:2, Node parent:1, Node height:2, Node value:a
Node number:3, Node parent:1, Node height:2, Node value:b
Node number:4, Node parent:1, Node height:2, Node value:c
Node number:5, Node parent:1, Node height:2, Node value
Node number:6, Node parent:2, Node height:3, Node value:false
Node number:7, Node parent:3, Node height:3, Node value:false
Node number:8, Node parent:4, Node height:3, Node value:false
Node number:9, Node parent:8, Node height:3, Node value:false
|
|
|
|
|
Use the debugger and see what your code is really doing.
The debugger permit you to execute your code line by line and inspect variables during execution.
You should see that somewhere, your code is not doing as expected.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Good morning sirs,
please I need your help on a project I am working on, I am new to this discussion forum on algorithm(but not to CodeProject) and new to algorithm itself, someone directed me here.
I started a project on algorithm for plotting SkewT-lnP chart, I have written all other algorithms to do all needed calculations including that of the grids but am stock on how to write the main algorithm for plotting the SkewT chart and the type of graphics library to use. am using c# language and vs2010 IDE.
thanks
|
|
|
|
|
Don't try to write your own graphics routines.
There are a number of open source and commercial libraries available. For the commercial ones, use their trial version.
Create a wrapper for your library calls so you can try different options (VisiBlox; SciChart; etc.).
If your product becomes commercially viable, then get a license if necessary.
|
|
|
|
|
OK, to summarise your problem, you have more than 3 dimensions to plot on a graph.
When plotting 3 dimensions on a graph, we use a projection to map these to 2 dimensions. This is based on the orientation of the axes. You need to create a similar projection from you multidimensional coordinates to a 2 dimensional plane.
Check out 4D Visualization: Projections (1)[^] as a starting point.
|
|
|
|
|
Hello,
Warning: my tree class does not contain links between nodes. To connect two nodes I use leafs. Parent -> Node(linking) -> Node(desired). That way I get "Parameter1" -> "0" -> "false".
Description: examples is list of string[] such that string[0] is "false" or "true" and the rest is "0" - "10" strings as attributes values. AttributesVector has Names - List<string> of attributes and Possible_values List<int> with max value of attributes (10 for all). My output is showing all possible parameters values (0, 1, 2, 3,..., 10) with response 'true' or 'false' and it should consider only the values that make any sense (attribute 1 never get value 0). I don't know why. I also get too many nodes in the tree. I have datasets that I can provide along with csv reader.
private Tree DecisionTreeLearning(List<string[]> examples, AttributesVector attributes_vector, Tree default_tree)
{
if (examples.Count() == 0)
return default_tree;
if (AllExamplesSameClassification(examples))
return new Tree(new Node(examples.First()[0]));
if (attributes_vector.Vector_length == 0)
return MajorityValue(examples);
string chosen_attribute = ChooseAttribute(examples, attributes_vector);
Tree current_tree = new Tree(new Node(chosen_attribute));
Tree current_majority_value = MajorityValue(examples);
int chosen_attr_index = attributes_vector.Names.FindIndex(match => match == chosen_attribute);
int temp2 = 2;
for (int i = 0; i < attributes_vector.Possible_values[chosen_attr_index] + 1; i++)
{
List<string[]> matching_examples = examples.FindAll(match => match[chosen_attr_index] == i.ToString());
List<string> new_attributes_names = new List<string> { };
foreach (string el in attributes_vector.Names)
{
string temp = string.Copy(el);
new_attributes_names.Add(temp);
}
new_attributes_names.RemoveAt(chosen_attr_index);
List<int> new_list_of_possible_values = new List<int> { };
foreach (int el in attributes_vector.Possible_values)
{
int temp = el;
new_list_of_possible_values.Add(temp);
}
new_list_of_possible_values.RemoveAt(chosen_attr_index);
AttributesVector new_attributes_vector = new AttributesVector(new_attributes_names, new_list_of_possible_values);
Tree subtree = new Tree();
subtree = DecisionTreeLearning(matching_examples, new_attributes_vector, current_majority_value);
current_tree.AddBranch(new Tree(new Node(i.ToString())), 1);
current_tree.AddBranch(subtree, temp2++);
}
return current_tree;
}
modified 29-Jan-16 5:17am.
|
|
|
|
|
|
I have 7500 points with me. when I plot in excel it will draw a sine wave with a glitch. I want to know the exact start and end position of the glitch. Is there any algorithm which I can use to find this. i am developing the application in C#. Below are 7500
|
|
|
|
|
i don't know if this is optimal, but it's easy:
if you have a nice big smooth sine wave, you can simply go through each pair of points in order, calculating the slope between each pair. the glitch will start at the spot where the slope changes by more than some threshold. to find the end of the glitch, do the same in reverse.
|
|
|
|
|
Another (more expensive) way would be to take the Fourier transform of your data, which should give you a sharp peak representing the sine wave, and high-frequency stuff representing the glitch. Subtract the sharp peak from the transform, and then use an inverse transform back to the time domain.
You should get a dataset which is very close to zero for most of the range, differing from zero only at the glitch.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Imagine a circular table with a number of metal weights hanging off the table on strings of the same length spaced at regular intervals around the circle. Let's say there are twelve weights.
Let's also assume some loss of "pull" here due to friction, some "inertia."
Now, imagine that all the weights are equal, and that you are looking down at the table from above it. You see all the strings connect to one central knot, and, the weights being equal, that knot appears in the center of the circular table.
I can't explain to you why I've got this bug in my brain to model how the location of the knot would move in response to variation in the weights, it's just something that came to my mind and interested me.
Is there a type of algorithm that would be useful for dealing with weighted vectors and relative forces ?
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 24-Jan-16 5:36am.
|
|
|
|
|
I'm not sure I see what you mean here. But if all strings are attached to a freely hanging weight surely the resulting force would be a vector depending on F=m*a? if the weight is heavier on one side surely the weights would be pulled towards the heavier object?
|
|
|
|
|
Kenneth Haugland wrote: if the weight is heavier on one side surely the weights would be pulled towards the heavier object? Yes.
But, what (in my mind is the 'non-obvious') I am interested in is a situation like this:
1. there are 12 weights that differ, but their relative 'pulls' balance so that the 'knot' is a the center of the 'table.'
2. now imagine the weight at '11 o'clock' becomes #x grams heavier; what is the displacement of the 'knot' from the center.
3. now imagine that each weight has some non-linear function governing the relationship between its 'pull' and its weight-value: so, a change of #x grams in the weight at 11 o'clock changes based on the function's return value.
A real-world example that comes to mind is in Bezier splines, where the control points have an interesting functional relationship to the rendered curve.
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
If you want friction involved you would have to use numerical methods unless you are going to restrict the movements of the threads severely. And without friction, I think the problem will be trivial, at least if you have the position of the weights fixed.
|
|
|
|
|
The general approach to such mechanical problems is to set up a set of coupled differential equations, which together describe the motion of each weight (and therefore - of the knot). Many packages for numerical solution of differential equations exist.
However, this isn't as simple a problem as it appears at first. If you take a simple case where exactly one weight is heavier than the others, the knot will start sliding towards the heavy weight. This means that the knot is no longer centred in the circle, and the strings are chords, not radii. This means that the forces acting on the edge of the table (caused by the strings) now have transverse components, as well.
At some point, the transverse force for each string will be enough to overcome the static friction between the edge of the table and the string. At this point, the weight will start sliding around the edge of the table (toward the heavy weight?). This will in turn change the dynamics of the problem even more.
This sliding effect may happen even with equal weights, if the initial position of the knot is sufficiently off-centre.
The analytical solution of this problem would probably be worth a nice paper in a peer-reviewed Physics journal.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Modeling wise I would expect that understanding the problem would be easier starting with a simpler model.
Start with a square table.
There is no friction on the table or edges.
There are 3 weights: A, B, C
A and B each weigh half as much as C.
A and B are hanging off the left side of the table. There is a 30 degree angle between the ropes and the knot. C is hanging off the right side.
The ropes are very long and have no weight.
Now double the weight of C.
My expectation would be that the knot would move to the right and the angle would decrease.
Fixing the ropes through rings at the table edge would make the force calculations easier (x and y) because the ropes should move (due to force in one direction.) Then one would need to apply calculus to solve once the rings are moved.
Then add three weights on one side and repeat.
I would expect that one would then 'round' the square table to progressively arrive at the final solution.
|
|
|
|
|
Not quite correct.
If the knot is at a constant position, and a string makes a non-perpendicular angle with the edge of the table, we have:
A transverse force (vector pointing along the edge of the table) will exist at the point where the string goes over the edge. If there is no friction, nothing will prevent the string from sliding until it makes a perpendicular angle with the table.
Note that as it slides, the string picks up velocity in the transverse direction. When it reaches the perpendicular position, the direction of the transverse force will reverse, causing the string's velocity to decrease. You end up with an odd type of pendulum.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Interesting thought.
However what is the impact on the rope when conserving momentum given that it has no weight.
|
|
|
|
|
The weight of the rope has little bearing on the result. If you take an ideal pendulum and replace the (weightless) string with a rod, the rod has a moment of inertia which affects the pendulum's movement (and period), but does not change the fundamental form of the movement.
For an example, compare a grandfather clock to an ideal pendulum.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
How can a string that has no mass have a moment of inertia?
Keep in mind that I am not referring to one with a low mass, but rather one that has none.
The force vector in the Y direction (top/bottom) on the string is reduced as the angle decreases but the string has velocity. (Although perhaps that is wrong since how can the force act on it with no mass.)
Daniel Pfeffer wrote: For an example, compare a grandfather clock to an ideal pendulum.
An ideal pendulum has a rod/string which is is controlled by the fixed dynamics of the weight and the connector. It is the mass moving which moves the rod. The rod doesn't move by itself.
If one removes the weight the pendulum no longer works because the physics require gravity on the mass. No mass, no conservation.
Daniel Pfeffer wrote: but does not change the fundamental form of the movement.
Realistically I suspect that if the rope has no mass then the angle cannot change because the force vector in the Y direction would have nothing to act upon.
If the string does have mass then the pendulum dynamics come into play.
|
|
|
|
|
jschell wrote: How can a string that has no mass have a moment of inertia?
The (massless) string may not have inertia, but it does indicate the direction of the force acting between the knot and the weight. For a real-world string (possessing mass), I maintain that when the angle between the string and the edge of the table is sufficiently non-perpendicular, it will slip. The only thing preventing it from slipping is the friction between it and the edge of the table. For an ideal string (possessing no mass), there can be no friction, so as soon as the angle between the string and the edge of the table changes, the position of the string (and the weight attached to it) will change.
jschell wrote: An ideal pendulum has a rod/string which is is controlled by the fixed dynamics of the weight and the connector. It is the mass moving which moves the rod. The rod doesn't move by itself.
If one removes the weight the pendulum no longer works because the physics require gravity on the mass. No mass, no conservation.
Not quite correct. If the rod is rigid and has mass, you could still construct a pendulum with it. The problem with the string is that the string is non-rigid.
For a non-rigid (but fixed-length) pendulum string, each infinitesimal element dl of the string at distance l from the fulcrum would attempt to move at its natural frequency, sqrt(g/l)/2pi. This would cause the string to change its shape from a line to some more complex, time-dependent curve (the exact details are too hairy for me to describe off-hand). The movement is no longer periodic, which you interpret as the pendulum "not working".
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Daniel Pfeffer wrote: For a real-world string (possessing mass), I maintain that when the angle between the string and the edge of the table is sufficiently non-perpendicular, it will slip.
Of course.
Daniel Pfeffer wrote: If the rod is rigid and has mass, you could still construct a pendulum with it.
However that is not what I said nor what was started with. If the rod has a mass then the mass is added to the system, but the rod itself does not define the system because it is consider to be significantly less than the system especially in a "ideal pendulum" thus what I was referring to with "No mass".
Daniel Pfeffer wrote: For a non-rigid (but fixed-length) pendulum string,... which you interpret as the pendulum "not working"
Nothing to do with what I said. You will need to take your own meaning from "ideal pendulum" but I meant that it has no mass. Once you remove the weight in that system the string no longer functions in any way.
In the table problem I am rather certain, now, that even with no friction on the table, if the string had no mass then the angle would never change because there is nothing for the Y force to act upon. With a very small mass and no table friction the string would act as a pendulum but would not conserve the swing because the knot would be moving. That would reduce the angle (conceptual moment to moment) and thus reduce the force vector in the Y direction over time.
|
|
|
|
|
Would this not be just a problem of solving for equilibrium among the various line tensions? Resolve the tensions into X-Y components, sum them , then find that value of X and Y that renders the sum as zero. Interesting problem, Bill. I've always enjoyed such challenges, but I'm many years out of practice.
Will Rogers never met me.
|
|
|
|
|
As I have mentioned above, in the general case this is a dynamic problem, which may have no equilibrium position (i.e. the knot eventually slides off the table).
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Conjecture: if the knot moves at all, it has to fall off eventually.
I can't really formally prove it. But if the knot slides somewhere, the weights will redistribute such that the weights in the direction the knot moved into can be closer together (they will attempt to all move to the place such that their length of string on the table is minimized, that will be the same location for all weights now because the knot isn't in the middle any more). So if there was an imbalance to begin with, it gets worse. So whatever weird things happen with weights swinging on their strings like pendula and weird outward forces from being swung around the table, there is no equilibrium to reach.
|
|
|
|
|