In Titan graph database, using gremlin, I want all the vertices whose out-degree is greater than 10.
How to do it?
Something like this doesn't work :
g.V().has(outE().count()>10)
Help please!!
You need something like this:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().filter(outE().count().is(gt(10)))
==>v[1]
==>v[4]
Related
I would like to get a subgraph around a specific node of a a directed graph based on the out_edges or in_edges only.
# This does not work
H_tmp = nx.ego_graph(G, node_name, 2)
H_tmp.out_edges = []
H = nx.ego_graph(H_tmp, node_name, 2)
I tried using nx.ego_graph twice, but I don't know an efficient way to remove all the out_edges or in_edges. Is there a way to tell ego_graph to use only a specific set of edges?
Using the eco_graph function on an undirected graph extracts the out successors of the node. According to the doc, if you only want the predecessors (in_edges), you can apply the eco_graph function on the reverse of your graph. You'll then need to reverse your subgraph. If you want both successors and predecessors, you can specify undirected=True. See example summarizing this below:
import networkx as nx
import matplotlib.pyplot as plt
H = nx.fast_gnp_random_graph(5, 0.3,directed=True)
plt.figure(figsize=(15,10))
plt.subplot(141)
plt.title('Full graph')
nx.draw(H,with_labels=True)
plt.subplot(142)
plt.title('All neighbors around node 2')
H_all=nx.ego_graph(H, 2, 1,undirected=True)
nx.draw(H_all,with_labels=True)#plot
plt.subplot(143)
plt.title('Out subgraph around node 2')
H_out=nx.ego_graph(H, 2, 1)
nx.draw(H_out,with_labels=True) #plot
plt.subplot(144)
plt.title('In subgraph around node 2')
H_in=nx.ego_graph(H.reverse(), 2, 1) #apply eco_graph to reverse graph to get in_edges
H_in_r=H_in.reverse() #reverse subgraph
nx.draw(H_in_r,with_labels=True) #plot
After checking the documentation on triangles of networkx, I've wondered if there is a more efficient way of generating a triangle free graph than to randomly spawn graphs until a triangle free one happens to emerge, (in particular if one would like to use a constant random seed).
Below is code that spawns graphs until they are triangle free, yet with varying random seeds. For a graph of 10 nodes it already takes roughly 20 seconds.
def create_triangle_free_graph(show_graphs):
seed = 42
nr_of_nodes = 10
probability_of_creating_an_edge = 0.85
nr_of_triangles = 1 # Initialise at 1 to initiate while loop.
while nr_of_triangles > 0:
graph = nx.fast_gnp_random_graph(
nr_of_nodes, probability_of_creating_an_edge
)
triangles = nx.triangles(G).values()
nr_of_triangles = sum(triangles) / 3
print(f"nr_of_triangles={nr_of_triangles}")
return graph
Hence, I would like to ask:
Are there faster ways to generate triangle free graphs (using random seeds) in networkx?
A triangle exists in a graph iff two vertices connected by an edge share one or more neighbours. A triangle-free graph can be expanded by adding edges between nodes that share no neighbours. The empty graph is triangle-free, so there is a straightforward algorithm to create triangle-free graphs.
#!/usr/bin/env python
"""
Create a triangle free graph.
"""
import random
import networkx as nx
from itertools import combinations
def triangle_free_graph(total_nodes):
"""Construct a triangle free graph."""
nodes = range(total_nodes)
g = nx.Graph()
g.add_nodes_from(nodes)
edge_candidates = list(combinations(nodes, 2))
random.shuffle(edge_candidates)
for (u, v) in edge_candidates:
if not set(n for n in g.neighbors(u)) & set(n for n in g.neighbors(v)):
g.add_edge(u, v)
return g
g = triangle_free_graph(10)
print(nx.triangles(g))
The number of edges in the resulting graph is highly dependent on the ordering of edge_candidates. To get a graph with the desired edge density, repeat the process until a graph with equal or higher density is found (and then remove superfluous edges), or until your patience runs out.
cutoff = 0.85
max_iterations = 1e+4
iteration = 0
while nx.density(g) < cutoff:
g = triangle_free_graph(10)
iteration += 1
if iteration == max_iterations:
import warnings
warnings.warn("Maximum number of iterations reached!")
break
# TODO: remove edges until the desired density is achieved
I am learning the networkx function named "networkx.graph_edit_distance(g2,q)". Actually, GED(g2,q) = 2.If we want to tranform g2 to q, we should do at least 2 graph edit operations"substituing (1,3) whose label is '2' in g2 to (1,3) whose label is '1', inserting (3,4) which is not exsits in g2 to (3,4) whose label
is '1".My code is shown below:
nodes = [(1,{'label':'C1'}),
(2,{'label':'C2'}),
(3,{'label':'C3'}),
(4,{'label':'C4'}),
(5,{'label':'N'})]
edges = [(1,2,{'label':'1'}),
(2,4,{'label':'1'}),
(4,5,{'label':'1'}),
(5,3,{'label':'1'}),
(3,1,{'label':'2'})]
g2 = nx.Graph()
g2.add_nodes_from(nodes)
g2.add_edges_from(edges)
nodes = [(1,{'label':'C1'}),
(2,{'label':'C2'}),
(3,{'label':'C3'}),
(4,{'label':'C4'}),
(5,{'label':'N'})]
edges = [(1,2,{'label':'1'}),
(2,4,{'label':'1'}),
(4,5,{'label':'1'}),
(5,3,{'label':'1'}),
(3,1,{'label':'1'}),
(3,4,{'label':'1'})]
q = nx.Graph()
q.add_nodes_from(nodes)
q.add_edges_from(edges)
GED_q_g2 = nx.graph_edit_distance(g2, q)
But unfortunately, the expected answer is GED =2, but it gives the answer GED_q_g2 = 1.Please how could I get the right answer?
When I look at the defined graph, the edit distance of 1 is correct, because only (3,4) needs to be removed.
The graph that you've drawn displays two edges between 1 and 3, though. I guess you've misunderstood the label functionality: It's just an optional data attribute that you can use for identification or plotting - it has nothing to do with the number of edges or weights.
If you want to use multiple edges between two nodes, have a look at nx.Multigraph.
just use the edge_match.
nx.graph_edit_distance(g1,g2,edge_match=lambda a,b: a['label'] == b['label']))
I'm doing a research about map projections. I use this script:
koordinat = [0 102;...
0 103.5;...
-1 103.5;...
-1 102];
lat = koordinat(:,1);
lon = koordinat(:,2);
mstruct = defaultm('utm');
mstruct.geoid = referenceEllipsoid('wgs84','meters');
mstruct.zone = utmzone(koordinat);
mstruct = defaultm(mstruct);
[x,y] = mfwdtran (mstruct,lat,lon);
format long g
luas = polyarea(x,y)
By using the "defaultm" and "mfwdtran" function, I can calculate the projected coordinate. Buut, my fellow researcher is feeling doubtful about these 2 Matlab functions. Do you know how to see the script of these two functions?
I already opened the defaultm.m and mfwdtran.m, but I didn't see anything like an m-file from mathworks.com called "deg2utm.m".
Any suggestion will helps.
Thank you
I am using Titan(titan-1.0.0-hadoop1). Let's say the node has a property unique1. I want the nodes with this property not equal to a particular value.
The query I have is:
g.V().filter(!it.unique1 = x)
I always get the error:
no such property, "unique1"
Thanks in advance.
You should study the TinkerPop docs a bit better as this is really a very basic query.
g.V().has("unique1", neq(x))
Try this. Sample Graph:
gremlin> graph = TinkerGraph.open();
==>tinkergraph[vertices:0 edges:0]
gremlin> v1 = graph.addVertex();
==>v[0]
gremlin> v2 = graph.addVertex();
==>v[1]
gremlin> v1.property("unique1", 1);
==>vp[unique1->1]
gremlin> v2.property("unique1", 2);
==>vp[unique1->2]
Then filter your traversal like so:
// g = graph.traversal()
gremlin> graph.traversal().V().filter(values("unique1").is(1));
==>v[0]
gremlin> graph.traversal().V().filter(values("unique1").is(2));
==>v[1]
gremlin> graph.traversal().V().filter(values("unique1").is(3));
gremlin>