Using tinkerpop blueprints API, what is the best way to find if an edge exists between two vertices? I would like to avoid vertex.getEdges()
and iterate until find the right one.
E.g.: Check if v1
is friend of v2
Vertex v1 = g.addVertex(null);
Vertex v2 = g.addVertex(null);
Edge edge = g.addEdge(null, v1, v2, "friends");
Edge edge = g.addEdge(null, v1, v2, "follows");
// Node with lots of edges - Supernode - problem?
List<Edge> edges = new ArrayList<Edge>();
for(Edge edge : g.getVertex(v1.getId()).getEdges(Direction.OUT, "friends")){
if(edge.getVertex(Direction.IN).getId().equals(v2.getId()){
edges.add(edge);
}
}
Should I use Vertex Query?
Via gremlin I could do:
g.v(v1.getID()).outE("friends").inV.filter{it.id == v2.getID}
Neo4j way:
IndexHits<Relationship> relationships = relationshipIndex().get("type", edgeType, node1, node2);
Thanks for the help! I am still new to this.
In Edit Mode, click on the Vertex Select icon and right-click on the first vertex of your new edge. Holding down the Shift key, right-click on the second vertex. Hit F on the keyboard to make a new edge.
The maximum number of edges possible in a single graph with 'n' vertices is nC2 where nC2 = n(n – 1)/2. The number of simple graphs possible with 'n' vertices = 2nc2 = 2n(n-1)/2.
One simple option is to do a depth-first search starting at one of the nodes until you reach the other node. You can then look at the path that was taken from the first node to the second, which must be the unique path between those nodes, and then count how many edges are in that path.
Now, suppose during BFS, vertex x is popped from queue and we are pushing all adjacent non-visited vertices(i) back into queue at the same time we should update distance[i] = distance[x] + 1;. Finally, distance[v] gives the minimum number of edges between u and v.
gremlin> g.v(1).bothE.as('x').bothV.retain([g.v(3)]).back('x')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With