Top.Deck
Top.Deck

Reputation: 1097

Use algo on Subgraph

Let's say I have the following sub-graph (or a view):

MATCH(n:Person)-[]->(m:Person) RETURN n, m;

How could I run CALL algo.louvain.stream against the above result graph?

And further, how could I run algo.scc on each community find by algo.louvain?

Upvotes: 0

Views: 201

Answers (1)

call-in-co
call-in-co

Reputation: 281

In case you hadn't found (or couldn't decipher) the Neo4j docs, here's the example provided:

CALL algo.louvain(
  'MATCH (p:User) RETURN id(p) as id',
  'MATCH (p1:User)-[f:FRIEND]-(p2:User)
   RETURN id(p1) as source, id(p2) as target, f.weight as weight',
  {graph:'cypher',write:true});

The docs don't say it explicitly, but the first query passed to algo.louvain specifies the collection of nodes to be considered by algo.louvain and the second query specifies the relationships among the nodes (or connected to the nodes) specified in query 1.

This means that you're going to have to specify whatever relationship type it is that's between the empty square brackets that you mentioned in the question. The algorithm won't run without something to connect the nodes— without edges to the vertexes, it's not a graph, after all.

Also, It is crucially important to replicate the RETURN id(p1) as source, id(p2) as target bit, no matter what the shape of your second query is, otherwise Java will throw an error.

Lastly, in terms of running algo.scc on each community, you'll need to modify the above query to specify the writeProperty in the configuration to algo.louvain. Once that query runs successfully, every node that was considered in the algo.louvain run will have a 0, 1, 2, etc. value as the property that you specified. Then, for each of these communities, you can call algo.scc as in here with the same structure as the above call to algo.louvain; however in the first query passed to algo.scc you'll specify the community that you're after. For example (this is off the cuff based on your question, not from the docs):

// strongly-connected components for community 0
CALL algo.scc(
  'MATCH (n:Person {algo.louvain.writeProperty here: 0}) RETURN id(n) as id',
  'MATCH (n:Person)-[k:KNOWS]->(m:Person)
   RETURN id(n) as source, id(m) as target',
  {graph:'cypher',write:true,writeProperty:'partition'})
  YIELD loadMillis, computeMillis, writeMillis, setCount, maxSetSize, minSetSize

Then, as the docs suggest, you can find the largest partition with the following:

MATCH (u:Person)
RETURN u.partition as partition,count(*) as size_of_partition
ORDER by size_of_partition DESC
LIMIT 1;

or further query your (sub)graph in any way that you choose because the results of the algorithms are written as properties on the Person nodes. HTH.

Upvotes: 2

Related Questions