How to receive the current depth in RecursiveASTVisitor (clang)? - c++

I try to understand several weeks the principles of operation with clang
AST, but meanwhile I did not answer the main issue: how to walk on this
tree?
I read all guides which I found, studied doxygen documentation and even
watched couple of lectures on YouTube, however the understanding did not
come.
That I understood:
1) AST of a tree has no general type of nodes
2) For movement on a tree it is offered to use either RecursiveASTVisitor,
or matcher. The first recursively realizes bypass in depth, and the second
allows to look for those nodes which are interesting.
The problem is in what part of a tree I am in what in one of two options I
cannot learn. I do not know how to define at what moment my visitor passed
to other branch and at what moment continues to move to depth.
Ideally it would be desirable to know depth of the node visited by me. It is
possible?
I very much like a dump() function output because in it communications
between nodes are accurately shown. However how to receive it in pure form
(but not as the text) I do not know.
Generally, the question is as follows: whether I can construct the tree on
the basis of AST, but with uniform type of nodes and how to make it?

Related

Depth first search Open and Closed list

I am really stuck at the moment and I am going insane.
In the simplest of terms, when do you stop on Open and Closed lists with a depth first search?
Do you open and close every node until there are no nodes left?
Please help because I am going doolally here
Thank you
Open list helps you in both depth first and breadth first searches to traverse your tree properly. Think about algorithms step by step. You are in a node with many children and your are going to expand one of them. After expansion there should be a mechanism to get back and continue your traversal. Open list performs that for you and tells you what is actually the next node to be expand. And the algorithm only clarify the order of child insertion into the list.
And Closed list generally improves the speed of algorithm. It prevents the algorithm from expanding pre-visited nodes. Maybe you reach node A that was expanded previously through another branch. This will let you cut this branch and try another path.
Heuristics are useful to get away from dead-end. In AI algorithms usually you are facing problems that they have many waste branches. By passing through each step you can add the path cost to a variable and when you want to add expanded nodes to your open list, considering it will help you never go through them. Otherwise, you will get into a trap and algorithm hangs.
Let me explain more with an example:
Consider the game 15-puzzles. You are going to solve it through an algorithm and you have to check all possible ways. (actually you are going to make a tree). When you move a tile in a direction that would be possible in your tree to move that in reverse direction in the next level, right? So you will never get out of such dead-ends and your algorithm hangs.
This was explanation of Open and Closed lists. You asked about when the algorithm finishes. Actually you will repeat the expand and add to Open list until you find your goal or Open list goes empty.

Testing important implementation details

I'm implementing a key -> value associative container. Internally it's a sorted binary tree, and I want to make sure it's balanced so that find operations are sure to be Olog(n). The problem is that this is an implementation detail that is entirely private to the class, and I can't readily measure it from outside.
The best I can think to do is to benchmark my find operations - if they operate in linear time it's probably because the tree is unbalanced - but that seems far too inexact, and I'd feel better if I had a more direct way to measure.
What design/testing patterns are out there that might be helpful in these sorts of situations?
You could extract the balanced tree to it's own class, and test that class. In that class the balanced-ness is a feature of the class and could expose something like depth which would let you inspect it and assert that the tree remains balanced.
You are correct in saying that you are testing an implementation detail. The problem here is that a bad implementation also produces the correct output, it just takes longer. This means that the only measurable unit is time.
What I would do is similar to what you propose: create a big collection of data and structure it in a way that a good implementation should be able to find what you're looking for in a matter of moments and a bad implementation has to go through your entire collection before finding it.
This could translate to having thousands of elements and searching for the element that's last in line. You could structure it in a way that a good implementation should find it at the top of the tree and thus find it very quickly while a bad implementation should find it somewhere at the bottom, thus taking time to find it.
Many frameworks have an option to specify a timeout so if you set this to a low enough value and you have plenty of data in your collection, you can weed out slow-running implementations like that.

passing AABB tree: serialization of AABB tree?

I'm trying to parallelize my code, and I'm using a CGAL's AABB tree for interference detection (pretty neat efficiency btw :) ).
No problem on a single machine (I'm not saying it's multi-threaded, but that's another story), but I now want to do several analyses at once, and I'm going to use MPI to spawn my software onto several computers. The obstacle(s) do not change in the different analyses, my AABB tree is thus identical for all children.
To minimize the overhead, I'd like to avoid re-reading and re-building my tree, and actually, not even a write file/read file operation.
Through the MPI function's SPAWN, I can give an array of strings to the child, and I'd like to pass the AABB tree along with the other global variables as arguments to skip the reading file part of the overhead.
1st question: That would mean passing something like 1.5MB+ in argv, anything wrong with that?
2nd question: How do I pass the tree? I read something interesting in this thread but it's old, and there is no follow up. Is there anyone who did the serialization in the meantime? Is there any new instruction to do it? Else I'll try to do such a function, at least in my case (ie, Simple_cartesian kernel, Triangle_3 primitives). Any help welcomed :)
Unfortunately, the answer in the thread you quoted is still valid: CGAL does not offer anything to help the serialization of an AABB Tree. What type of primitive do you have in your AABB Tree?

What algorithm opencv GCGRAPH (max flow) is based on?

opencv has an implementation of max-flow algorithm (class GCGRAPH in file gcgraph.hpp). It's available here.
Does anyone know which particular max-flow algorithm is implemented by this class?
I am not 100% confident about this, but I believe that the algorithm is based on this research paper describing max-flow algorithms for computer vision. Specifically, Section 3 describes a new algorithm for computing maximum flows.
I haven't lined up every detail of the paper's algorithm with the implementation of the algorithm, but many details seem to match:
The algorithm described works by using a bidirectional search from both s and t, which the implementation is doing as well: for example, there's a comment reading // grow S & T search trees, find an edge connecting them.
The algorithm described keeps track of a set of orphaned nodes, which the variable std::vector<Vtx*> orphans seems to track in the implementation.
The algorithm described works by building up a set of trees and reusing them; the algorithm implementation keeps track of a tree associated with each node.
I hope this helps!

dependency sort with detection of cyclic dependencies

Before you start throwing links to wikipedia and blogs in my face, please hear me out.
I'm trying to find the optimal algorithm/function to do a dependency sort on... stuff. Each item has a list of its dependencies.
I would like to have something iterator-based, but that's not very important.
What is important is that the algorithm points out exactly which items are part of the dependency cycle. I'd like to give detailed error information in this case.
Practically, I'm thinking of subclassing my items from a "dependency node" class, which has the necessary booleans/functions to get the job done. Cool (but descriptive) names are welcome :)
It's normally called a topological sort. Most books/papers/whatever that cover topological sorting will also cover cycle detection as a matter of course.
I don't exactly get why is it so hard to find the dependecy cycle if there is any! you just have to check if there is any node you already passed over while appling bfs algorithm to find out all the dependecies. if there is one you just roll back the way you came down to revisit a node alll the way up and mark all the nodes until you reach the first visit at the specified node. all the ones in your pass will be marked as a cycle. (just leave a comment and i'll give a code to do that if you need)