0
votes

I am creating Tinkergraph in gremlin.

Actually, I want to create graph of 50 nodes and they are connected via edges randomly.

I have created 50 nodes by:

(0..<50).each{graph.addVertex().property("NodeId",it)}

that creates 50 nodes.

I can't create graph properly, connecting the nodes via edges. No one node should be left.

I tried this also:

v = g.V().has("NodeId",0).next(); (0..<50).each{v.addEdge("childs",g.V().has("NodeId",it).next())}

It creates graph where all nodes are connected to the only one node.

Any ideas how to fix it?

Create Tinkergraph of 50 nodes and connected via edges.

1

1 Answers

0
votes

Your question is not very clear to me. Are you asking how to connect every vertex with each other? If so, here you go:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV().emit().repeat(property("NodeId", loops()).addV()).times(49).as("x").
......1>   sideEffect(hasNot("NodeId").property("NodeId", 49)).barrier().
......2>   V().where(neq("x")).addE("childs").from("x").iterate()

At this point you'll have a graph with 50 vertices, each one connected with a child edge to each other vertex in the graph.

gremlin> g
==>graphtraversalsource[tinkergraph[vertices:50 edges:2450], standard]

Let's pick a random vertex to verify that it's in fact connected to all all vertices in the graph except to itself.

gremlin> g.V().has("NodeId", 30).out("childs").values("NodeId").fold().order(local)
==>[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]