[Lintcode] Clone Graph

Source: Internet
Author: User
Tags lintcode

Clone Graph

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

OJ ' s undirected graph serialization:

Nodes is 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 `#` .

1. First node is labeled as `0` . Connect node to `0` both nodes `1` and `2` .
2. Second node is labeled as `1` . Connect node to `1` node `2` .
3. Third node is labeled as `2` . Connect node `2` to Node `2` (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

`       1      /      /       0---2         /          \_/`

Solution:

Thinking on this topic can be divided into two parts 1, traversing the figure 2,clone graph.

Traversal needs to use the BFS, the basic map of the search is BFS, then that is to use the queue, the first node into the queue, and then pop access to all of his neighbors, the neighbor not visited into the queue, in turn, pop repeats the above operation, In this process, the neighbor into the stack is stored in the HashMap, in order to confirm in the future when the stack is in the queue, to ensure that only the point into the queue is not traversed. In this problem, it is no problem to simulate a queue with a pointer +arraylist.

Clone, is to do deep copy. Using NewNode as a pointer to the Copynode in a map, again traversing node in the container, the original point of the neighbors in the map map (BFS will be cloned to) added to the neighbors of the new NewNode.

The map of node in the last return map is available.

` Public classSolution {/**     * @paramnode:a undirected Graph node *@return: A undirected graph node*/     Publicundirectedgraphnode Clonegraph (Undirectedgraphnode node) {if(node = =NULL){            return NULL; }        //because I need the collection 2 times, so I can ' t use queue, then I choice ArrayList and a pointer (start)arraylist<undirectedgraphnode> nodes =NewArraylist<undirectedgraphnode>(); HashMap<undirectedgraphnode, undirectedgraphnode> map =NewHashmap<undirectedgraphnode, undirectedgraphnode>();        Nodes.Add (node); Map.put (node,NewUndirectedgraphnode (Node.label)); intStart = 0; //Clone Nodes         while(Start <nodes.size ()) {Undirectedgraphnode cur= Nodes.get (start++);  for(inti = 0; I < cur.neighbors.size (); i++) {Undirectedgraphnode neighbor=Cur.neighbors.get (i); if(!Map.containskey (neighbor)) {Map.put (neighbor,NewUndirectedgraphnode (Neighbor.label));                Nodes.Add (neighbor); }            }        }        //Clone Neighbors         for(inti = 0; I < nodes.size (); i++) {Undirectedgraphnode NewNode=Map.get (Nodes.get (i));  for(intj = 0; J < Nodes.get (i). Neighbors.size (); J + +) {NewNode.neighbors.add (Map.get (Nodes.get (i). Neighbors.get (j))); }        }        returnmap.get (node); }}`
View Code

[Lintcode] Clone Graph

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.