Check node is only neighbor node or a child node - contiki

Is it possible to check? If node 2 gets DIO from node 3, then node 2 checks whether node 3 is only a neighbor node or it is a child node of node 2.

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

How to duplicate node tree in neo4j?

How can I create full node tree from existing node tree, Actually i have 1 node and i need to find all relation and nodes that has my existing top node. I need to create full node tree to another node.
Copy All Node Tree from A with Relation and create Duplicate same node and relation for B Node
Okay, this is a tricky one.
As others have mentioned apoc.refactor.cloneNodesWithRelationships() can help, but this will also result in relationships between cloned nodes and the originals, not just the clones, as this proc wasn't written with this kind of use case in mind.
So this requires us to do some Cypher acrobatics to find the relationships from the cloned nodes that don't go to the cloned nodes so we can delete them.
However at the same time we also have to identify the old root node so we can refactor relationships to the new root (this requires separate processing for incoming and outgoing relationships).
Here's a query that should do the trick, making assumptions about the nodes since you didn't provide any details about your graph:
MATCH (a:Root{name:'A'})
WITH a, id(a) as aId
CALL apoc.path.subgraphNodes(a, {}) YIELD node
WITH aId, collect(node) as nodes
CALL apoc.refactor.cloneNodesWithRelationships(nodes) YIELD input, output
WITH aId, collect({input:input, output:output}) as createdData, collect(output) as createdNodes
WITH createdNodes, [item in createdData WHERE item.input = aId | item.output][0] as aClone // clone of root A node
UNWIND createdNodes as created
OPTIONAL MATCH (created)-[r]-(other)
WHERE NOT other in createdNodes
DELETE r // get rid of relationships that aren't between cloned nodes
// now to refactor relationships from aClone to the new B root node
WITH DISTINCT aClone
MATCH (b:Root{name:'B'})
WITH aClone, b
MATCH (aClone)-[r]->()
CALL apoc.refactor.from(r, b) YIELD output
WITH DISTINCT b, aClone
MATCH (aClone)<-[r]-()
CALL apoc.refactor.to(r, b) YIELD output
WITH DISTINCT aClone
DETACH DELETE aClone

Neo4j: Return only root nodes from possible child paths

I have a set of (n) values which all have corresponding nodes in my graph. I start with unknown relationships to each other. (see start nodes in blue)
I want to find, as simply as possible, is if any of the value/nodes are children of any of the others then applying these rules to filter the results:
If the node is a child then discard it. (white nodes)
If the node is a root then return it. (green nodes)
If the node does not have any children also return it. (green node 673)
There can be up to 50 starting nodes. I've tried iterating through them comparing two at a time discarding them if they are a child - but the number of iterations quickly gets out of hand in larger sets. I'm hoping there is some graph magic I've overlooked. Cypher please!
Thanks!
Let's say that you have an input parameter nids - set of values for the id property of node, target nodes have the label Node, the relationship between nodes is of type hasChild.
Then you need to find such nodes corresponding to the input set, and which do not have parents from the nodes corresponding to the input set:
UNWIND {nids} as nid
MATCH (N:Node {id: nid})
OPTIONAL MATCH (N:Node {id: nid})<-[:hasChild]-(P:Node) WHERE P.id IN {nids}
WITH N, collect(P) AS ts WHERE size(ts) = 0
RETURN N
And do not forget to add an index to the id property for the node:
CREATE INDEX ON :Node(id)

Neo4j displaying path of a hiearchy, and select the depth of hierarchy to display

My first Neo4j question - so gentle please
Here is some ascii art of what I want to show
1 - I have 2 nodes, Node 1 and Node 5, where Node1 has OUTPUTS relationship to Node3 and Node3 OUTPUTS to node 5
2 - In the case of Node 3, it is decomposed into Nodes 2,3 and 4, where
Node 2 and Node 4 are CHILD of Node 3.
I want to be able to selectively show a path that has the parent node only
PATH A below
OR
where I want to include the child nodes as well, PATH B
PATH A
node1 -[:OUTPUT]-> node3 - [:INPUT]-> node5
|
-----[CHILD]---- -------[CHILD]-----
| |
| |
node1-[:OUTPUT]->node2-[:INPUT]->node3-[OUTPUT]->node4-[INPUT]-> node5
PATH B
thanks all

Using Property value of Node1 as a label name of Node2

I'm using neo4j 2.1.2 Community Edition.
I have a node called Labelled as Company and it's properties are as below:
id : 101
Name : Company
Also i have more node which is a child node of Company with a relationship :Active . I want this child node label-name should derive from the ParentNode. That is the parent Name property should act as a label for it's Child Node.
My Child node looks like as below:
Id : 101
Name: Flipkart
How can i achieve this through Cypher query. How can i pass the Property of one node to act as a Label of another node ?
Thanks

Resources