Leetcode clone Graph

Source: Internet
Author: User
Clone graph total accepted: 16482 total submissions: 72324my submissions

Clone an undirected graph. each node in the graph containslabelAnd a list of itsneighbors.


OJ's undirected graph serialization:

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#.

  1. First node is labeled0. Connect Node0To both nodes1And2.
  2. Second node is labeled1. Connect Node1To Node2.
  3. Third node is labeled2. Connect Node2To Node2(Itself), thus forming a self-cycle.

Visually, the graph looks like the following:

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

Discuss

// First, this type of deep copy question requires an auxiliary tool to determine whether the nodes are duplicated. This is a copy of the graph. All nodes of the graph are traversed according to a certain rule.

// I use map to determine whether the nodes are repeated, and then traverse the source Image Based on the breadth first. Then, the nodes of the new graph are updated one by one and connected accordingly.


<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;">/**</span>
 * Definition for undirected graph. * struct UndirectedGraphNode { *     int label; *     vector<UndirectedGraphNode *> neighbors; *     UndirectedGraphNode(int x) : label(x) {}; * }; */ //7:35->class Solution {public:    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {        map<UndirectedGraphNode *,UndirectedGraphNode *> repeat;        // one set and two queue        // store the original nodes and new nodes;que is for old graph. que2 is for new graph.        queue<UndirectedGraphNode *> que;        queue<UndirectedGraphNode *> que2;        UndirectedGraphNode *head=NULL,*h1=NULL,*h2=NULL,*h3=NULL;        int i=0,j=0;        if(node==NULL)        {            return head;        }        que.push(node);        h1=head = new UndirectedGraphNode(node->label);        que2.push(h1);        repeat[node] = h1;        // begin bfs        while(que.size()>0)        {            node = que.front();            que.pop();            h1 = que2.front();            que2.pop();            for(i=0;i<node->neighbors.size();i++)            {                h2 = node->neighbors[i];                if(repeat.count(h2)==0)                {                    h3 = new UndirectedGraphNode(h2->label);                    h1->neighbors.push_back(h3);                    repeat[h2] = h3;                    que.push(h2);                    que2.push(h3);                }else                {
<span style="white-space:pre"></span>//add the new link in new graph                    h1->neighbors.push_back(repeat[h2]);                }            }        }        return head;    }};


Leetcode clone Graph

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.