Barnes-Hut tree creating - algorithm

I am currently trying to create a Barnes-Hut octree, however, I still not fully understand how to do this properly. I have read threads here, this article and some others. I believe I do understand how to make a tree if every node contains the information about the indices of particles inside, and if you keep storing the empty nodes. But if you do not want to? How to make a tree such that at the end you will only have necessary information: say, monopoles and quadrupoles for all non-empty nodes. I made so many different attempts that now I am completely confused, to be honest. What should I contain in each node? What would be the pseudocode for such thing?
P.S. By the way, is it different for monopoles and quadrupoles? I mean I can imagine that you do not need the exact information about the particles inside the node to calculate a monopole (it is just a full mass of node), but for quadruple?
Thank you in advance!
P.S. By the way, I use julia language if it is somehow relevant.

Related

Understanding thread-safety

Let use as example standard Binary Search Tree, its nodes having an encapsulated value and pointers to the left and right children and its parent. Its primary methods are addNode(), removeNode() and searchNode().
I want to understand how to make my data structure safe. I however do not know much about thread-safety and want to ask if what I am thinking is correct or not.
My first problem is one of decision: what I want to happen when I have concurrent methods? Let us say I am using my BST to manage a map in a game: the position of the character of a player is represented by an array of doubles, I store the player's positions in my BST in lexicographic order. One player destroyed a target (and his thread want to remove it from my map), but another player is initiating an attack and needs a list of all possible targets inside a certain distance from him, including the target just removed (and his thread want to execute a search that will include the target). How I manage the situation? What about other situations?
My second problem, (I think) my biggest, is one of actuation: how I make happen what I want to happen. I read about things like atomic-operations, linearity, obstruction-free, lock-free, wait-free. I never found out in a single place a simple definition for each, the advantages of that strategy, the disadvantages. What are those things? What am I doing if I am locking the nodes, or if i am locking the pointers to other nodes, or the values encapsulates in the node?
Am I understanding correctly the problematics of thread-safety? Do you have suggestions on problems I should think about, or on written papers and pdfs I should read in order to better understand the problems and their many solutions?

Creating an Intersecting Tree in Fortran

I want to create a tree in Fortran (90) like the one in this picture:
The idea is that I would then be able to follow a path through the tree starting from the root in the following way. At each node perform a check with the value stored there: passing the check move to the left-most child, not passing or having reached a leaf-node move the highest node that the traversal hasn't been to yet. Here is an example of a possible traversal (green indicating passing the test and red not passing):
Importantly, not every node is reached (the ones in black) which is the point of the procedure actually.
So, I think I need is a subroutine that would insert nodes in the tree, in order to build it and another that would allow me to follow paths of the kind described above.
My question is, is this possible? Does this data structure have a name?
Granted that I have virtually no experience with building data structures of this kind, Google has not been much help. An example code would be great but I would be happy to just be referred to some reading where I could learn this.

How do we do Graph representation in Ruby

The real question is how to represent a graph data structure in ruby (some example code might help me understand).
I currently have an idea to represent a graph. that is every Node has an array of neighbourNodes which are object_id of node objects. Is there any better solution? Can i achieve this with some library easily. I have looked at GRATR and RGL. both are outdated (atleast i think so.) Anyway any working example on tuby 2.0.0 will help me a lot.
I have Busstops which compose Routes. Routesis a sequence of Busstops. How would I represent the graph for all the Routes. I want to use Dijkstra's Algorith to find a shortest path between two busstops (which may or may not lie on the same Route. Which means you have to change a bus on the way)).
This question is really vague, so you should expect to receive vague answers. Here's mine:
It's All Data
When you're looking to do something visually, it all starts with data
Your busstops have routes -- this means nothing to Rails or your graphing system. What will mean something is numbers & data; specifically geolocational data (for the bus stops & other geolocational data)
We've never done anything with maps or routes; so I don't know how you'd plot a route, and find the nearest bus stop. I do know, however, that in order to get that working, you'll definitely need to pull the correct data from your database
How I'd Approach It
I'd start by getting all the data you'll need stored in the database:
Each bus stop needs a location (long & lat value)
Each bus stop's route needs to be mapped out (perhaps with sequential waypoints of locational data)
You need a "reference" point (long & lat value to gauge against)
Once you have all these values in place, you'll then be able to get some sort of process sorted to show the data on the graph
As mentioned in the comments, since you do not want to use a new data store such as Neo4j, the best option is using PostGIS and pgRouting. They are relatively easy to implement as they use SQL and are extensions for PostgreSQL.
Since i asked a graph representation in ruby.i came up with this idea that every Node is connected to some other Nodes called Edges.
In ruby I create a Node Object and then push other Node'sobject_ids to thisNode.
newNode.neighbours << otherNode.object_id
There might me other ways. But this came to my mind. Please do tell me if there is a better way to do this.
For now this is what im using.

JUNG - How to get the giant connected component of a graph?

Currently, what I'm doing is:
WeakComponentClusterer<Integer, String> wcc = new WeakComponentClusterer<Integer, String>();
Collection<Graph<Integer,String>> ccs = FilterUtils.createAllInducedSubgraphs(wcc.transform(graph),graph);
The problem is that in ccs is stored all the connected components but I just want the giant one (GCC). Since the order of the clusters in the collection css is not determinated by their size, I have to iterate over the whole collection in order to find the giant cluster. The bad thing is that the graph I'm using is huge and has many clusters; so, that iteration costs a lot.
Since I'm new at JUNG I was just wondering if there's a fast way of retrieving the GCC of a graph. Any help is valid.
Probably the easiest way to solve your problem would be to hack WeakComponentClusterer so that it kept track of the component sizes (or of which one was the largest, since that's what you're interested in) as it was constructing them, and then exposed that information to the user.
This is a modification we might make at some point but it's easy enough for you to to in your local copy of the code.

Store hierarchies in a way that is resistant to corruption

I was thinking today about the best way to store a hierarchical set of nodes, e.g.
(source: www2002.org)
The most obvious way to represent this (to me at least) would be for each node to have nextSibling and childNode pointers, either of which could be null.
This has the following properties:
Requires a small number of changes if you want to add in or remove a node somewhere
Is highly susceptible to corruption. If one node was lost, you could potentially lose a large amount of other nodes that were dependent on being found through that node's pointers.
Another method you might use is to come up with a system of coordinates, e.g. 1.1, 1.2, 1.2.1, 1.2.2. 1.2.3 would be the 3rd node at the 3rd level, with the 2nd node at the prior level as its parent. Unanticipated loss of a node would not affect the ability to resolve any other nodes. However, adding in a node somewhere has the potential effect of changing the coordinates for a large number of other nodes.
What are ways that you could store a hierarchy of nodes that requires few changes to add or delete a node and is resilient to corruption of a few nodes? (not implementation-specific)
When you refer to corruption, are you talking about RAM or some other storage? Perhaps during transmission over some medium?
In any case, when you are dealing with data corruption you are talking about an entire field of computer science that deals with error detection and correction.
When you talk about losing a node, the first thing you have to figure out is 'how do I know I've lost a node?', that's error detection.
As for the problem of protecting data from corruption, pretty much the only way to do this is with redundancy. The amount of redundancy is determined by what limit you want to put on the degree of corruption you would like to be able to recover from. You can't really protect yourself from this with a clever structure design as you are just as likely to suffer corruption to the critical 'clever' part of your structure :)
The ever-wise wikipedia is a good place to start: Error detection and correction
I was thinking today about the best way to store a hierarchical set of nodes
So you are writing a filesystem? ;-)
The most obvious way to represent this (to me at least) would be for each node to have nextSibling and childNode pointers
Why? The sibling information is present at the parent node, so all you need is a pointer back to the parent. A doubly linked-list, so to speak.
What are ways that you could store a hierarchy of nodes that requires few changes to add or delete a node and is resilient to corruption of a few nodes?
There are actually two different questions involved here.
Is the data corrupt?
How do I fix corrupt data (aka self healing systems)?
Answers to these two questions will determine the exact nature of the solution.
Data Corruption
If your only aim is to know if your data is good or not, store a hash digest of child node information with the parent.
Self Healing Structures
Any self healing structure will need the following information:
Is there a corruption? (See above)
Where is the corruption?
Can it be healed?
Different algorithms exist to fix data with varying degree of effectiveness. The root idea is to introduce redundancy. Reconstruction depends on your degree of redundancy. Since the best guarantees are given by the most redundant systems -- you'll have to choose.
I believe there is some scope of narrowing down your question to a point where we can start discussing individual bits and pieces of the puzzle.
A simple option is to store reference to root node in every node - this way it is easy to detect orphan nodes.
Another interesting option is to store hierarchy information as a descendants (transitive closure) table. So for 1.2.3 node you'd have the following relations:
1., 1.2.3. - root node is ascendant of 1.2.3.
1.2., 1.2.3. - 1.2. node is ascendant of 1.2.3.
1., 1.2. - root node is ascendant of 1.2.
etc...
This table can be more resistant to errors as it holds some redundant info.
Goran
The typical method to store a hierarchy, is to have a ParentNode property/field in every node. For root the ParentNode is null, for all other nodes it has a value. This does mean that the tree may lose entire branches, but in memory that seems unlikely, and in a DB you can guard against that using constraints.
This approach doesn't directly support the finding of all siblings, if that is a requirement I'd add another property/field for depth, root has depth 0, all nodes below root have depth 1, and so on. All nodes with the same depth are siblings.

Resources