###### leetcode:clone graph

Clone an undirected graph. Each node in the graph contains alabeland 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 by#.

1. First node is labeled as0. Connect node0to both nodes1and2.
2. Second node is labeled as1. Connect node1to node2.
3. Third node is labeled as2. Connect node2to node2(itself), thus forming a self-cycle.

Visually, the graph looks like the following:

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

1.
1 /**
2  * Definition for undirected graph.
3  * struct UndirectedGraphNode {
4  *     int label;
5  *     vector<UndirectedGraphNode *> neighbors;
6  *     UndirectedGraphNode(int x) : label(x) {};
7  * };
8  */
9 class Solution {
10 public:
11     UndirectedGraphNode* cloneGraph(UndirectedGraphNode *node)
12     {
13         if (node == NULL)
14         {
15             return NULL;
16         }
17
18         map<UndirectedGraphNode*, UndirectedGraphNode*> gphMap;
19         queue<UndirectedGraphNode*> gphQue;
20
21         //创建所有顶点
22         gphQue.push(node);
23         while (!gphQue.empty())
24         {
25             UndirectedGraphNode *tmp = gphQue.front();
26             gphQue.pop();
27
28             if (gphMap.find(tmp) == gphMap.end())
29             {
30                 UndirectedGraphNode *newNode = new UndirectedGraphNode(tmp->label);
31                 gphMap[tmp] = newNode;
32
33                 for (int i = 0; i != tmp->neighbors.size(); ++i)
34                 {
35                     gphQue.push(tmp->neighbors[i]);
36                 }
37             }
38         }
39
40         //调整顶点间关系
41         gphQue.push(node);
42         while (!gphQue.empty())
43         {
44             UndirectedGraphNode *tmp = gphQue.front();
45             gphQue.pop();
46
47             UndirectedGraphNode *exitNode = gphMap[tmp];
48             if (exitNode->neighbors.empty() && !tmp->neighbors.empty())
49             {
50                 for (int i = 0; i != tmp->neighbors.size(); ++i)
51                 {
52                     exitNode->neighbors.push_back(gphMap[tmp->neighbors[i]]);
53                     gphQue.push(tmp->neighbors[i]);
54                 }
55             }
56         }
57
58         return gphMap[node];
59     }
60 };
2.BFS实现广度优先搜索
1 class Solution {
2 public:
3     UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node)
4     {
5         if (node == NULL)
6         {
7             return NULL;
8         }
9
10         map<const UndirectedGraphNode*, UndirectedGraphNode*> gphMap;
11         queue<const UndirectedGraphNode *> gphQue;
12
13         gphQue.push(node);
14         gphMap[node] = new UndirectedGraphNode(node->label);
15         while (!gphQue.empty())
16         {
17             const UndirectedGraphNode *tmp = gphQue.front();
18             gphQue.pop();
19
20             for (int i = 0; i != tmp->neighbors.size(); ++i)
21             {
22                 if (gphMap.find(tmp->neighbors[i]) == gphMap.end())
23                 {
24                     //build the vertex
25                     UndirectedGraphNode *newNode = new UndirectedGraphNode(tmp->neighbors[i]->label);
26                     gphMap[tmp->neighbors[i]] = newNode;
28                     gphQue.push(tmp->neighbors[i]);
29                 }
30                 else
31                 {
32                     gphMap[tmp]->neighbors.push_back(gphMap[tmp->neighbors[i]]);
33                 }
34             }
35         }
36
37         return gphMap[node];
38     }
39 };
3.DFS深度优先搜索
1 class Solution {
2 public:
3     UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node)
4     {
5         if(node == NULL)
6         {
7             return NULL;
8         }
9
10         map<const UndirectedGraphNode*, UndirectedGraphNode*> gphMap;
11
12         return GphClone(node, gphMap); //or return gphMap[node]
13     }
14 private:
15     // DFS
16     static UndirectedGraphNode* GphClone(const UndirectedGraphNode *node,
17         map<const UndirectedGraphNode*, UndirectedGraphNode*> &gphMap)
18     {
19             // a copy already exists
20             if (gphMap.find(node) != gphMap.end())
21             {
22                 return gphMap[node];
23             }
24
25             UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
26             gphMap[node] = newNode;
27
28             for (int i = 0; i != node->neighbors.size(); ++i)
29             {
30                 newNode->neighbors.push_back(GphClone(node->neighbors[i], gphMap));
31             }
32
33             return newNode;
34     }
35 };

#### [leetcode] 133. Clone Graph 解题报告

2016-03-05 04:47:30

#### Clone Graph -- LeetCode

2014-04-01 09:53:23

#### *Leetcode_clone-graph(c++ version)

2014-05-17 15:58:33

#### [LeetCode][Java] Clone Graph

2015-08-01 17:20:50

#### [C++]LeetCode: 129 Clone Graph (图的深拷贝 BFS && DFS)

2015-01-28 16:52:54

#### [leetcode-133]Clone Graph（java）

2015-08-14 21:30:53

#### LeetCode | Clone Graph

2014-01-03 22:42:29

#### 【LeetCode】Clone Graph 解题报告

2014-09-11 21:08:40

#### Clone Graph 图的复制 @LeetCode

2013-12-23 05:52:36

#### 133. Clone Graph Leetcode Python

2015-02-26 11:04:54

## 不良信息举报

leetcode:clone graph