If I have some XML structure like this
A
B C
D E F G
So A has two children nodes B and C, and B has two children nodes D and E, and C has two children nodes F and G. How can I write an xpath to select E and G? I'm looking for a way to union two selectors something like /A/(B/E)|(C/G).
I was trying something like
/A/B/E + /A/C/G
but that has syntax errors...
I have built an XML input from your scenario above, as follows:
<A>
<B>
<D>DDD</D>
<E>EEE</E>
</B>
<C>
<F>FFF</F>
<G>GGG</G>
</C>
</A>
if you run this query:
/A[B]//E|/A[C]//G
it outputs:
<E>EEE</E>
<G>GGG</G>
Related
I wanted to ask about Fibonacci heaps.
If I have this scenario:
A
|
B
Then, we add two more nodes C and D:
A
|\
B C
|
D
Now we delete B:
A
|
C
|
D
Now we add E and F.
I saw it creates a tree like that:
E
|\
F A
|
C
|
D
But I don't understand why E and F are connected with the tree. From what I read, we connect trees with the same rank (for example, a tree of one node with another tree of one node), am I wrong?
Thank you very much.
If you add the nodes C and D into the first Fibonacci heap, you would not end up with the tree you drew. Instead, you'd have this heap:
A C D
|
B
Remember that Fibonacci heaps lazily add each newly added value to the top-level list of trees and only coalesce things on a deletion. If you were to delete B, you'd promote it to the top level, like this:
A B C D
You'd then remove B:
A C D
Now, you'd scan the root list and coalesce the trees of the same order together. Let's suppose you scan the nodes in the order A, C, D. First, you'll coalesce A and C together, like this:
A D
|
C
At this point, no further coalesces will happen, since there's exactly one tree of each order.
If you then add in E and F, you'd put them at the top level, like this:
A D E F
|
C
So yes, you're right, those nodes shouldn't get added into the tree.
Given the string
{A} AND ({B} OR {C} OR ({D} AND {E}))
How can this be represented in a data structure?
The "natural" representation is a tree. Each non-leaf node is an operator and its children are the operands. In your example, you'd have a root node labeled "AND" with two children. The left child is {A} and the right one is labeled "OR". This "OR" has three children, one labeled {B}, one labeled {C} and one labeled "AND". This last "AND" has two children, {D} and {E}.
If you wish "OR" to be a binary operator (rather than ternary or n-ary), you can have the "OR" with two children: one is {B} and the other is another "OR" whose children are {C} and the "AND".
It's a circuit. If I were to wire up that equation on a breadboard with some transistors and LED's I'd draw the following schematic:
A
/
output-----[AND] B
\ /
[OR]--C
\ D
\ /
[AND]
\
E
Coincidentally, this can even be structured as a simple map of arrays of maps. Let's use JSON as an example format for this data:
{AND:[
A,
{OR:[
B,
C,
{AND:[
D,
E
]}
]}
]}
This is basically a graph. In the simplest case like in this example it is a tree but be warned: that is only an artefact of this example. In general it is a graph because you can have cycles in the structure (one element appearing in more than one place).
Here's an example of an equation that is not a neat tree:
{A} OR ({B} AND (NOT {A}))
This translates to:
A-------------.
/ |
output----[OR] [NOT]<--'
\ /
[AND]
\
B
I have four sets:
A={a,b,c}, B={d,e}, C={c,d}, D={a,b,c,e}
I want to search the sequence of sets that give me: a b c d
Example: the sequence A A A C can give me a b c d because "a" is an element of A, "b" is an element of A, "c" is an element of A and "d" is an element of C.
The same thing for : D A C B, etc.
I want an algorithm to enumerate all sequences possibles or a mathematical method to find the sequences.
You should really come up with some code of your own and then ask specific questions about problems with it. But it's interesting, so I'll share some thoughts.
You want a b c d.
a can come from A, D
b can come from A, D
c can come from A, C, D
d can come from B, C
So the problem reduces to finding all of the 2*2*3*2=24 ways to combine those options.
One way is recursion with backtracking. Build it from left to right, output when you have a complete set. Like the 8 queens problem, but much simpler since everything is independent.
Another way is to count the integers and map them into a mixed-base system. First digit base 2, then 2, 3, 2. So 0 becomes AAAB, 1 is AAAC, 2 is AACB, etc. 23 is DDDC and 24 needs five digits so you stop there.
Having some issues with a Prolog question:
The following clauses represent a directed graph, where nodes are
atoms, and edges are denoted by the connected predicate. Given that
the following clauses are in the database, answer the two questions
below.
connected(a,b).
connected(b,d).
connected(b,e).
connected(b,f).
connected(a,c).
connected(c,f).
connected(c,g).
connected(h,c).
path(X,Y) :- connected(X,Y).
path(X,Z) :- connected(X,Y), path(Y,Z).
Show the Prolog query that returns all nodes having two or more
different incoming edges (i.e., at least two different nodes are
connected to it). Also, show the result of entering the query (asking
for every solution). Your query may return the same node multiple
times, and may printout the values of other variables in the query.
The variable denoting the node in question should be called DNode.
So far I have:
path(DNode,__) , path(__,DNode).
But that only give me b and c
I think the letters with more than one nodes are a, b, c, f.
I tried this to get a, b and c:
path(__,DNode),path(DNode,__) ; path(DNode,__) , path(DNode,__).
But I got a, b, c and h.
I am assuming I'll have to like this to get all the letters I want:
path(__,DNode),path(DNode,__) ; path(DNode,__) , path(DNode,__) ; path(__,DNode) , path(__,DNode).
It gives me a, b, c, e, f, g and h though.
Any advice about how to get the 4 letters I want would be appreciated.
if you display your graph, perhaps with Graphviz
?- writeln('digraph G {'), forall(connected(A,B), writeln(A->B)), writeln('}').
you can see that only [c,f] have 2 incoming edges, and that you don't need path/2. It's sufficient a join on the second argument of connected/2, with a test that the first arguments are different (operator (\=)/2).
I have seen some similar questions here, but the answers dont solve my problem.
I want to draw a graph. I write some code like this:
digraph {
{rank = same a b c d e f }
a -> b -> c -> d -> e -> f
a -> f
b -> d -> f
b -> f
}
but the result is that some of the edges overlapped each other.
So my question is how can I fix the edge to make it not overlap
and I also wanna know how can I give the node a fixed position? There is no problem this graph. But some times when I wanna a graph with a sequence of
a b c d e f
but when i create some edges and the sequence will change like:
a->e b c d f
You can use the attribute pos of a node or edge to specify coordinates. To see where dot places your nodes and edges you can simply run dot myinputfile.dot without any output parameter. This will produce the dot file with added coordinates (among other additions).
Based on this you can force dot to place some or all nodes at certain coordinates.