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.

How can the first and last node in a graph have different color?

Hello all, I have a use case wherein a process for resolving a issue is represented as graph. Below is the data which I have used.

Operator ID, Activity Done, path
14, enter, 1
12, open, 2
13, change, 3
14, update, 4
15, close, 5
16, view, 6
14, update, 7
11, Note, 8

I have used the below query:

LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.Operator ID) AS ids, collect(line.Activity Done) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.merge.relationship(n, activities[i], {path: path[i]}, {path: path[i]}, m, {path: path[i]})
YIELD rel
RETURN rel

Output in Neo4j is as below:

My question is:

How can "only" the first node (in this case first node is Operator ID=14) and last node (in this case last node is Operator ID=11) have different colors , other nodes can have same color.

I have gone through K-1 Coloring algorithm, but it assigns different color to all nodes present. And I have also tried in neo4j bloom but there I had manually changed the color for a particular node but that is not I want.

Is this possible? How can we implement it in the given query so as to get different colors for just First & last Node?

The purpose for asking this question was to, Identify where the process starts & end by looking at the graph.

Thank you.

(P.S- Thanks @Cobra for helping with the query)

1 ACCEPTED SOLUTION

The one query solution:

LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.merge.relationship(n, activities[i], {path: path[i]}, {path: path[i]}, m, {path: path[i]})
YIELD rel
WITH apoc.coll.flatten(collect([startNode(rel), endNode(rel)])) AS nodes
WITH [head(nodes), last(nodes)] AS startEnd
UNWIND startEnd AS n
MATCH (n)
SET n:StartEnd

View solution in original post

6 REPLIES 6

Hello @munavath.2001077

Do you mean a different color for the nodes in Neo4j Desktop? If yes, you will have to add a new Label to the start and end nodes (you can keep or remove the Operator label from these nodes or keep it). Check this link for more style.

MATCH (a:Operator)-[r]->(b:Operator)
WHERE r.path IS NOT NULL
WITH DISTINCT r.path AS path, a.id AS a, b.id AS b
ORDER BY path
WITH apoc.coll.flatten(collect([a, b])) AS ids
WITH [head(ids), last(ids)] AS startEnd
MATCH (n:Operator)
WHERE n.id IN startEnd
SET n:StartEnd
REMOVE n:Operator

Don't forget that the visualization in Neo4j Desktop is only here to help you to check what you get from queries.

Regards,
Cobra

Thank you @Cobra for the reply.

Yes I meant different color for the nodes in Neo4j Desktop. So, Do I add the above query which you posted now in the query which I have mentioned?

Not it's a different query, you can execute this one after the previous one.

Or you only want one query?

Okay @Cobra, Do I need to add "Load the the CSV file" statement again in both of these queries if I execute individually.

If it is one query, that would a lot helpful.

The one query solution:

LOAD CSV WITH HEADERS from "file:///C:/Process.csv" AS line
WITH collect(line.`Operator ID`) AS ids, collect(line.`Activity Done`) AS activities, collect(line.path) AS path
UNWIND ids AS id
MERGE (n:Operator {id: id})
WITH DISTINCT ids, activities, path
UNWIND range(0, size(ids)-1) AS i
WITH ids[i] AS a, ids[i+1] AS b, i, activities, path
WHERE a IS NOT null AND b IS NOT null
MATCH (n:Operator {id: a})
MATCH (m:Operator {id: b})
CALL apoc.merge.relationship(n, activities[i], {path: path[i]}, {path: path[i]}, m, {path: path[i]})
YIELD rel
WITH apoc.coll.flatten(collect([startNode(rel), endNode(rel)])) AS nodes
WITH [head(nodes), last(nodes)] AS startEnd
UNWIND startEnd AS n
MATCH (n)
SET n:StartEnd

Thanks a ton @Cobra

It works perfectly.