leetcode:clone graph

转载 2016年05月31日 16:22:16


题目描述:

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
         / \
         \_/
感觉题目很晦涩,没太懂,最后就理解成:就是遍历一个给定的树完全复制建立一个我们自己的树。相当于c++里面的深度复制。
实现:
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;
27                     gphMap[tmp]->neighbors.push_back(newNode);            //Adjust the Vertex    
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 19 Clone Graph

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

LeetCode_Clone Graph

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

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着...

算法设计Week3 LeetCode Algorithms Problem #133 Clone Graph

题目描述: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbo...

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

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbor...

Clone Graph -- leetcode

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

LeetCode133. Clone Graph

题目: https://leetcode.com/problems/clone-graph/ Clone an undirected graph. Each node in the graph c...
  • codeTZ
  • codeTZ
  • 2016年03月22日 20:04
  • 202

[Leetcode]Clone Graph

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

LeetCode: Clone Graph [133]

【题目】 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors...

【leetcode】Clone Graph

问题 给定一个无向图,对该无向图进行拷贝,图中每个结点包含一个label 域和一个用来记录相临节点的vector容器。假设label是唯一的。 分析 在这之前,我们回忆图的遍历算法,宽度优先(BFS)...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:clone graph
举报原因:
原因补充:

(最多只允许输入30个字)