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

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]

Related

Networkx create graph from adjacency matrix without edge weights

When I call G = nx.convert_matrix.from_numpy_array(A, create_using=nx.DiGraph), where A is a 0-1 adjacency matrix, the resulting graph automatically contains edge weights of 1.0 for each edge. How can I prevent this attribute from being added?
I realize I can write
for _,_,d in G.edges(data=True):
d.clear()
but I would prefer if the attributes were not added in the first place.
There is no way to do that with native networkx functions. This is how you can do it:
G = nx.empty_graph(0, nx.DiGraph)
G.add_nodes_from(range(A.shape[0]))
G.add_edges_from(((int(e[0]), int(e[1])) for e in zip(*A.nonzero())))
This is exactly how the nx.convert_matrix.from_numpy_array function is implemented internally. I got however rid of all controls, so be careful with this. Additional details can be found here

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.

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]

What next() means in TinkerPop

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

How to modify vertex data when calling the mapTriplets method in Graphx of Spark

The mapTriplets operation in Graphx of Spark can transform the triplets into some other form as the definition describes:
def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2]
My data is a sparse bipartite graph, and the vertices data of an edge will be updated during each iteration. For example, here is an edge (srcAttr, dstAttr, attr), the vertex of srcAttr and dstAttr will be modified according to attr. Therefore, what I need is to get all (srcAttr, dstAttr, attr) combinations, and use attr to update the vertices.
Graphx provides the mapTriplets method which can transform all (srcAttr, dstAttr, attr) combinations, but I cannot figure out how to modify vertex when executing this method.
So, is there any strategy that can modify the vertices when traversing all edges?
I cannot figure out how to modify vertex when executing this method
Because it is simply not possible. First of all GraphX data structures, same as other distributed data structures in Spark, are immutable. Moreover mapTriplets is designed to transforms edges not vertices.
is there any strategy that can modify the vertices when traversing all edges?
If you want to transform vertices using edge data then aggregateMessages should give you what you want. It takes two functions
one from EdgeContext to Unit, which can be used to send messages to the source and/or destination nodes
second one which reduces messages for each vertex
and returns a VertexRDD which can be further used to construct a new graph.