Neo4j: How time-consuming is EVERY branch between node A and F?

Viewed 68

The following graph is given:

    -> B --> E -
   /            \ 
A -              -> F
   \            /
    -> C --> D -

All nodes are of type task. As properties, they have a start time and an end time (both are of the data type DateTime). All relationships are CONNECT_TO and are directed to the right. The relationships have no properties.

Can somebody help me how the following query should look like in Cypher: How time-consuming is EVERY branch between node A and F?

A list as result would be fine:

Path           Duration [minutes]
---------------------------------
A->B->E->F     100
A->C->D->F      50

Thanks for your help.

1 Answers

Creating your graph

The first statement creates the nodes, the second the relationships between them.

CREATE
  (TaskA:Task {name: 'TaskA', time:10}),
  (TaskB:Task {name: 'TaskB', time:20}),
  (TaskC:Task {name: 'TaskC', time:30}),
  (TaskD:Task {name: 'TaskD', time:10}),
  (TaskE:Task {name: 'TaskE', time:40}),
  (TaskF:Task {name: 'TaskF', time:10})

CREATE
  (TaskA)-[:CONNECT_TO]->(TaskB),
  (TaskB)-[:CONNECT_TO]->(TaskE),
  (TaskE)-[:CONNECT_TO]->(TaskF),
  (TaskA)-[:CONNECT_TO]->(TaskC),
  (TaskC)-[:CONNECT_TO]->(TaskD),
  (TaskD)-[:CONNECT_TO]->(TaskF);

Your desired solution

  1. Defining your start node (Task A)
  2. Finding path of variable length
  3. Defining your end node (Task F)
  4. Retrieve all task nodes for each path
  5. Sum the duration for all tasks of each path
  6. Bonus: amount of tasks per path

Neo4j Statement:

//           |----------- 1 -----------|  |----- 2 ----|  |----------- 3 -----------|
MATCH path = (taskA:Task {name: 'TaskA'})-[:CONNECT_TO*]->(taskF:Task {name: 'TaskF'})
UNWIND
// |-- 4 -|
nodes(path) AS task
//           |---- 5 -----|                  |--- 6 ----|
RETURN path, sum(task.time) AS timeConsumed, length(path)+1 AS taskAmount;

Result

╒══════════════════════════════════════════════════════════════════════╤════════════════╤════════════╕
│"path"                                                                │ "timeConsumed" │"taskAmount"│
╞══════════════════════════════════════════════════════════════════════╪════════════════╪════════════╡
│[{"name":"TaskA","time":10},{},{"name":"TaskB","time":20},{"name":"Tas│80              │4           │
│kB","time":20},{},{"name":"TaskE","time":40},{"name":"TaskE","time":40│                │            │
│},{},{"name":"TaskF","time":10}]                                      │                │            │
├──────────────────────────────────────────────────────────────────────┼────────────────┼────────────┤
│[{"name":"TaskA","time":10},{},{"name":"TaskC","time":30},{"name":"Tas│60              │4           │
│kC","time":30},{},{"name":"TaskD","time":10},{"name":"TaskD","time":10│                │            │
│},{},{"name":"TaskF","time":10}]                                      │                │            │
└──────────────────────────────────────────────────────────────────────┴────────────────┴────────────┘
Related