Adjacency Matrix using Cypher Query


I am new to Neo4j and I am trying to extract the adjacency matrix of my graph. I found this cypher query ( to get the adjacency matrix but it return the matrix without the row names and column names. How is it possible to save the names too?

Here is the cypher query:

// Get all vertices.
WITH collect(n) AS Nodes
// For each vertices combination...
WITH [n IN Nodes |
[m IN Nodes |
// ...Check for edge existence.
CASE size((n)--(m))
] AS AdjacencyMatrix
// Unroll rows.
UNWIND AdjacencyMatrix AS AdjacencyRows
RETURN AdjacencyRows;

I'm not sure I would do it that way at all, or at least I'd step back to find out more about what you're trying to accomplish before proceeding.

The simplest adjacency matrix would be node IDs as rows, node IDs as columns, and a boolean in a cell i f there was a relationship between those two node IDs.

You might also check out the graph data science library, which has a concept of in-memory graphs on top of Neo4j which is already very similar to what you're trying to do here.

1 Like