skip to Main Content

When I create a single directed edge, two edges are getting created.

I have a graph named, staff_details. I created two vertices.

First Vertex:

SELECT *
FROM cypher('staff_details', $$
    CREATE (:Employee {name: 'Geyoung Ha', title: 'Project Lead'})
$$) as (n agtype);

Second Vertex:

SELECT *
FROM cypher('staff_details', $$
    CREATE (:Employee {name: 'Muneeb', title: 'Software Engineer'})
$$) as (n agtype);

After this, I created a directed edge from Vertex-1 to vertex-2 using:

SELECT *
FROM cypher('staff_details', $$
    MATCH (a:Employee), (b:Employee)
    WHERE a.name = 'Muneeb' AND b.name = 'Geyoung Ha'
    CREATE (a)-[e:supervised_by]->(b)
    RETURN e
$$) as (e agtype);

After this when I see all the edges between the Employee (verteices) in my Graph using:

SELECT * FROM cypher('staff_details', $$
MATCH (:Employee)-[e]-(:Employee)
RETURN e
$$) as (e agtype);

I get:

{"id": 1125899906842629, "label": "supervised_by", "end_id": 844424930131970, "start_id": 844424930131969, "properties"
: {}}::edge
 {"id": 1125899906842629, "label": "supervised_by", "end_id": 844424930131970, "start_id": 844424930131969, "properties"
: {}}::edge
(2 rows)

Why am I getting two same edges? But I have created only one directed edge.

2

Answers


  1. MATCH (a)-[e]-(b) checks for incoming or outgoing edges for each vertex in given pattern.

    Suppose there is a pattern created like (a)-[e]->(b). Now the way MATCH (a)-[e]-(b) works is, it will see for vertex ‘a’, if there is any edge ‘e’ outgoing or incoming towards it from ‘b’. Now it will see for vertex ‘b’, if there is any edge ‘e’ outgoing or incoming towards it from ‘a’.

    In this case, ‘a’ has an outgoing edge ‘e’ towards ‘b’, and ‘b’ has incoming edge ‘e’ from ‘a’. Hence output is kind of duplicated with just vertices opposite. If you display the start and end id of edge, you will notice that it is same for both results, just the vertices are opposite in output.

    So MATCH (:Employee)-[e]->(:Employee) will solve your problem.

    Login or Signup to reply.
  2. Here two edges are not getting created when you are creating an edge between the two vertices. Rather, when you query the edge by issuing MATCH (:Employee)-[e]-(:Employee), this query would check the incoming and outgoing edges for both the vertices (because the direction for matching the edge is not specified). Thus, Vertex-1 has outgoing edge and the same edge is incoming for Vertex-2 so it displays the edge twice. If you look closely to the output

    {"id": 1125899906842629, "label": "supervised_by", "end_id": 844424930131970, "start_id": 844424930131969, "properties"
    : {}}::edge

    The id, start_id and end_id of the edge is same that confirms that this is a single edge.

    If you specify the direction of edge as in MATCH (:Employee)-[e]->(:Employee) would check the edge that is originating at Vertex-1 and terminating at Vertex-2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search