Clone Graph
Given an adjacency list representation of an undirected graph, create a deep copy (clone) of the graph.
Input: adj_list is a list with one entry per node: the -th entry (counting from 1) is the list of node 's neighbour indices (nodes are 1-indexed). Return the cloned adjacency list in the same format: same neighbours in the same order, and fully independent of the input (modifying the clone must not change the original). An empty graph returns an empty list. The tests print each node's neighbours on one line, space-separated.
Example
2,4 1,3 2,4 1,3
2 4 1 3 2 4 1 3
- Input:
adj_listhas one entry per node; the -th entry (counting from 1) lists the labels of node 's neighbours. In the sample, nodes 1 and 3 each touch 2 and 4, while nodes 2 and 4 each touch 1 and 3. - The graph: the 4 edges , , , form a 4-cycle. The graph is undirected, so each edge appears from both ends: entries.
- Deep copy: the clone must describe exactly the same graph (same nodes, same neighbour labels, same order) but be fully independent of the original: adding node 3 to node 1's neighbours in the clone must leave the original's node 1 at .
- Output: the result prints as
2 4,1 3,2 4,1 3, identical to the input line for line. - Edge case: a graph with no nodes clones to an empty result, which the tests print as
EMPTY.
Constraints
- 1 <= number of nodes <= 100
- 0 <= neighbors per node <= 100