leetcode Ch7-Graph Search
1. Clone Graph
BFS:
class Solution {
public:
typedef UndirectedGraphNode UGNode;
UndirectedGraphNode* cloneGraph(UndirectedGraphNode* node) {
if (node == NULL) {
return NULL;
}
vector<UGNode*> nodes;
unordered_map<UGNode*, UGNode*> umap;
//clone nodes only
nodes.push_back(node);
umap[node] = new UGNode(node->label);
int start = ;
while (start < nodes.size()) {
UGNode* tmp = nodes[start];
start++;
for (int i = ; i < tmp->neighbors.size(); i++) {
if (umap.count(tmp->neighbors[i]) == ) {
nodes.push_back(tmp->neighbors[i]);
umap[tmp->neighbors[i]] = new UGNode(tmp->neighbors[i]->label);
}
}
}
//clone neighbors only
for (int i = ; i < nodes.size(); i++) {
UGNode* newNode = umap[nodes[i]];
for (int j = ; j < nodes[i]->neighbors.size(); j++) {
newNode->neighbors.push_back(umap[nodes[i]->neighbors[j]]);
}
}
return umap[node];
}
};
DFS:
class Solution {
public:
typedef UndirectedGraphNode UGNode;
UndirectedGraphNode* cloneGraph(UndirectedGraphNode* node) {
if (node == NULL) {
return NULL;
}
return dfs(node);
}
UGNode* dfs(UGNode* node) {
UGNode* p = new UGNode(node->label);
umap[p->label] = p;
for (int i = ; i < node->neighbors.size(); i++) {
if (umap.count(node->neighbors[i]->label) == ) {
p->neighbors.push_back(dfs(node->neighbors[i]));
} else {
p->neighbors.push_back(umap[node->neighbors[i]->label]);
}
}
return p;
}
private:
unordered_map<int, UGNode*> umap;
};
这里umap里的second存的都是copy后的图节点,不是原图。
图的邻接表DFS:
struct VertexNode {
int data;
EdgeNode* next;
};
struct EdgeNode {
int index; // 存储其对应的点的下标
EdgeNode* next;
};
struct Graph {
VertexNode vexs[MAXVEX];
int numVertex;
};
vector<bool> visited;
void dfs(Graph* g, int i) {
visited[i] = ;
print(g, i);
EdgeNode* p = g->vexs[i]->next;
while (p) {
if (!visited[p->index]) {
dfs(g, p->index);
}
p = p->next;
}
}
void dfsTraverse(Graph* g) {
for (int i = ; i < g->numVertex; i++) {
if (!visited[i]) {
dfs(g, i);
}
}
}
图的邻接表BFS:
void bfsTraverse(Graph* g) {
queue<int> q;
for (int i = ; i < numVertex; i++) {
if (!visited[i]) {
visited[i] = ;
print(g, i);
q.push(i);
while (!q.empty()) {
int j = q.top();
q.pop();
EdgeNode* p = g->vexs[j].next;
while (p) {
if (!visited[p->index]) {
visited[p->index] = ;
print(g, p->index);
q.push(p->index);
}
p = p->next;
}
}
}
}
}
leetcode Ch7-Graph Search的更多相关文章
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- Graph Search图谱搜索
来自百度百科的解释: Graph Search为2013年1月16日,Facebook首席执行官马克·扎克伯格(Mark Zuckerberg)在门罗帕克公司总部召开的新闻发布会上宣布推出社交搜索工具 ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- Android多媒体之照相机
1.调用系统的照相机 public void click(View view) { // 激活系统的照相机拍照 Intent intent = new Intent("android.med ...
- WPF中后台代码停止RepeatBehavior=RepeatBehavior.Forever的办法
1.在使用Begin()方法启动动画的时候,要将isControlable设置成true,就是Begin方法的第二个参数 scanningStoryBoard.Begin(this,true);// ...
- No result defined for action and result input
今天在编程的时候,我遇到了No result defined for action and result input的错误,这个错误想必大家都有遇到过吧,我今天发了很长时间弄这个错误,我以为我的Act ...
- 有关索引的DMV
转自:http://www.cnblogs.com/CareySon/archive/2012/05/17/2505981.html#commentform 有关索引的DMV 1.查看那些被大量更新, ...
- TLD(Tracking-Learning-Detection)一种目标跟踪算法
原文:http://blog.csdn.net/mysniper11/article/details/8726649 视频介绍网址:http://www.cvchina.info/2011/04/05 ...
- .Net平台技术介绍、C#语言
转载别人的 只是用做学习 一.什么是.Net平台? .Net平台是微软搭建的技术平台,技术人员在此平台上进行应用的搭建与开发.它提供了运行所必须的环境.NET Framework类库以及CLR(公共 ...
- MySQL---7、常用操作
1.表列的添加.修改和删除 2.视图 3.字符集和校对集 4.触发器相关知识
- 11、Map、可变参数、Collections
Map接口 Map集合概述 *A:Map集合概述: 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同 a:Collection中的集合,元素 ...
- Hibernate与mybatis比较
Hibernate与mybatis比较 1.先说底层: a)Jdbc:全称java数据库连接,是java语言用来规范客户端如何访问数据库的程序接口. b) 一般步骤: i.加载驱动程序 ii.获得数据 ...
- php中怎么导入自己写的类
如果写的类是写在当前php文件内,就直接实例化若你的类写在其他的php文件里,就要先用include或require,将类文件引入<?php include("class.php&qu ...