user1919
user1919

Reputation: 3938

How to represent the lines of a network in Neo4j

I am trying to realize a datamodel in Neo4j. The model has points of interest in a city and streets. The streets connect the points. Initially I thought that points and streets should both represented in the graph database as nodes. Between these two different type of nodes there is a relationship ("point is connected with"). Now I am thinking the possibility that instead of representing the street as a node, perhaps is more correct to represent the street as relationship ("connects two points") And this is my question actually. What is the more correct way to represent the network (line part) in a model: with nodes or with relationships?

Upvotes: 0

Views: 178

Answers (1)

Neel Virdy
Neel Virdy

Reputation: 148

The only major difference between relationships and nodes is that relationships must exist between two nodes. This means that you wouldn't be able to store a specific street if you didn't store two points of interest that it connects. So, if you see this being an issue, you may want to store streets as nodes. If you are certain that you will only want to store streets if there are points of interest in your database that exist on the street, then it'd make more sense to represent the streets as relationships.

In general, you should try to avoid storing properties in nodes that you only intend to use to find relationships between them. In this case, you mention possible storying a "point is connected with" property in each point of interest node. This would work, but is essentially just saying that a relationship exists between two points without actually using a relationship. Again, in the case where you want to be able to store streets that don't have points of interests existing on them, this may be necessary, and you could store streets that don't have points of interests on them by leaving the "point is connected with" property as NULL, but I would advise against this.

Another thing to think about is what you would store in the relationship. If you go with the model where streets are nodes, it becomes very difficult to represent quantities like distances between points of interest without adding relationships into your graph specifically for those properties, which may as well be properties of a street relationship.

UPDATE: Thought I'd add an example query to show how making the streets relationships can simplify your logic and make using your database much simpler and more intuitive.

Imagine you wish to find the path with the fewest points of interest between points A and B.

This is what the query would look like with the relationships model:

MATCH (a:Point {name: "foo"}), (b:Point {name: "bar"}),
  p = shortestPath(a-[*:Street]-b)
RETURN p

By using relationships where appropriate, you enable the capabilities of Neo4j, allowing you to get a lot of work done with relatively simple queries. It's hard to think of a way to write this query in the model where you represent streets as nodes, but it would in all likelihood be much more complex and less efficient.

Upvotes: 3

Related Questions