// Critical Path Analysis Query (Optimized) // Finds the longest path in terms of total duration of jobs. // 1. Find all potential start nodes first MATCH (start_node:Job) WHERE NOT EXISTS((:Job)-[:PRECEDES]->(start_node)) // 2. Find all potential end nodes first MATCH (end_node:Job) WHERE NOT EXISTS((end_node)-[:PRECEDES]->(:Job)) // 3. Now, match paths ONLY between the pre-filtered start and end nodes MATCH path = (start_node)-[:PRECEDES*]->(end_node) // 4. Calculate the total duration for each path WITH path, REDUCE(totalDuration = 0, job IN nodes(path) | totalDuration + job.duration) AS total_duration // 5. Return the path and its total duration for visualization RETURN path, total_duration // 6. Order by the total duration in descending order to find the longest path ORDER BY total_duration DESC // 7. Limit to the top 1 result, which is the Critical Path LIMIT 1;