Skip to content

Graph: Clone a directed graph

Mani Bhushan edited this page Aug 14, 2016 · 16 revisions

Question:

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

High Level Approach:

Do a breadth first search of the input graph and while cloning individual graph nodes, check if you have already created that graph node or not. We can use a hashmap to keep track of already created graph nodes.

  • Time Complexity: O(V + E)
  • Space Complexity: O(V + E)

Working Solution:

CloneGraphDriver.java Send me a pull request if you feel this solution can be improved further.

Clone this wiki locally