I'm trying to select elements <BBB> who have in their descendants at least 2 more elements of the same type at different depths.
I found it possible with:
//BBB//BBB//BBB
but if i want to change the depth of my query I have to write:
//BBB//BBB//BBB//BBB
or
//BBB//BBB
Is it possible specify the depth of my query with a parameter?
You can search for elements that have at least two/three/... ancestors of a type, e.g.:
//*[count(ancestor::BBB) >= 2]
Use:
//BBB[ancestor::BBB[$pN]]
Where $pN should be substituted with the desired number of BBB descendants at different depths.
Related
Problem
I have two expressions.
Only two operators are used: + and *. (Example: (a + b), (a * b) are valid expressions).
I'm supposed to write a program that would determine if these two expressions are equal.
Example:
Expression 1: (a + b)c = ac + b*c;
Expression 2: c*(a + b) = ca + cb = ac + bc;
Conclusion: Expression 1 and 2 are equal.
Note:
I've got the following indications:
Bring these two expressions in "sum of products" form, sort the terms of the sum, and then check if the expressions are equal.
I should use tree structure to accomplish this task:
Create Expression Tree for each of two expressions
For each Tree of the two resulted (from point 1) create another tree, that would represent "the sum of products" form of the respective expression.
What I already know
I've already written programs that create Expression Trees.
I have experience working with trees.
Question
Can you, please, suggest me an algorithm that would solve this problem ?
Thank you in advance.
You need first to search for cases where you have a multiplication node above an addition node. When you find replace the structure there so that the addition is on top. Do this until you don't find any such cases anymore.
Now your tree should look like a sum of products. Take each product and flip left and right children so that they are sorted. The exact criteria doesn't matter but you should always get the same structure for a product term like abc(note that should also compare terms to nodes, and maybe rotate terms, so that can fix (ab)c and a(bc) to the same structure). When you're done, do the same for the addition above.
Do this for both expression trees, when you're done just compare if you got the exact same tree.
I want to perform an undirected traversal to extract all ids connected through a certain type of relationship
When I perform the following query it returns the values fast enough
MATCH path=(s:Node {entry:"a"})-[:RelType*1..10]-(x:Node)
RETURN collect(distinct ID(x))
However doing
MATCH path=(s:Node {entry:"a"})-[:RelType*]-(x:Node)
RETURN collect(distinct ID(x))
takes an huge amount of time. I suspect that by using * it searches every path from s to x, but since I want only the ids these paths can be discarded. What I really want is an BFS or DFS search to find the connect nodes from s.
Both query returns the exact same result since there are no elements with shortest path higher than 5 (only in the test example !).
Did you add an index for create index on :Node(entry) ?
Also depending on the # of rels per node in your path you get rels^10 (or general rels^steps) paths through your graph that are potentially returned.
Can you try first with a smaller upper limit like 3 and work from there?
Also leaving off the direction really hurts as you then get cycles.
What you can also try to do is:
MATCH path=(s:Node {entry:"a"})-[:RelType*]->(x:Node)
RETURN ID(X)
and stream the results and do the uniqueness in the client
Or this if you don't want to do uniqueness in the client
MATCH path=(s:Node {entry:"a"})-[:RelType*]->(x:Node)
RETURN distinct ID(X)
So this is the problem, I insert in my binary search tree the following elements:
Number Name Age
1 abc 2
2 abcd 1
I can sort it by using the number since I use it as the key variable, but how can I sort it by age? I tough about creating an array and transfer all the elements and sort them inside the array but that doesn't seem right, there most be a way to do it more properly. Any tips?
What is the best approach to store the graph with unknown order of nodes in a vector. For example i have node coming in an unknown order like 35,23,89,200,12,89,569 etc... I want to store them in such a way that the memory is not wasted and the nodes are accessed efficiently if in constant time then it will be great. May be some hash function will work but if there is one that can distinguish the nodes please tell me or if there is any other approach for that.
Thanks
Simplest solution I think of is just insert them to your vector in order, and create a map<int,int> to map from their values to their indexes.
In your example:
map[35] == 0
ma[[23] == 1
map[89] == 2
map[200] == 3
map[12] == 4
...
now, access the node i as vector[map[i]]
EDIT:
A second possibility will be to use a set instead of vector to hold elements, but it might not always be desired [set has no duplicates, and will no contain elements in the order you inserted them], but consider if it suits you.
I'm having a tree structure like this:
1 ABC
1.1 DEF
1.1.2 GHI
1.2 JKL
1.2.1 MNO
2 PQR
2.1
... with no limits on the depth and length of each level. Now what happens is that I take out some of the elements all around the tree structure and in the end I want to have a proper, restructured hierarchy numbering.
How do you usually re-sort & apply proper hierarchy numbering in the least amount of work in such a case? This is a somewhat basic use case, but I'm looking for some room for improvement.
I suppose that you are keeping both number and the text in the same value variable.
The simplest thing you could possibly do is to:
Separate that into two variables: number and text
Whenever you are swapping two tree nodes (i.e. during sorting) swap just text values and keep the number values where they are.
Whenever you are adding new element as the last subelement, just use the previousLastElement.number + 1
Whenever you print out the elements number reversly append all its parents numbers separated by dot.
The only remaining complexity now is when you insert elements, where you would have to "push" the other elements' numbers after that one (but only on that one level), or when you remove elements, when you would have to pull them.
You could use a priority queue like a self-balancing tree.