标签:
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 #
.
0
. Connect node 0
to both nodes 1
and 2
.1
. Connect node 1
to node 2
.2
. Connect node 2
to node 2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / / 0 --- 2 / \_/
Depth-first Search Breadth-first Search Graph
这里我们使用Map来进行区分,Map的key值为原来的node,value为新clone的node,当发现一个node未在map中时说明这个node还未被clone,
将它clone后放入queue中处理neighbors。
使用Map的主要意义在于充当BFS中Visited数组,它也可以去环问题,例如A--B有条边,当处理完A的邻居node,然后处理B节点邻居node时发现A已经处理过了
处理就结束,不会出现死循环!
/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector<UndirectedGraphNode *> neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */ class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if(node == NULL) return NULL; map <UndirectedGraphNode*, UndirectedGraphNode*> old2New; queue <UndirectedGraphNode*> q; UndirectedGraphNode* oldNode = NULL; // each node in queue is already copied itself // but neighbors are not copied yet old2New[node] = new UndirectedGraphNode(node->label); q.push(node); while(!q.empty()) { oldNode = q.front(); q.pop(); for(int i = 0; i < oldNode->neighbors.size(); i++) { if(oldNode->neighbors[i] != NULL) { if(old2New.count(oldNode->neighbors[i]) != 0) { old2New[oldNode]->neighbors.push_back(old2New[oldNode->neighbors[i]]); } else { UndirectedGraphNode* tmp = new UndirectedGraphNode(oldNode->neighbors[i]->label); old2New[oldNode->neighbors[i]] = tmp;//build the mapping old2New[oldNode]->neighbors.push_back(tmp); // add it to queue after copy it if the node doesn‘t the map q.push(oldNode->neighbors[i]); } } } } return old2New[node]; } };
标签:
原文地址:http://www.cnblogs.com/diegodu/p/4554189.html