# Reduce a graph to a DAG

**URL:** <https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662>\
**Category:** Cypher\
**Created:** [December 7, 2021, 7:36pm UTC](https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662 "2021-12-07T19:36:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy\_hegedus](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andy_hegedus/32/18744_2.png) [@andy\_hegedus](https://community.neo4j.com/u/andy_hegedus)\
**Post date:** [December 7, 2021, 7:36pm UTC](https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662/1 "2021-12-07T19:36:20Z")

</div>

Hi,

Here is the scenario: I have graph with a series of documents that reference each other with directed relationships. A given document may reference parents, grandparents, and or great-grandparents. What I am interested in is reducing the graph to bare essence to so that the reference node relationships go through the tree structure. I am looking to pare down the relationships to highlight the the flow through the documents.  
For example if a start with this.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/b/7bf0e1c2ff763a6c902134c2001b26cf8a41f12b.png)

My hand trimming reveals this:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/e/7/e7543e9d68f3c6ee19177ee558d3a44d061fb7b9.png)

Is there a generic cypher strategy to reveal this pattern from the original. I have date properties and document serial numbers as property keys to assist.

How would I go about writing this?  
Andy

---

<div class="post-metadata">

**Author:** ![martin3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/martin3/32/21016_2.png) [@martin3](https://community.neo4j.com/u/martin3)\
**Post date:** [December 10, 2021, 5:11pm UTC](https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662/2 "2021-12-10T17:11:52Z")

</div>

A suggestion:

```auto
MATCH (d1:Document)-[:CITES]->(:Document)-[:CITES]->(d3:Document)<-[c:CITES]-(d1)
DELETE c

```

But please make sure it really does what you want on a backup, before deleting your citations :)

---

<div class="post-metadata">

**Author:** ![andy\_hegedus](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andy_hegedus/32/18744_2.png) [@andy\_hegedus](https://community.neo4j.com/u/andy_hegedus)\
**Post date:** [December 10, 2021, 10:15pm UTC](https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662/3 "2021-12-10T22:15:40Z")

</div>

Thank you I will give a try, though to be safe I might just set a visibility property as a start. The down side of course is that it is not possible to purge unwanted property keys later.

Andy

---

<div class="post-metadata">

**Author:** ![martin3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/martin3/32/21016_2.png) [@martin3](https://community.neo4j.com/u/martin3)\
**Post date:** [December 10, 2021, 10:37pm UTC](https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662/4 "2021-12-10T22:37:24Z")

</div>

I am not sure if I understood what you wanted to achieve, and I didn't test the query myself, so better test it on a backup first. But maybe you can alter that query to get exactly what you want. Another alternative, yielding a different result, might be:

```auto
MATCH (d1:Document)-[c1:CITES]->(:Document)-[c2:CITES]->(d3:Document)<-[:CITES]-(d1)
DELETE c1, c2

```

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [December 11, 2021, 5:16am UTC](https://community.neo4j.com/t/reduce-a-graph-to-a-dag/48662/5 "2021-12-11T05:16:47Z")

</div>

You can try with virtual nodes. Check this link:

> [@How do I create a subnetwork from a large network?](https://community.neo4j.com/t/how-do-i-create-a-subnetwork-from-a-large-network/28595/3):
>
> I used your Cypher script and added the [:rest] relationships with the aim to get End State merge (a1:Delivery {inode:0,type:'Start',load:'Truck',compileunit:'route'}) merge (a2:Delivery {inode:1, type:'Delivery', load:'pallet', compileunit:'route'}) merge (a3:Delivery {inode:2, type:'Delivery' , load:'pallet', compileunit:'route'}) merge (a4:Delivery {inode:3, type:'Delivery' , load:'pallet', compileunit:'route'}) merge (a1)-[:drives]-\>(a2) merge (a2)-[:drives]-\>(a3) merge (a2)-[:drives]-\>(a4…
