Question
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
Nodes are labeled uniquely.
We use #
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
Review
This is a classic question which can be solved by DFS and BFS.
Review for DFS and BFS on Graph.
Solution 1 -- BFS
We can use BFS to traverse original graph, and create new graph.
1. Classic way to implement BFS is by queue. There is another class in Java, LinkedList, which has both list and deque's interface. It can also act as queue.
2. We also need to record whether a node in original graph has been visited. When a node in original graph was visited, it means that a node in new graph was created. Therefore, we create a map to record.
Time complexity O(n), space cost O(n)
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null)
return null;
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
// Create a queue for BFS
LinkedList<UndirectedGraphNode> ll = new LinkedList<UndirectedGraphNode>();
ll.add(node);
// Create a map to record visited nodes
Map<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
hm.put(node, newHead);
while (ll.size() > 0) {
UndirectedGraphNode tmpNode = ll.remove();
UndirectedGraphNode currentNode = hm.get(tmpNode);
List<UndirectedGraphNode> neighbors = tmpNode.neighbors;
for (UndirectedGraphNode neighbor : neighbors) {
if (!hm.containsKey(neighbor)) {
// If the neighbor node is not visited, add it to the list, hashmap and current node's neighbor
UndirectedGraphNode copy = new UndirectedGraphNode(neighbor.label);
currentNode.neighbors.add(copy);
ll.add(neighbor);
hm.put(neighbor, copy);
} else {
// If the neighbor node is already visited, just add it to current node's neighbor
UndirectedGraphNode copy = hm.get(neighbor);
currentNode.neighbors.add(copy);
}
}
}
return newHead;
}
}
Solution 2 -- DFS
Usually, we use recursion to implement DFS. We also need a map to record whether a node has been visited. Time complexity O(n), space cost O(n).
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null)
return null;
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
Map<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
hm.put(node, newHead);
DFS(hm, node);
return newHead;
} private void DFS(Map<UndirectedGraphNode, UndirectedGraphNode> hm, UndirectedGraphNode node) {
UndirectedGraphNode currentNode = hm.get(node);
List<UndirectedGraphNode> neighbors = node.neighbors;
for (UndirectedGraphNode neighbor : neighbors) {
if (!hm.containsKey(neighbor)) {
UndirectedGraphNode copy = new UndirectedGraphNode(neighbor.label);
currentNode.neighbors.add(copy);
hm.put(neighbor, copy);
DFS(hm, neighbor);
} else {
UndirectedGraphNode copy = hm.get(neighbor);
currentNode.neighbors.add(copy);
}
}
}
}