BFS和DFS解决LeetCode133. Clone Graph
来源:互联网 发布:淘宝客推广平台有哪些 编辑:程序博客网 时间:2024/06/05 04:05
133 Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
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
#
.First node is labeled as
0
. Connect node0
to both nodes1
and2
.
Second node is labeled as1
. Connect node1
to node2
.
Third node is labeled as2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:1 / \ / \0 --- 2 / \ \_/
题目虽然讲了很多内容,包括OJ中序列化的图的表示法,但是题目所要求的复制一个图,实际上只是考察了图的遍历。根据给定的一个UndirectedGraphNode* node
,由该点出发,遍历整个图,并以此创建一个新的和原来一样的无向图即可。
图的遍历可以用DFS和BFS实现。这题唯一需要注意的地方在于,在创建新图时,要确认新建结点的“时机”以及连接节点的方式,不要重复new
同一个结点。例如,假设给定的图为:
0 -- 1
使用DFS,先访问0
,然后访问0
的临接结点1
,而在访问了1
之后,要准确地将先前创建的新的0
结点放入1
的neighbors
中,否则会导致图的结构错误。这里,我采用了map<int, UndirectedGraphNode*>
来解决。既可以保证不重复创建结点,又能根据label
准确找回原来创建的结点。
代码实现如下:
DFS:
// DFS versionclass Solution {public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) { return NULL; } node_map[node->label] = new UndirectedGraphNode(node->label); dfs(node); return node_map[node->label]; }private: map<int, UndirectedGraphNode*> node_map; void dfs(UndirectedGraphNode* &node) { for (int i = 0; i < node->neighbors.size(); i++) { UndirectedGraphNode* neighbor = node->neighbors[i]; if (node_map[neighbor->label] == NULL) { node_map[neighbor->label] = new UndirectedGraphNode(neighbor->label); dfs(neighbor); } node_map[node->label]->neighbors.push_back(node_map[neighbor->label]); } }};
BFS:
// BFS version:class Solution {public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) { return NULL; } map<int, UndirectedGraphNode*> node_map; node_map[node->label] = new UndirectedGraphNode(node->label); queue<UndirectedGraphNode*> que; que.push(node); while (!que.empty()) { UndirectedGraphNode* temp = que.front(); que.pop(); for (int i = 0; i < temp->neighbors.size(); i++) { UndirectedGraphNode* neighbor = temp->neighbors[i]; if (node_map[neighbor->label] == NULL) { node_map[neighbor->label] = new UndirectedGraphNode(neighbor->label); que.push(neighbor); } node_map[temp->label]->neighbors.push_back(node_map[neighbor->label]); } } return node_map[node->label]; }};
- BFS和DFS解决LeetCode133. Clone Graph
- LeetCode133. Clone Graph
- LeetCode133—Clone Graph
- leetcode133. Clone Graph
- LeetCode133 Clone Graph
- LeetCode133.Clone Graph
- Clone Graph BFS & DFS
- Clone Graph [leetcode] dfs和bfs
- LeetCode133——Clone Graph
- [LeetCode] Clone Graph(!!!!graph&dfs&bfs)
- Clone Graph DFS&BFS 图的复制
- LeetCode 133 Clone Graph (BFS || DFS)
- leetcode -- Clone Graph -- deep copy问题,dfs,bfs
- Leetcode 133. Clone Graph用BFS、DFS两种解法
- Clone Graph,bfs,map
- Clone Graph BFS
- [leetcode] 133 clone graph bfs
- DFS&&BFS Search Graph
- canvas save()和canvas restore()状态的保存和恢复使用方法及实例
- 十月培训
- Android学习《第一行代码》01
- 锁小结
- RecyclerView的基本使用
- BFS和DFS解决LeetCode133. Clone Graph
- C#字符串连接消耗
- JDBC连接 防止注入 事件 回滚
- hibernate setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
- 佛祖保佑 永无BUG
- jquery几种加载方式
- [Machine Learning & Algorithm] 随机森林(Random Forest)
- java 2048游戏设计初体验
- PAT乙级1031. 查验身份证(15)