2

I'd like to model autonomous systems and their relationships in Graph Database (memgraph-db)

There are two different kinds of relationships that can exist between nodes:

  • undirected peer2peer relationships (edges without arrows in image)
  • directed provider2customer relationships (arrows pointing to provider in image)

The following image shows valid paths that I want to find with some query

valid paths (source: caida.org)

They can be described as

(s)-[:provider*0..n]->()-[:peer*0..n]—()<-[:provider*0..n]-(d)

or in other words

0-n c2p edges followed by 0-n p2p edges followed by 0-n p2c edges

I can fix the first and last node and would like to find a (shortest/cheapest) path. As I understand I can do BFS if there is ONE relation on the path.

Is there a way to query for paths of such form in Cypher?

As an alternative I could do individual queries where I specify the length of each of the segments and then do a query for every length of path until a path is found.

i.e.

MATCH (s)<-[]->(d)                            // All one hop paths
MATCH (s)-[:provider]->()-[:peer]-(d)         
MATCH (s)-[:provider]->()<-[:provider]-(d)
...
xuma202
  • 1,074
  • 1
  • 10
  • 22
  • 1
    It seems like a network use-case where the goal is to find the shortest path between two providers. Is that correct? Providers can also be connected, but there has to be a peer in between, which causes problems with the simple BFS? Is that the right reasoning, or is there something else? If you could elaborate a bit more on the high-level goal, it would help me understand better in the first place. – buda Feb 10 '21 at 15:57
  • 1
    Yes this is a network use-case. I've added some more context to the question. – xuma202 Feb 10 '21 at 16:35
  • Peer and customers are equivalent? Why is path 1. valid, and path 4. is invalid? Z,A,D,E,F are providers and B,C are peers? In general, if you could write one variable-length path (BFS) with some filtering that would be great. – buda Feb 10 '21 at 19:47
  • 1
    A node can be peer and customer. All nodes are the same, it's the relationships that are either of type peer or customer/provider. Path 4 is invalid due to the edge between Z and B that is traversed in the wrong direction. A legal path must be of the described there sections (that can be empty) and 4 has two p2c (or c2p) sections. I was able to match paths with `()-[:provider * bfs]->()-[:peer * bfs]-()<-[:provider * bfs]-()` however bfs will not allow empty path for the sections. So I will have to threat 7 cases where at least one segment is empty. – xuma202 Feb 11 '21 at 01:03
  • I think I finally got the problem, sorry about the confusion with edge types vs node labels. – buda Feb 11 '21 at 11:46

1 Answers1

1

Since it's viable to have 7 different path sections, I don't see how 3 BFS patterns (... BFS*0..n) would yield a valid solution. It's impossible to have an empty path because the pattern contains some nodes between them (I have to double-check that).

Writing individual patterns is not great.

Some options are:

  • MATCH path=(s)-[:BFS*0.n]-(d) WHERE {{filter_expression}} -> The expression has to be quite complex in order to yield valid paths.
  • MATCH path=(s)-[:BFS*0.n]-(d) CALL module.filter_procedure(path) -> The module.procedure(path) could be implemented in Python or C/C++. Please take a look here. I would recommend starting with Python since it's much easier. Python for the PoC should be fine. I would also recommend starting with this option because I'm pretty confident the solution will work, + it's modular. After all, the filter_procedure could be extended easily, while the query will stay the same.

Could you please provide a sample dataset in a format of a Cypher query (a couple of nodes and edges / a small graph)? I'm glad to come up with a solution.

buda
  • 460
  • 4
  • 8