A directed acyclic graph (DAG) is a graph which doesn’t contain a cycle and has directed edges. We are given a DAG, we need to clone it, i.e., create another graph that has copy of its vertices and edges connecting them.
Input : 0 - - - > 1 - - - -> 4 | / \ ^ | / \ | | / \ | | / \ | | / \ | | / \ | v v v | 2 - - - - - - - - -> 3 Output : Printing the output of the cloned graph gives: 0-1 1-2 2-3 3-4 1-3 1-4 0-2
To clone a DAG without storing the graph itself within a hash (or dictionary in Python). To clone, it we basically do a depth-first traversal of the nodes, taking original node’s value and initializing new neighboring nodes with the same value, recursively doing, till the original graph is fully traversed. Below is the recursive approach to cloning a DAG (in Python). We make use of dynamic lists in Python, append operation to this list happens in constant time, hence, fast and efficient initialization of the graph.
Graph Before Cloning:- edge 0x7fa03dd43878-0x7fa03dd43908:0-1 edge 0x7fa03dd43908-0x7fa03dd43950:1-2 edge 0x7fa03dd43950-0x7fa03dd43998:2-3 edge 0x7fa03dd43998-0x7fa03dd439e0:3-4 edge 0x7fa03dd43908-0x7fa03dd43998:1-3 edge 0x7fa03dd43908-0x7fa03dd439e0:1-4 edge 0x7fa03dd43878-0x7fa03dd43950:0-2 Cloning Process Starts Cloning Process Completes. Graph After Cloning:- edge 0x7fa03dd43a28-0x7fa03dd43a70:0-1 edge 0x7fa03dd43a70-0x7fa03dd43ab8:1-2 edge 0x7fa03dd43ab8-0x7fa03dd43b00:2-3 edge 0x7fa03dd43b00-0x7fa03dd43b48:3-4 edge 0x7fa03dd43a70-0x7fa03dd43b90:1-3 edge 0x7fa03dd43a70-0x7fa03dd43bd8:1-4 edge 0x7fa03dd43a28-0x7fa03dd43c20:0-2
Creating the DAG by appending adjacent edges to the vertex happens in O(1) time. Cloning of the graph takes O(E+V) time.
This article is contributed by Raveena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph
- Assign directions to edges so that the directed graph remains acyclic
- All Topological Sorts of a Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Longest path in a directed Acyclic graph | Dynamic Programming
- Minimum time taken by each job to be completed given by a Directed Acyclic Graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Calculate number of nodes between two vertices in an acyclic Graph by DFS method
- Count permutations of all integers upto N that can form an acyclic graph based on given conditions
- Count ways to change direction of edges such that graph becomes acyclic
- Clone an Undirected Graph
- Clone an undirected graph with multiple connected components
- Find if there is a path between two vertices in a directed graph
- Detect Cycle in a Directed Graph
- Shortest path with exactly k edges in a directed and weighted graph