neo4j / graph-data-science

Source code for the Neo4j Graph Data Science library of graph algorithms.
https://neo4j.com/docs/graph-data-science/current/
Other
629 stars 161 forks source link

Bug: Degree centrality algorithm ignores reverse relationship projection #60

Closed itsabdelrahman closed 4 years ago

itsabdelrahman commented 4 years ago

Environment

Reproduction steps

  1. Insert sample graph
MERGE (alice:User {id: "Alice"})
MERGE (bridget:User {id: "Bridget"})
MERGE (charles:User {id: "Charles"})
MERGE (doug:User {id: "Doug"})
MERGE (mark:User {id: "Mark"})
MERGE (michael:User {id: "Michael"})

MERGE (alice)-[:FOLLOWS]->(bridget)
MERGE (alice)-[:FOLLOWS]->(charles)
MERGE (mark)-[:FOLLOWS]->(doug)
MERGE (mark)-[:FOLLOWS]->(michael)
MERGE (bridget)-[:FOLLOWS]->(michael)
MERGE (doug)-[:FOLLOWS]->(mark)
MERGE (doug)-[:FOLLOWS]->(michael)
MERGE (michael)-[:FOLLOWS]->(mark)
MERGE (michael)-[:FOLLOWS]->(alice)
MERGE (michael)-[:FOLLOWS]->(doug)
MERGE (alice)-[:FOLLOWS]->(michael)
MERGE (bridget)-[:FOLLOWS]->(alice)
MERGE (michael)-[:FOLLOWS]->(bridget)
  1. Compute degree centrality
CALL gds.alpha.degree.stream({
  nodeProjection: "User",
  relationshipProjection: {
   FOLLOWS: {
        type: "FOLLOWS",
        projection: "REVERSE"
   }
  }
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).id AS userId, score AS degreeCentrality
ORDER BY degreeCentrality ASC

which yields:

╒═════════╤══════════════════╕
│"userId" │"degreeCentrality"│
╞═════════╪══════════════════╡
│"Charles"│0.0               │    <-- should be 1.0
├─────────┼──────────────────┤
│"Bridget"│2.0               │
├─────────┼──────────────────┤
│"Doug"   │2.0               │
├─────────┼──────────────────┤
│"Mark"   │2.0               │
├─────────┼──────────────────┤
│"Alice"  │3.0               │    <-- should be 2.0
├─────────┼──────────────────┤
│"Michael"│4.0               │
└─────────┴──────────────────┘

which is identical to the results yielded by running the same algorithm without the reverse relationship projection option.

Mats-SX commented 4 years ago

@ar-maged Thanks for the very comprehensive bug report! Very easy to follow steps, and I quickly identified the issue: the key in the relationship projection for controlling the projected direction of the relationship is called orientation, not projection. This is why the results are the same regardless of the value you use for the key (it is being ignored).

In the upcoming 1.3 version of the GDS library we have improved our configuration validation, so your query actually reports the unknown projection key with an error message. You can check the preview release out already today on our release page.

When using orientation: 'REVERSE' I'm able to see the correct results. Try that and see if it resolves things for you as well.

itsabdelrahman commented 4 years ago

@Mats-SX Thanks a bunch for the quick response and the thorough explanation! orientation: 'REVERSE' works like a charm. It appears that the documentation just needs to be updated then.

Good luck with the upcoming v1.3 💪

Mats-SX commented 4 years ago

Aha! Thanks for pointing that out. We will make sure to amend it. I will reopen this issue and close it once the documentation has been corrected.

jjaderberg commented 4 years ago

Docs have been updated. Closing. Thanks all.