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 };




Smart SVN Revision Graph页面查看分支的合并信息

一直使用Tortoise SVN客户端查看SVN上的代码的修改日志,分支情况等信息,在开发过程中拉分支,开发新功能是免不了的,拉分支自然需要合并回主干,在Tortoise SVN的Revision G...
  • luojinbai
  • luojinbai
  • 2017年02月07日 18:58
  • 1649

PB上的 graph 控件使用

要做成这样的饼式占比图很简单,首先拖到jie
  • samrtian
  • samrtian
  • 2014年07月05日 15:09
  • 1426

【算法导论-37】Graph的Java实现

前言之前的博客“【算法导论-35】图算法JGraphT开源库介绍”中提到的开源版本的图算法。然而,继续《算法导论》的学习必须自己实现Graph。所以,放弃使用该库,实现自己的Graph类。 注意,本...
  • BrilliantEagle
  • BrilliantEagle
  • 2016年09月09日 20:11
  • 2932

LeetCode:Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ...
  • fytain
  • fytain
  • 2013年09月30日 12:25
  • 2605

leetcode题目:Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neig...
  • woailiyin
  • woailiyin
  • 2014年05月09日 15:19
  • 588

Leetcode 133. Clone Graph & 138. Copy List with Random Pointer

因为自己是先做的138,先说138。 138. Copy List with Random Pointer Total Accepted: 64859 Total Submissions:...
  • fantasiasango
  • fantasiasango
  • 2016年05月23日 05:37
  • 462

LeetCode——133.Clone Graph && 621.Task Scheduler

本周两道题,一道是图的题,一道是贪心的题。 Clone Graph 1.问题描述 克隆一个无向图,已知节点的形式,是一个结构体,有一个label和一个邻接表,表示所有邻居节点。节点的label值...
  • parishong
  • parishong
  • 2017年06月25日 15:52
  • 91

LeetCode133 Clone Graph

详细见:leetcode.com/problems/clone-graph Java Solution: github package leetcode; import java.u...
  • zxwtry
  • zxwtry
  • 2017年05月15日 22:28
  • 136

leetcode133. Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ’s ...
  • stone_yw
  • stone_yw
  • 2017年03月23日 16:07
  • 145

Clone Graph Leetcode 133

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ’s ...
  • majesty_
  • majesty_
  • 2015年06月14日 22:50
  • 176
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:clone graph
举报原因:
原因补充:

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