How to get all the unique path and calculate a numeric value?

Question :

  • How can I get all unique the paths in optimal way , from a target node till node at 3rd hop ?
  • How can I optimize this query or is there any other way to so that I can get the same output ?

Condition :

  • The path should not traverse from node at 4th hop and get back to 3rd hop node .
    (Paths going beyond 3rd hop and coming back to 3rd hop node should be eliminated )

Data Base Details :

  • I have a dataset of about 100K nodes and 600K edges
  • I have set the dbms.memory.heap.initial_size and dbms.memory.heap.max_size = 6G
    dbms_memory_pagecache_size = 2G
  • Neo4j version - 4.0.4
  • Plugins - APOC

Problem Faced

  • This query is takes 4-5 hrs when , there are more than 5K nodes at 3rd hop and sometime it gives me OUT-OF-MEMORY error .

I am using the query below -

           MATCH (p)
           call apoc.neighbors.athop(p,'KNOWS', 2) yield node
           with collect(id(node)) as d3node,p
           call apoc.neighbors.athop(p,'KNOWS', 3) yield node
           with collect(id(node)) as d4node,p,d3node
           CALL apoc.path.expandConfig(p, {
                relationshipFilter: "KNOWS",
                blacklistNodes: d4node
                endNodes:d3node,
                uniqueness:'NODE_PATH'
            })YIELD path
            with [r in relationships(path) |  r.foo ] as foo, [n in  nodes(path) | n.foobar] as nfoobar,p
            with reduce(res = 1 , w in foo | res * w) as prod,nfoobar,p
            return  prod