Neo4j cypher query to see whether given nodes are connected - neo4j

Given the following nodes B,E,F,G,H,J,K. I have to write a neo4j query to check how these nodes are connected each other.
For example, suppose this is the pattern I want to display.
B-H-F
|
G-J-K-E
What is the query to display this graph? I don't want to display all other nodes which are connected to these nodes.

You need to find the paths between each pair of source nodes.
Make sure that each path consists of only those nodes that are included in the source set.
You can do this with a cypher:
WITH {GIVEN_NODES} as NDS // The source array of node IDs
UNWIND RANGE(0, size(NDS)-2) as i
UNWIND RANGE(i+1, size(NDS)-1) as j
WITH NDS,
NDS[i] as N1,
NDS[j] as N2
MATCH path = (N1)-[*]-(N2)
WHERE length(path)+1 <= size(NDS) AND
ALL(n in nodes(path) WHERE n in NDS)
RETURN path

Related

Neo4J common neighbors of a set nodes in a path

My network consists of nodes and the relationship is a numeric number. Think of it as a set of cities, and the relationship is the whether there is a road and if so how far is it.
I have path from my neo4j query, wonder how I can find the neighbor of this path given the following condition. These neighbors should be neighbor to more than one node in the path.
In the following picture, I have tried to illustrate what I mean. My path looks like the blue star below. I would like to find the green nodes. These green nodes, are connected to two or more nodes in the path. I have draw a few of these green nodes.
As an output I would like to have a path that include the blue path as well as the green ones.
EDIT
My original path looks like
If I use the suggested solution by #NonameCurious, I will have
As you can see the result is a group of nodes which there is no relationship. I assume it is because the query only returns nodes. However, I would like to have the connection between those "neighbors" with the original path be displayed on top of the original path.
How about this:
WITH nodes(path) AS nodes
UNWIND nodes AS node
MATCH (a)--(node) WHERE NOT a IN nodes
WITH a, COUNT(DISTINCT node) AS relCounts
WITH a WHERE relCounts > 1
RETURN a
I am assuming path is given.
UPDATE:
If you need to filter relationships, you can use something like this:
WITH nodes(path) AS nodes
UNWIND nodes AS node
MATCH (a)-[r]-(node) WHERE NOT a IN nodes AND r.score > 27
WITH a, COUNT(DISTINCT node) AS relCounts
WITH a WHERE relCounts > 1
RETURN a
SECOND UPDATE:
If you just want to somehow get a subgraph of all the new nodes along with the old ones, you can do this
WITH nodes(path) AS nodes, path UNWIND nodes AS node MATCH (a)-[r]-(node)
WHERE NOT a IN nodes AND r.score > 27
WITH a, COLLECT(DISTINCT node) AS connectedNodes, COLLECT(DISTINCT r) AS connectedRels, path WHERE SIZE(connectedNodes) > 1
UNWIND connectedNodes AS connectedNode
UNWIND connectedRels AS connectedRel
RETURN a, connectedRel, connectedNode, path

Neo4J: How can I find if a path traversing multiple nodes given in a list exist?

I have a graph of nodes with a relationship NEXT with 2 properties sequence (s) and position (p). For example:
N1-[NEXT{s:1, p:2}]-> N2-[NEXT{s:1, p:3}]-> N3-[NEXT{s:1, p:4}]-> N4
A node N might have multiple outgoing Next relationships with different property values.
Given a list of node names, e.g. [N2,N3,N4] representing a sequential path, I want to check if the graph contains the nodes and that the nodes are connected with relationship Next in order.
For example, if the list contains [N2,N3,N4], then check if there is a relationship Next between nodes N2,N3 and between N3,N4.
In addition, I want to make sure that the nodes are part of the same sequence, thus the property s is the same for each relationship Next. To ensure that the order maintained, I need to verify if the property p is incremental. Meaning, the value of p in the relationship between N2 -> N3 is 3 and the value p between N3->N4 is (3+1) = 4 and so on.
I tried using APOC to retrieve the possible paths from an initial node N using python (library: neo4jrestclient) and then process the paths manually to check if a sequence exists using the following query:
q = "MATCH (n:Node) WHERE n.name = 'N' CALL apoc.path.expandConfig(n {relationshipFilter:'NEXT>', maxLevel:4}) YIELD path RETURN path"
results = db.query(q,data_contents=True)
However, running the query took some time that I eventually stopped the query. Any ideas?
This one is a bit tough.
First, pre-match to the nodes in the path. We can use the collected nodes here to be a whitelist for nodes in the path
Assuming the start node is included in the list, a query might go like:
UNWIND $names as name
MATCH (n:Node {name:name})
WITH collect(n) as nodes
WITH nodes, nodes[0] as start, tail(nodes) as tail, size(nodes)-1 as depth
CALL apoc.path.expandConfig(start, {whitelistNodes:nodes, minLevel:depth, maxLevel:depth, relationshipFilter:'NEXT>'}) YIELD path
WHERE all(index in range(0, size(nodes)-1) WHERE nodes[index] = nodes(path)[index])
// we now have only paths with the given nodes in order
WITH path, relationships(path)[0].s as sequence
WHERE all(rel in tail(relationships(path)) WHERE rel.s = sequence)
// now each path only has relationships of common sequence
WITH path, apoc.coll.pairsMin([rel in relationships(path) | rel.p]) as pairs
WHERE all(pair in pairs WHERE pair[0] + 1 = pair[1])
RETURN path

Using Cypher how would one select all nodes connected to a node within exactly one hop whilst excluding the central node from the result?

Take the above image as an example. Using Cypher, how would I match all of the nodes except for the longest chain and the central node? I.e. all nodes within exactly one hop of the central node whilst excluding the central node (all nodes and edges except 3 nodes and 2 edges).
I have tried the following:
MATCH (n:Node) WHERE n.id = "123" MATCH path = (m)-[*1..1]->(n) RETURN m
This very nearly works, however it still returns the central node (i.e. node n). How would I exclude this node from my query result?
[UPDATED]
This will return all distinct nodes directly connected to the specified node, and explicitly prevents the specified node from being returned (in case it has a relationship to itself):
MATCH (n:Node)--(m)
WHERE n.id = "123" AND n <> m
RETURN DISTINCT m;
Ideally I would have liked to match the nodes as mentioned in my question and delete them. However, as I have not found a way to do so an inverse approach can be utilised whereby all nodes but those as mentioned in the question are matched instead. Thereby effectively excluding (but not deleting) the unwanted nodes.
This can be achieved using this query:
MATCH (n:Node) WHERE n.id = "123" MATCH path = (m)-[*2..]->(n) RETURN path
This returns the central node and all paths to that node that have a "length" greater than or equal to 2.

Get nodes which are not connected to specific node in Neo4j

I want to get all the nodes which are not connected to the given set of nodes. Suppose I've 5 nodes A,B,C,D,E. Now A->B->C are connected with the :Is_Friend relationship. Now I want all the nodes which are not connected to A (i.e D and E).
I tried this query but it's not working
MATCH (a:Friend{name:"A"})-[:Is_Friend_Of*]->(b:Friend)
MATCH (c:Friend)
WHERE NOT (c)-[:Is_Friend_Of]->(b)
RETURN c
Thi query should do what you want it to, however, I would caution that depending on the size of the number of unmatched friends in your database you could get a lot of matches.
// match the single longest chain of friends in a :Is_Friend_Of relationship
// starting with 'A' that is possible
MATCH path=(a:Friend {name:"A"})-[:Is_Friend_Of*]->(b:Friend)
WHERE NOT (b)-[:Is_Friend_Of*]->()
WITH path
// then find the other friends that aren't in that path
MATCH (c:Friend)
WHERE NOT c IN nodes(path)
RETURN c

Neo4j Cypher find two disjoint nodes

I'm using Neo4j to try to find any node that is not connected to a specific node "a". The query that I have so far is:
MATCH p = shortestPath((a:Node {id:"123"})-[*]-(b:Node))
WHERE p IS NULL
RETURN b.id as b
So it tries to find the shortest path between a and b. If it doesn't find a path, then it returns that node's id. However, this causes my query to run for a few minutes then crashes when it runs out of memory. I was wondering if this method would even work, and if there is a more efficient way? Any help would be greatly appreciated!
edit:
MATCH (a:Node {id:"123"})-[*]-(b:Node),
(c:Node)
WITH collect(b) as col, a, b, c
WHERE a <> b AND NOT c IN col
RETURN c.id
So col (collect(b)) contains every node connected to a, therefore if c is not in col then c is not connected to a?
For one, you're giving this MATCH an impossible predicate to fulfill, so it will never find the shortest path.
WHERE clauses are associated with MATCH, OPTIONAL MATCH, and WITH clauses, so your query is asking for the shortest path where the path doesn't exist. That will never return anything.
Also, the shortestPath will start at the node you DON'T want to be connected, so this has no way of finding the nodes that aren't connected to it.
Probably the easiest way to approach this is to MATCH to all nodes connected to your node in question, then MATCH to all :Nodes checking for those that aren't in the connected set. That means you won't have to do a shortestPath from every single node in the db, just a membership check in a collection.
You'll need APOC Procedures for this, as it has the fastest means of matching to nodes within a subgraph.
MATCH (a:Node {id:"123"})
CALL apoc.path.subgraphNodes(a, {}) YIELD node
WITH collect(node) as subgraph
MATCH (b:Node)
WHERE NOT b in subgraph
RETURN b.id as b
EDIT
Your edited query is likely to blow up, that's going to generate a huge result set (the query will build a result set of every node reachable from your start node by a unique path in a cartesian product with every :Node).
Instead, go step by step, collect the distinct nodes (because otherwise you'll get multiples of the same nodes that can be reached via different paths), and then only after you have your collection should you start your match for nodes that aren't in the list.
MATCH (:Node {id:"123"})-[*0..]-(b:Node)
WITH collect(DISTINCT b) as col
MATCH (a:Node)
WHERE NOT a IN col
RETURN a.id

Resources