Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to find an edge between two vertices?

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.

like image 710
Luccas Avatar asked May 01 '13 22:05

Luccas


People also ask

How do you find the edge between two vertices?

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.

How do you find the number of edges between two vertices on a graph?

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.

How do you find the number of edges between two nodes in a tree?

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.

How do you find minimum edges?

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.


1 Answers

gremlin> g.v(1).bothE.as('x').bothV.retain([g.v(3)]).back('x')
like image 154
Huangmao Quan Avatar answered Sep 20 '22 00:09

Huangmao Quan