Clone Graph--leetcode

Source: Internet
Author: User

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         /          \_/

The main idea of the topic is given an image without a direction, there may be a ring, a method of realization, deep clone of this figure.

My approach is to use BFS, starting at a point, traversing its neighbors, then joining the queue, when the queue is non-empty, looping through, because the label is unique, using a map to save the newly generated node, using the label as key. In addition, the visited array is used to save the queue to avoid repeated traversal.

Talk is cheap>>

 PublicUndirectedgraphnode clonegraph (undirectedgraphnode root) {HashSet<Integer> visited =NewHashset<>(); if(root==NULL)            return NULL; List<UndirectedGraphNode> queue =NewArraylist<>(); HashMap<Integer,UndirectedGraphNode> map =NewHashmap<>();        Queue.add (root);  while(!Queue.isempty ()) {undirectedgraphnode node= Queue.get (0); if(Map.get (node.label) = =NULL) {map.put (Node.label,NewUndirectedgraphnode (Node.label)); } undirectedgraphnode tmp=Map.get (Node.label); Queue.remove (0);  for(inti = 0; I < node.neighbors.size (); i++) {                intKey =Node.neighbors.get (i). label; if(Map.get (key) = =NULL) {map.put (key,NewUndirectedgraphnode (key));                } tmp.neighbors.add (Map.get (key));                Visited.add (Node.label); if(!Visited.contains (Key))                    {Queue.add (Node.neighbors.get (i));                Visited.add (key); }            }        }        returnMap.get (Root.label); }

Clone Graph--leetcode

Contact Us

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.