How to filter out a list of particular nodes using Gremlin - titan

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]

Related

OSMNX/networkx projection disconnects nodes

I would like to create a map of isolines for a given country.
I fetched road map using OSMNX:
cf = '["highway"~"motorway|motorway_link|trunk|primary|secondary"]'
G = ox.graph_from_place('Venezuela', network_type='drive', custom_filter=cf)
# project the graph to UTM
G_projected = ox.project_graph(G)
Then I created a subgraph of all nodes within given radius from starting point (lat, long):
node = ox.get_nearest_node(G, (8.284904228184445, -62.72239713960212))
subgraph = nx.ego_graph(G_projected, node, radius=10000, distance="length")
Resulting subgraph contains only 1 node (no matter what radius is used)
list(subgraph.nodes())
[5098062996]
List of neighbours is empty:
list(G_projected.neighbors(5098062996))
But for graph G (before projection was conducted):
list(G.neighbors(5098062996))
[5098062995]
The CRS of the starting point is EPSG:4326, WGS 84, project_graph projects it to UTM zone in which the graph’s centroid lies. But what is the resulting CRS, I guess that in this case, it is also WGS 84 because there is no difference in terms of geometry between results for G and G_projected. Is it necessary to project G to UTM each time, and what about the disconnected nodes, is it the result of projection operation?
Following methods turned out to be not very helpful in identifying the problem:
list(nx.isolates(G_projected))
list(nx.isolates(G))
Both return empty list. Which mean that every node has neighbours (is connected?)
Given node is also in the list of connected components of a graph:
5098062996 in list(nx.connected_components(G_projected.to_undirected()))[0]
True
We have contradicted information, the node is connected but has no neighbours and resulting subgraph contains only the starting point.
I have to emphasise that for different nodes everything work fine, I encountered issues only with that particular node. For G without projection it works. I only used the projection because I saw it in notebook examples at OSMNX github page.
I should simply do not use the projection or I should use it and there is some other issue with the graph connectivity?
Any advice would be appreciated.
I cannot reproduce your exact query because getting the graph for all of Venezuela requires over 700 downloads. But I can reproduce your area of interest around that point, and everything appears to be working correctly:
import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)
pt = 8.28490, -62.72240
cf = '["highway"~"motorway|motorway_link|trunk|primary|secondary"]'
G = ox.graph_from_point(pt, dist=5000, network_type='drive', custom_filter=cf)
G_projected = ox.project_graph(G)
node = ox.get_nearest_node(G, pt)
print(list(G.neighbors(node))) #[]
print(list(G_projected.neighbors(node))) #[]
print(list(G.predecessors(node))) #[5098062995]
print(list(G_projected.predecessors(node))) #[5098062995]
x = G.nodes[node]['x']
y = G.nodes[node]['y']
bbox = ox.utils_geo.bbox_from_point((y, x), dist=200, project_utm=True)
nc = ['r' if n==node else 'w' for n in G.nodes()]
fig, ax = ox.plot_graph(G_projected, node_color=nc, node_size=50, bbox=bbox)
The node in question is this one, which given the network types in your custom filter, has no successor nodes in your graph. For a MultiDiGraph, neighbors is equivalent to successors and thus in both the unprojected and projected graphs, you get an empty list (and an empty subgraph when induced on this node). This node does however have one predecessor in your graph, which you can see printed in the code snippet using the predecessors method.

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.

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

creating a graph on matlab - using the graph function

I am trying to create a graph in matlab, not a plot of some function but an actual graph with vertex and edges, the thing is that it won't let me create a graph according to the examples.
what am I doing wrong? is there any library I need to install or something?
when I try this code:
G1 := Graph([1,a,3], [[1,a],[1,3]]):
Graph::printGraphInformation(G1)
its from the documentation, it gives me this error:
Undefined function 'G1' for input arguments of type 'char'.