cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to aggregate the nodes properties in different graphs

  • I want to calculate the total probability of graph by multiplying probabilities of nodes. but the multiplication changes according to connection of nodes. For example, the calculation for above graph should be like: The probability of that the end-to-end system will work from startnode2 (node a) to endnode2 (node e) following the relationship :TO will be: 1*(1-(1-0.999)*(1-0.999)*(1-0.999))*1)= 0.999999999 node.PNG

     
     
5 REPLIES 5

I posted a comment for a similar problem:

https://community.neo4j.com/t5/neo4j-graph-platform/calculate-joint-probabilities-of-paths-in-a-grap...

As I stated in the comment, I believe you will need to write a custom procedure if you are looking for an algorithm that will calculate the probability for any graph between the start and finish nodes. 

What are you looking for?

I've tried this, but it only handles nodes connected in series, not in loop.


MATCH path = (start:Node {name: "start"})-[:TO*]->(end:Node {name: "end"})

WITH nodes(path) as nodes_in_path

RETURN reduce(probability = 1.0, node in nodes_in_path | probability * 

    (CASE 

        WHEN size([(node)-[:TO]->(:Node) | 1]) > 1 THEN (1-node.probability)*node.probability

        ELSE node.probability END)) as probability


I think you can handle in cypher the scenario where you have multiple simple paths between start and end.  In simple, I mean they don't branch. I sketched it out on paper. 

IMG_2891.png

The result of your match statement will be a set of rows, each row corresponding to a path in the diagram. You can calculate each path's probability of success using the 'reduce' operation as you suggested. You can collect these values and use an overall 'reduce' method to calculate the overall probability of failure, then calculate the overall probability of success from the 'reduce' operations result and return it. 

Cypher would not be the tool if you want to be able to calculate the probability for a random network that can have branching and such. 

Can somone please correct this qury;

MATCH p = (startNode:Label1)-[*]->(endNode:Label2)
WITH nodes(p) AS pathNodes
WITH pathNodes,
REDUCE(prob = 1, i in RANGE(0, LENGTH(pathNodes) - 2) |
CASE WHEN (pathNodes[i])-[:DIRECT]->(pathNodes[i+1]) THEN prob * pathNodes[i].probability
ELSE prob * (1 - (1 - pathNodes[i].probability) * COUNT((pathNodes[i])-[:LOOPED*]->(pathNodes[i+1]))) END ) as aggregateProbability
RETURN aggregateProbability
Error:   
Invalid input '[': expected "+" or "-" (line 5, column 26 (offset: 179))
"CASE WHEN (pathNodes[i])-[:DIRECT]->(pathNodes[i+1]) THEN prob * pathNodes[i].probability"




You can’t use an expression when referencing a node or relationship in a pattern. To fix it, assign pathNode[i] and pathNodes[i+1] in a WTH statement to variables and use those variables in the pattern. Also, wrap your pattern in an ‘exits’  method to make it a predicate. I think your use is being deprecated.