1
votes

I have set of nodes which will be declared by user(I mean user enters some words and those which exist among my nodes will be included in my set of nodes),and I want to find the longest path which exists between just these nodes(does not have to include all of the nodes in set ,but should not include a node which is not exist in my set of nodes)in neo4j embedded java database.(I do not know what is the start node,all of them can be the start node.)

First I thought maybe I have to make a subgraph of these nodes and relations between them,and then find the longest path in that subgraph,but I don't know what is the right and best thing to do?

Then i thought,I can directly find longest path between the set of nodes(can that be done?).

Or if this can't be done,how about finding the relations of the certain nodes(set of nodes) and make a set of relations,and then find the longest path between set of relations?

I want the optimum and fastest way in performance,and I don't want cypher query,because I am using java core api(it is faster),so please tell me what is the best way?

Consider it as a large scale database,with so many nodes.

Thanks in advance.

1

1 Answers

0
votes

i'm afraid this cannot be done by any standard algorithm except traversing the whole graph. thus, there is no optimum way in performance. neo4j has no build-in option for it.

this is an cypher query (sorry i don't know other input languages in neo4j):

start n1=node(*), n2=node(user) 
match n1--n2    //filter only the just declared user nodes, or any other matching condition
with n1, n2
match p=n1-[r:..*]-m, m--n2    //all paths between declared user nodes
return p
order by length(r) desc
limit 1