Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10058

Generate table view from a graph in neo4j

I have the following graph slightly modified from this question.

The generated graph I have is the:

Generated graph

But I want to get the original table that has been inserted with either in html or regenerating the original excel:

Original table

So which is the neo4j query that return the result above?

Upvotes: 0

Views: 255

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30407

You won't be able to for two reasons.

One is that direction will be impossible to determine. You have (using shorthand)

(1,2)->(6,7)<-(3,2)
(6,7)->(9,2)->(5,1)
(7,7)->(4,1)->(1,2)
(6,7)->(7,7)->(3,2)

Unless your graph has an error, you can see that the relationship between (6,7) and (3,2) isn't consistent with the rest, so my guess is you weren't consistent with the relationship direction when creating the graph, and if this is so this throws off any possible ordering when attempting to generate the table.

But if we assume that this was a mistake in merging to the graph, and it was supposed to be (6,7)->(3,2), then that fixes the ordering issue.

But now there's other ordering issues. You have no data in your graph to determine which node to start with.

The graphical result of this line alone: (7,7), (4,1), (1,2) can be expressed just as well with (4,1), (1,2), (7,7) or (1,2), (7,7), (4,1).

You may be able to generate rows back that will be logically equivalent, but you won't be able to get back the identical input.

But again, assuming that the graph was supposed to be (6,7)->(3,2), and assuming that a logically equivalent table is fine, then something like this may work:

MATCH p=(a)-->(b)-->(c)-->(a)
UNWIND nodes(p) as node
WITH p, node
ORDER BY id(node)
WITH p, collect(node) as nodes
WITH head(collect(p)) as path, nodes
WITH path[0] as a, path[1] as b, path[2] as c
RETURN a.x as Xa, a.y as Ya, b.x as Xb, b.y as Yb, c.x as Xc, c.y as Yc

Why so complicated? Because for each triangle of three nodes, there are 3 ways to order those nodes (as mentioned above), and so there will be 3 paths for each set of the same nodes. To avoid redundant rows with the same points in different orders, we order the nodes in the path, collect the 3 paths, take only one of them, and use those when returning the x and y values.

Upvotes: 1

Related Questions