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.

Adjacency matrix

Hi everyone

I would like to find the adjacency matrix of the following bipartite graph

redha_benhisse1_0-1669936093203.jpeg

if the link exists, the matrix takes the value revenue. otherwise, it takes 0.

MATCH (c:customer)
WITH collect(c) AS cust
UNWIND cust AS a
MATCH (p:part)
WITH collect(p) AS par
UNWIND par AS b
RETURN a.id AS cutomer, b.id AS part, 
CASE 
WHEN EXISTS((a)-[r:customer_to_part]->(b)) THEN r.revenue 
ELSE 0 
END AS value

But this does not work.

Can you please help me to find the right matrix

1 ACCEPTED SOLUTION

Just a couple notes. The order of the query operations will cause the 'match(p:part) collect(p)' block to be repeated for each customer node. Also, its not allowed to introduce a variable in a pattern expression, so returning r.revenue isn't allowed. I refactored your query to resolve these two issues.

match(c:customer)
with collect(c) as customers
match(p:part)
with customers, collect(p) as parts
unwind customers as customer
unwind parts as part
optional match (customer)-[r:customer_to_part]->(part)
return customer.id as cust_id, part.id as part_id,
case
    when r is not null then r.revenue
    else 0
end as value

View solution in original post

2 REPLIES 2

Just a couple notes. The order of the query operations will cause the 'match(p:part) collect(p)' block to be repeated for each customer node. Also, its not allowed to introduce a variable in a pattern expression, so returning r.revenue isn't allowed. I refactored your query to resolve these two issues.

match(c:customer)
with collect(c) as customers
match(p:part)
with customers, collect(p) as parts
unwind customers as customer
unwind parts as part
optional match (customer)-[r:customer_to_part]->(part)
return customer.id as cust_id, part.id as part_id,
case
    when r is not null then r.revenue
    else 0
end as value