What next() means in TinkerPop - titan

I'm currently reading the TinkerPop3 Documentation
What I'm confused is that I can't find any explanation about next().
For example, w/ next() or w/o next() returns same vertext
gremlin> g.V().has('name', 'marko')
==>v[1]
gremlin> g.V().has('name', 'marko').next()
==>v[1]
but, the class names are different from each other.
gremlin> g.V().has('name', 'marko').getClass()
==>class org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal
gremlin> g.V().has('name', 'marko').next().getClass()
==>class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex
Without 'next()' an assigned variable has no value.
gremlin> marko = g.V().has('name', 'marko')
==>v[1]
gremlin> marko
Even, with clockWithResult() the outputs are completely different.
gremlin> clockWithResult(1){g.V().both().barrier().both().barrier().both().barrier().count().next()}
==>1.079524
==>72
gremlin> clockWithResult(1){g.V().both().barrier().both().barrier().both().barrier().count()}
==>0.11863599999999999
==>[GraphStep([],vertex), VertexStep(BOTH,vertex), NoOpBarrierStep(2147483647), VertexStep(BOTH,vertex), NoOpBarrierStep(2147483647), VertexStep(BOTH,vertex), NoOpBarrierStep(2147483647), CountGlobalStep]
or this example:
gremlin> g.V(1).out('knows').values('name').fold()
==>[vadas, josh]
gremlin> g.V(1).out('knows').values('name').fold().next()
==>vadas
==>josh
In the manual, there are many other examples which make me confusing.
I hope marko and his friends would help me.

The short answer is that the Gremlin Console iterates results for you automatically.
x = g.V().has('name', 'marko')
In the above example, x will be a Traversal instance, which is a type of Iterator. When the console encounters an Iterator it automatically unrolls it so that you can see the results. In this case:
x = g.V().has('name', 'marko').next()
the addition of next() just says that you want to call Iterator.next() - in other words you want to get the first item out of the Iterator. So, in the above case, x will be a Vertex.
For this case:
gremlin> marko = g.V().has('name', 'marko')
==>v[1]
gremlin> marko
you now know that marko is an Iterator, so when you evaluate it on again, the console tries to iterate it. Of course, the console already iterated it on the previous line, so when it attempts to do so again, there is nothing additional to iterate. Here's an example that makes it more apparent as to what is going on:
gremlin> x = g.V();null
==>null
gremlin> x.next()
==>v[1]
gremlin> x.next()
==>v[2]
gremlin> x
==>v[3]
==>v[4]
==>v[5]
==>v[6]
Note the use of ;null on the first line which prevents the console from iterating x. Why? because my script returns null not the x.
It should now be clear as to what your example with clock is doing...the first line which calls next() is measuring the execution of the traversal and the second is measuring the execution of the Traversal construction. Why do you need to call next() in this case? Because the Traversal is inside of a closure - remember, the Console only iterates the return value of the function not every Iterator in your scripts.
Finally:
gremlin> g.V(1).out('knows').values('name').fold()
==>[vadas, josh]
gremlin> g.V(1).out('knows').values('name').fold().next()
==>vadas
==>josh
hopefully everything else I've discussed above allows you to see why the next() produces this behavior, but just in case, here's what the console is doing essentially:
gremlin> x = g.V(1).out('knows').values('name').fold();null
==>null
gremlin> y = x.next();null
==>null
gremlin> y
==>vadas
==>josh

Related

GREMLIN for Scala : How to drop edge between two vertex and connect edges between two vertex in single query

I am using this library for gremlin scala by https://github.com/mpollmeier/gremlin-scala#getting-started.
Here is a use case there is three vertex(A,B,C). Two A,B vertex is already connected with edges name "IS".
Now I want to drop "IS" edge between A,B and connect A to C with edge named "IS" in single gremlin scala query. Earlier I wrote two different gremlin scala query one is for dropping edge and other is for adding edge but I want to combine these two queries in one.
Assuming that all of the following are supported by that library, you should be able to run the following which will drop a relationship between V(1) and V(2) while adding an edge between V(1) and V(6).
Leaned upon a related question in order to formulate this, which can be found here
For your case: A=1, B=2, C=6
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.E()
==>e[7][1-knows->2]
==>e[8][1-knows->4]
==>e[9][1-created->3]
==>e[10][4-created->5]
==>e[11][4-created->3]
==>e[12][6-created->3]
gremlin> g.V(1).as('A').bothE().where(otherV().hasId(2)).as('drop').addE('IS').from('A').to(V(6)).select('drop').drop()
gremlin> g.E()
==>e[8][1-knows->4]
==>e[9][1-created->3]
==>e[10][4-created->5]
==>e[11][4-created->3]
==>e[12][6-created->3]
==>e[13][1-IS->6]

Get nodes from a graph condensation

I have an adjacency matrix adj and a cellarray nodeManes that contains names that will be given to the graph G that will be constructed from adj.
So I use G = digraph(adj,nodeNames); and I get the following graph :
Now, I want to find the strongly connected components in G and do a graph condensation so I use the following:
C = condensation(G);
p2 = plot(C);
and get this results :
So I have 6 strongly connected components, but my problem is that I lost the node names, I want to get something like:
Is that any way to get the nodes names in the result of the condentation?
I think the official documentation can take you to the right point:
Output Arguments
C - Condensation Graph
Condensation graph, returned as a digraph object. C is a directed
acyclic graph (DAG), and is topologically sorted. The node numbers in
C correspond to the bin numbers returned by conncomp.
Let's take a loot at conncomp:
conncomp(G) returns the connected components of graph G as bins. The
bin numbers indicate which component each node in the graph belongs to
Look at the examples... I think that if you use conncomp on your graph before using the condensation function, you will be able to rebuild your node names on your new graph with a little effort.

Understanding the chi2gof function in matlab

I'm trying to understand how to use the chi2gof function in matlab with a very simple test. Let's assume that I toss a coin 190 times and get 94 heads and 96 tails. The null hypothesis should be that i get 95h, 95t. As far as I understand the documentation, I should be able to test the hypothesis by running
[h,p,stats] = chi2gof([94,96], 'expected', [95,95])
However, this returns h = 1, which supposedly means that null hypothesis is rejected, which makes no sense. Another pecular thing is that the O parameter in stats returns as O: [0 2] - but shouldn't this be my input ([94,96])? What am I doing wrong?
What am I doing wrong?
The problem is that you are passing the cumulative outcome of your coin tosses to chi2gof. The goodness-of-fit test must be performed on the full sample. From the official documentation (reference here):
x = sample data for the hypothesis test, specified as a vector (the wrong part of your code)
Expected = expected counts for each bin (the correct part of your code)
Let's make an example using the correct variables:
ct = randsample([0 1],190,true,[0.49 0.51]);
[h,p,stats] = chi2gof(ct,'Expected',[95 95]);
The returned value of h is 0, which is absolutely correct.
Now, let's make an example that is supposed to fail:
ct = randsample([0 1],190,true,[0.05 0.95]);
[h,p,stats] = chi2gof(ct,'Expected',[95 95]);
As you can see, h returned from this second test will be equal to 1.
On a final note, don't forget to take a look at the second output argument, which is the p-value of the test and is an important element to evaluate the significance of the result.

What does a function with this ~ mean? (e.g. function=f(~, x, y))

I am doing another coursera assignemnt, this time with aerial robotics. I have to program a pd controller using the matlab ode45 (ordinary diff. equation). And the file that has to contain this code gets called as follows:
pd_controller(~, s, s_des, params)
I searched around but couldn't find anthing that explain this to me and how it works.
In the main program the function is called with a time variable which I would need for my ODE:
controlhandle(t, s, s_des, params)
Where this controlhandle is the functionhandler for pd_controller.
So, what does this mean? And can I access whatever is behind ~?
Besides:
I found one example, but the other around. A function, let's call it function = f(a,b) was called with f(~, b) where a and b has been declared inside the function.
The symbol is called a tilde, and it signifies that you are ignoring that input argument.
See the documentation here: https://mathworks.com/help/matlab/matlab_prog/ignore-function-inputs.html
In your case, the function controlhandle will not be passed a t variable, and probably has (should have) some check for this and perhaps a default t if none is given.
This works the same with output arguments, for example if you want the index of a max in an array, but not the max itself, you would use
a = [pi, 3.6, 1];
[~, idx] = max(a); % idx = 2, we don't know what the max value is
It means you don't need pass this parameter in this function call. Also, you can use it in the output of some functions too. For example:
A = [1 4 2 2 41];
[~, B] = sort(A);
this means you don't need the second output, and you can ignore that.
In your case, when no value sent for the first parameter t, probably the function acts on a default value for t in his computation.
Also, you can find more about that in matlab documentation.
I should have mentioned that this post exists as an answer, but it might be here instead.

How to filter out a list of particular nodes using Gremlin

I am using Titan and Gremlin 3.0.1
The graph only has one type of node which is "nodeA" and has five edge labels, namely "relation1", "relation2", etc.
Now I want to find the nodes which do not have "relation1" or "relation2" edges. Below is the query I am using:
g.V().except(g.V().in('relation1', 'relation2'))
This gives the error: "the wrong type of argument for except"
Any help will be appreciated.
except is not a step in TinkerPop 3. All you need is the not step:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().not(bothE("created"))
==>v[2]
gremlin> g.V().not(outE("created"))
==>v[2]
==>v[3]
==>v[5]
gremlin> g.V().not(inE("created"))
==>v[1]
==>v[2]
==>v[4]
==>v[6]