I am new to Arango and I am trying to understand the 'right' way to write some queries. I read (https://www.arangodb.com/docs/stable/graphs-traversals-using-traversal-objects.html) and (http://jsteemann.github.io/blog/2015/01/28/using-custom-visitors-in-aql-graph-traversals/), since they always popped up when searching for what I am trying to do. In particular, I have a graph where a given node has a single path (via a certain 'type' of edge) from that node to a leaf. Something like x -a-> y -a-> z. Where a is the edge type, and x,y,z are the nodes. These paths can be of arbitrary length. I would like to write an AQL query that returns the single 'longest' path from the starting node to the leaf node. I find that I always get every sub-path and would then have to do some post-processing. The traversal objects looked like they supplied a solution to this issue, but it seems they are now deprecated. Is there a correct way to do this in AQL? Is there a document that shows how to do what steemann does in his article, but only using AQL? Is there some great AQL documentation on graph queries other than what is on the arangodb site (all of which I have already read, including the graph presentation and the udemy course)? If not, I would be happy to write something to share with the community, but I am not sure yet how to do this myself, so I'd need some pointers to material that can get me started. Long, short, I'd just like to know how to run my query to find the path from node to leaf. However, I'd be happy to contribute once I see how things should be done without traversal-objects. Thank you for your help
Taking a traversal in OUTBOUND direction as example, you can do a second traversal with depth = 1 to check if you reached a leaf node (no more incoming edges).
Based on this information, the “short” paths can be filtered out.
Note that a second condition is required:
it is possible that the last node in a traversal is not a leaf node if the maximum traversal depth is exceeded.
Thus, you need to also let paths through, which contain as many edges as hops you do in the traversal (here: 5).
LET maxDepth = 5
FOR v, e, p IN 1..maxDepth OUTBOUND "verts/s" edges
LET next = (
FOR vv, ee IN OUTBOUND v edges
//FILTER ee != e // needed if traversing edges in ANY direction
LIMIT 1 // optimization, no need to check for more than a single neighbor
RETURN true // using a constant here, which is not actually used
)
FILTER LENGTH(next) == 0 || LENGTH(p.edges) == maxDepth
RETURN CONCAT_SEPARATOR(" -> ", p.vertices[*]._key)
Cross-post from https://groups.google.com/g/arangodb/c/VAo_i_1UHbo/m/ByIUTqIfBAAJ
Related
This is similar to How to trace the path in a Breadth-First Search?, but the method described in the answers in that post doesn't work for my case, it seems.
By path here, I essentially mean a sequence of connected nodes to get from a beginning to end state.
Consider an undirected graph with vertices V={a,b,c} and edges = {{a,b},{a,c}}, and assume that we must traverse the successors alphabetically. We start at node a and the end state is to visit all 3 nodes.
Breadth first search would first visit the edge a->b, and then the edge a->c. So the solution path is a->b->a->c. Since there is no edge between b & c, we must go back through a (so we must traverse the edge b->a). In the answer in the above linked post, the accepted solution would only output a->c.
I can't think of a way to modify the conventional bfs algorithm to do this. I have the same question for dfs, but I'd like to start with bfs for now.
It seems strange to want to do this. It's certainly simpler with depth-first search (DFS), which always either follows an edge or backtracks along that edge. In contrast, breadth-first search (BFS) generally does not visit (or backtrack to) a node adjacent to the previous one visited.
Specifically, this part of your question is wrong, and reveals a misconception:
Since there is no edge between b & c, we must go back through a (so we must traverse the edge b -> a).
BFS will never traverse the edge back from b to a in your example. It finishes visiting b, then polls c from the queue and visits c immediately, without "travelling" there via a.
For an analogy, it makes sense to think of DFS as tracing out a path; if you are in a maze, you could use breadcrumbs to mark places you've "visited", and therefore solve the maze by DFS. In contrast, a human cannot solve a maze by BFS because humans cannot have a queue of places they know how to get to, and "teleport" to the next place in the queue. BFS does not meaningfully trace out a path that you could follow by travelling along edges in the graph.
That said, if you really wanted to, you can construct a path visiting the nodes of the graph, such that each node is visited for the first time in the same order as BFS. The simplest way to do this would be to do a BFS to build a list of the nodes in "BFS order", while also building the "BFS tree". Then for each node in BFS order, you can get to it from the previous node going via their lowest common ancestor in the BFS tree. This path only goes via nodes that have already been visited earlier in BFS order.
I'm just getting started with arangodb and have gotten to my first real problem:
Is it possible to search for nodes connected to all of multiple others? This seems like a basic operation for a graph db but I just can't think of a solution.
For reference, if we take the 'knows' example graph I want to know which persons know Charlie AND Dave (which should be only Bob)
knows example graph (not allowed to embed images yet)
For now my best guess is to start a traversal for all of the "targets" and reduce and filter the response myself, is this really the only way?
EDIT:
OK, to further specify I have added another connection, eve knows dave too, but should NOT be returned since she does not know charlie
EDIT2:
So far I've come up with this query
FOR start IN ['persons/charlie', 'persons/dave']
LET knownBy = (FOR v,e,p IN 1 INBOUND start knows
RETURN v)
FOR p IN knownBy
COLLECT person = p
LET knows = (FOR v IN 1 OUTBOUND person._id knows
RETURN v._id)
FILTER knows ALL IN ['persons/charlie', 'persons/dave']
RETURN person
However, this feels a bit unnatural, getting the persons known by 'X' to get the persons that know 'X'... Also, the profiler shows that about a third of the time is used for optimizing the plan, there has to be a better solution, right?
If we take the knows example and only search for two connected vertices you can start a traversal on one target with a depth of 2 and filter that the third vertex on the path has to be the second target, then you can just return the second vertex on the path.
FOR v, e, p IN 2 ANY 'persons/charlie' knows
FILTER p.vertices[2]._id == 'persons/dave'
RETURN p.vertices[1]
If you search for more than two vertices, the following query should work well. It starts a traversal with a depth of 1 and collect every _id of the person neighbors and checks of your targets are all containing in the neighbors.
LET targets = ['persons/charlie','persons/dave']
FOR person IN persons
FILTER targets ALL IN FLATTEN((
FOR v, e, p IN 1 ANY person._id knows
RETURN v._id
))
RETURN person
I'm looking for the right keywords/nomenclature for the following problem, since I cannot find anything on google to this topic:
I have a graph where each edge and each node is assigned to a certain class/color or whatever you call it. Now I want to find a path between a start and a goal node, having some constraints on the path. For example I'd like to have as less "blue" nodes on the path as possible, or max. 2 "red" edges, or a combination of those things. Of course there are also the usual edge costs, which have to be minimized in addition to the fixed path constraints.
How is this kind of problem called, or what do I have to search for?
Best regards
Mark
I do not think that a name for such a general problem exists. However, I'm pretty certain you can re-model your graph and solve this problem via a simple Dijkstra search:
Trying to avoid certain (type of) vertex: Say you have a vertex that is to be avoided, and that has k neighbors. Replace it by a K_k (i.e. a clique with k vertices), and connect each neighbor to one of the k new vertices. Then set the weight of all the clique-edges to something large. Now every path passing over the original vertex will have to pass through the clique and "pay the fee", i.e. it will be avoided, if possible
Trying to avoid certain edges: Just raise their edge weight accordingly
Then, run a simple Dijkstra search. If you have multiple classes that are to be avoided, you can even set the weights as to determine priorities for avoiding each of them..
Hope that helps,
Lukas
Take US cities for example and say I want the traversal of all cities and roads that go through NYC, Chicago and Seattle.
This can be done with TRAVERSAL AQL function (using filterVertices). However this function only takes the ID and not the vertex example as in GRAPH_TRAVERSAL.
The GRAPH_TRAVERSAL doesn't have a filter option, so my question is there a way to filter the results using graph operations?
the feature is actually there but was somehow not documented. I added it to our documentation which should be updated soon. Sorry for the inconvenience.
filterVertices takes a list of vertex examples.
Note: you can also give the name of a custom AQL function. with signature function(config, vertex, path). For more specific filtering.
vertexFilterMethod defines what should be done with all other vertices:
"prune" will not follow edges attached to these vertices. (Used here)
"exclude" will not include this specific vertex.
["prune", "exclude"] both of the above. (default)
An example query for your question is the following (airway is my graph):
FOR x in GRAPH_TRAVERSAL("airway", "a/SFO", "outbound", {filterVertices: [{_key: "SFO"}, {_key: "NYC"}, {name: "Chicago"}, {name: "Seattle"}], vertexFilterMethod: "prune"}) RETURN x
Hint: Make sure you include the start vertex in the filter as well. Otherwise it will always return with an empty array (the first visited vertex is directly pruned)
So I need some help brainstorming, from a theoretical standpoint. Right now I have some code that just draws some objects. The objects lie in the leaves of a quadtree. Now as the objects move I want to keep them placed in the correct leaf of the quadtree.
Right now I am just reconstructing the quadtree on the objects after I change their position. I was trying to figure out a way to correct the tree without rebuilding it completely. All I can think of is having a bunch of pointers to adjacent leaf nodes.
Does anyone have an idea of how to figure out the node into which an object moves without just having a ton of pointers everywhere or a link to articles on this? All I could find was different ways to build the quadtree, nothing about updating it.
If I understand your question. You want some way of mapping between spatial coordinates and leaves on the quadtree.
Here's one possible solution I've been looking at:
For simplicity, let's do the 1D case first. And lets assume we have 32 gridpoints in x. Every grid point then corresponds to some leaf on a quadtree of depth five. (depth 0 = the whole grid, depth 1 = 2 points, depth 2 = 4 points... depth 5 = 32 points).
Each leaf could be represented by the branch indices leading to the leaf. At each level there are two branches we can label A and B. So, a particular leaf might be labeled BBAAB, which would mean, go down the B branch, then the B branch, then the A branch, then the B branch and then the B branch.
So, how do you map e.g. BBABB to an x grid point between 0..31? Just convert it to binary, so that BBABB->11011 = 27. Thus, the mapping from gridpoint to leaf-node is simply a matter of translating the letters A and B into 0s and 1s and then interpreting the result as a binary number.
For the 2D case, it's only slightly more complicated. Now we have four branches from each node, so we can label each branch path using a four-letter alphabet, e.g. starting from the root and taking the 3rd branch and then the fourth branch and then the first branch and then the second branch and then the second branch again we would generate the string CDABB.
Now to convert the string (e.g. 'CDABB') into a pair of gridvalues (x,y).
Let's assume A is lower-left, B is lower right, C is upper left and D is upper right. Then, symbolically, we could write, A.x=0, A.y=0 / B.x=1, B.y=0 / C.x=0, C.y=1 / D.x=1, D.y=1.
Taking the example CDABB, we first look at its x values (CDABB).x = (01011), which gives us the x grid point. And similarly for y.
Finally, if you want to find out e.g. the node immediately to the right of CDABB, then simply convert it to a pair of binary numbers in x and y, add +1 to the x value and convert the new pair of binary numbers back into a string.
I'm sure this has all been discovered, but I haven't yet found this information on the web.
If you have the spatial data necessary to insert an element into the quad-tree in the first place (ex: its point or rectangle), then you have the same data needed to remove it.
An easy way is before you move an element, remove it from the quad-tree using the same data you used to originally insert it, then move it, then re-insert.
Removal from the quad-tree can first remove the element from the leaf node(s), then if the leaf nodes become empty, remove them from their parents. If the parents become empty, remove them from their parents, and so forth.
This simple method is efficient enough for a complex world of objects moving every frame as long as you implement the quad-tree efficiently (ex: use a free list for the nodes). There shouldn't have to be a heap allocation on a per-node basis to insert it, nor a heap deallocation involved in removing every single node. Most node allocations/deallocations should be a simple constant-time operation just involving, say, the manipulation of a couple of integers or pointers.
You can also make this a little more complex if you like. You can start off storing the previous position of an object and then move it. If the new position occupies nodes other than the previous position, then remove the object from the nodes it no longer occupies and insert it to the new ones. Otherwise just keep it in the same node(s).
Update
I usually try to avoid linking my previous answers, but in this case I ended up doing a pretty comprehensive write up on the topic which would be hard to replicate anywhere else. Here it is: https://stackoverflow.com/a/48330314/4842163