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的更多相关文章

  1. 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 ...

  2. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  3. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  4. Graph Search图谱搜索

    来自百度百科的解释: Graph Search为2013年1月16日,Facebook首席执行官马克·扎克伯格(Mark Zuckerberg)在门罗帕克公司总部召开的新闻发布会上宣布推出社交搜索工具 ...

  5. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

  10. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. github里如何删除一个repository仓库

    高手请绕行,新手往下走. 作为一个刚接触github(https://github.com/)的新手,除了感叹开源的丰富和强大之外,自己肯定也想试用一下,因此申请帐号神马的.今天自己创建一个Repos ...

  2. SpringMVC 过滤器

    参考: http://qq-22530757.iteye.com/blog/2177513 http://www.jdon.com/dl/best/spring-security.html https ...

  3. ubuntu apache2 .htaccess 下配置 反向代理

    安装完apache2后, a2enmod rewrite //启用.htaccess规则 a2enmod proxy a2enmod proxy_http //启用反向代理支持 [P] 配置OK,就可 ...

  4. Android多媒体之照相机

    1.调用系统的照相机 public void click(View view) { // 激活系统的照相机拍照 Intent intent = new Intent("android.med ...

  5. Modular Rails: The complete Guide to Modular Rails Applications 笔记

    fix SamuraiCRM/engines/core/test/dummy/config/routes 修改如下 Rails.application.routes.draw do mount Sam ...

  6. WPF 使用依赖属性自定义控件

    使用依赖属性自定义控件,依赖属性必须定义在自定义控件中,不能定义在其他文件中 一.先实现一个类继承你要复写的类 using System; using System.Collections.Gener ...

  7. Linux常用命令之sed(2)

    Sed SED的英文全称是 Stream EDitor,它是一个简单而强大的文本解析转换工具,在1973-1974年期间由贝尔实验室的Lee E. McMahon开发,今天,它已经运行在所有的主流操作 ...

  8. 13 Timer和TimerTask

    下面内容转载自:http://blog.csdn.net/xieyuooo/article/details/8607220 其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了ru ...

  9. 09 jdk1.5的并发容器:CopyOnWriteArrayList(转载)

    原文链接:http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略. 其基本思路是,从一开始大家都在共享同一个内容 ...

  10. [CPP] Object Based Class

    前言 几年前接触到一款很好玩的RPG游戏,叫作CPP.最近想着怀念一下,又不想干巴巴地去玩.于是乎,我打算写几篇攻略,主要是记录一下游戏中一些奇妙的点.游戏的第一章是面向对象程序设计,其中又分为基于对 ...