博客
关于我
CLRS 22.3-2,7 邻接表实现
阅读量:299 次
发布时间:2019-03-03

本文共 6889 字,大约阅读时间需要 22 分钟。

// stdafx.h : 标准系统包含文件的包含文件,// 或是经常使用但不常更改的// 特定于项目的包含文件//#pragma once#include "targetver.h"#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// TODO: 在此处引用程序需要的其他头文件
// aiqiyi.cpp: 定义控制台应用程序的入口点。//#include "stdafx.h"using namespace std;int times = 0;class Node{public:	int val;	Node* parent;	vector
next; int color; int start_time; int end_time; Node(int val_char, Node* par, vector
nex, int col, int sta_t, int end_t) : val(val_char), parent(par), next(nex), color(col), start_time(sta_t), end_time(end_t) {}};void DFS_VISIT(vector
>& node_next, vector
& node_set, int target){ times++; node_set[target]->start_time = times; node_set[target]->color = 1; for (int i = 0; i < node_next[target].size(); ++i) { if ((node_next[target][i])->color == 0) { (node_next[target][i])->parent = node_set[target]; DFS_VISIT(node_next, node_set, node_next[target][i]->val-'q'); } } times++; node_set[target]->end_time = times;}void DFS(vector
>& node_next, vector
& node_set){ for (int i = 0; i < 10; ++i) { if ((node_set[i])->color == 0) DFS_VISIT(node_next, node_set, i); }}int main(){ vector
>node_next(10,vector
(0)); vector
node_set; for (int i = 0; i < 10; ++i) { auto temp = new Node('q' + i, NULL, node_next[i], 0, 0, 0); node_set.push_back(temp); } (node_next[0]).push_back(node_set[2]); (node_next[0]).push_back(node_set[3]); (node_next[0]).push_back(node_set[6]); (node_next[1]).push_back(node_set[4]); (node_next[1]).push_back(node_set[8]); (node_next[2]).push_back(node_set[5]); (node_next[3]).push_back(node_set[7]); (node_next[3]).push_back(node_set[8]); (node_next[4]).push_back(node_set[8]); (node_next[5]).push_back(node_set[6]); node_next[6].push_back(node_set[2]); (node_next[7]).push_back(node_set[9]); (node_next[8]).push_back(node_set[0]); (node_next[9]).push_back(node_set[7]); DFS(node_next,node_set); for (int i = 0; i < 10; i++) cout << node_set[i]->start_time<<" "<
end_time << endl; return 0;}
void DFS_noncursion(vector
>& node_next, vector
& node_set){ stack
node_stack; node_stack.push(node_set[0]); int iter = 0; while (iter < 10) { if (node_stack.empty()) { while (iter < 10 && node_set[iter]->color) ++iter; if (iter >= 10) break; node_stack.push(node_set[iter]); } else { while (!node_stack.empty()) { auto top_rank = (node_stack.top())->val; if (node_set[top_rank - 'q']->color == 0) { times++; node_set[top_rank - 'q']->start_time = times; node_set[top_rank - 'q']->color = 1; } int sign = 1; for (int j = 0; j < node_next[top_rank - 'q'].size(); ++j) { if (node_next[top_rank - 'q'][j]->color == 0) { cout << char(node_next[top_rank - 'q'][j]->val) << endl; node_next[top_rank - 'q'][j]->parent = node_set[top_rank - 'q']; node_stack.push(node_next[top_rank - 'q'][j]); sign = 0; break; } } if (sign) { times++; node_set[top_rank - 'q']->end_time = times; node_stack.pop(); } } } }}

以下为尝试用CLRS上的DFS与拓扑排序检查有向图中是否存在环。

// aiqiyi.cpp: 定义控制台应用程序的入口点。  //  #include "stdafx.h"  using namespace std;static int times = 0;static bool sign = 0;class Node{public:	int val;	int parent;	vector
next; int color; int start_time; int end_time; Node(int val_char, int par, vector
nex, int col, int sta_t, int end_t) : val(val_char), parent(par), next(nex), color(col), start_time(sta_t), end_time(end_t) {}};int find_index(vector
& node_set, int targets){ for (int ind = 0; ind < node_set.size(); ++ind) { if (node_set[ind]->val == targets) return ind; }}int DFS_VISIT(vector
> node_next, vector
node_set, int targets)//targets is the value of node!!{ times++; int target = find_index(node_set, targets); node_set[target]->start_time = times; node_set[target]->color = 1; for (int i = 0; i < node_next[target].size(); ++i) { if ((node_next[target][i])->color == 0) { if (sign) return 1; (node_next[target][i])->parent = node_set[target]->val; DFS_VISIT(node_next, node_set, node_next[target][i]->val); } } times++; node_set[target]->end_time = times; return 0;}int DFS(vector
> node_next, vector
node_set){ for (int i = 0; i < node_set.size(); ++i) { if ((node_set[i])->color == 0) { if (DFS_VISIT(node_next, node_set, node_set[i]->val)) return 1; } } return 0;}static const bool Less(const int &a, const int & b){ return a >= b;}bool canFinish(int numCourses, vector
>& prerequisites){ vector
>node_next(numCourses, vector
(0)); vector
node_set; vector
>node_next_T(numCourses, vector
(0)); vector
node_set_T; for (int i = 0; i < numCourses; ++i) { auto temp = new Node(i, 0, node_next[i], 0, 0, 0); auto temp_T = new Node(i, 0, node_next_T[i], 0, 0, 0);; node_set.push_back(temp); node_set_T.push_back(temp_T); } int pair_len = prerequisites.size(); for (int i = 0; i < pair_len; ++i)//to generate the graph { node_next[prerequisites[i].first].push_back(node_set[prerequisites[i].second]); node_next_T[prerequisites[i].second].push_back(node_set_T[prerequisites[i].first]); } for (int i = 0; i < numCourses; ++i) { node_set[i]->next = node_next[i]; node_set_T[i]->next = node_next_T[i]; } //we need to check if it's an DAG first--to check if there exists a strongly-connectedly-component DFS(node_next,node_set); //to record the finish time of each node_visit in the first round of DFS;the info is useful for the next round DFS vector
fin_time; for (int i = 0; i < numCourses; ++i) fin_time.push_back(node_set[i]->end_time); //release memory!!! vector
().swap(node_set); vector
>().swap(node_next); //to compare the vector before and after sort, then you'll know the rank of numbers in the vector //here maybe you can use priority queue vector
fin_time_I = fin_time; sort(fin_time.begin(), fin_time.end(),Less); vector
index; for (int i = 0; i < numCourses; ++i) { index.push_back(find(fin_time_I.begin(),fin_time_I.end(),fin_time[i])-fin_time_I.begin()); } //sort node_set_T and node_next_T in the visiting order vector
>node_next_R; vector
node_set_R; for (int i = 0; i < numCourses; ++i) { node_set_R.push_back(node_set_T[index[i]]); node_next_R.push_back(node_next_T[index[i]]); } sign = 1; times = 0; int res = !DFS(node_next_R, node_set_R); //for (int i = 0; i < numCourses; ++i) //{ // cout <<"node_num:"<< node_set_R[i]->val<<" sta:"<< node_set_R[i]->start_time<<" end:"<
end_time << endl; //} for (int i = 0; i < numCourses; ++i) delete node_set_T[i]; vector
().swap(node_set_T); vector
().swap(node_set_R); vector
>().swap(node_next_T); vector
>().swap(node_next_R); return res;}int main(){ vector
> pre; //pre.push_back({ 2,1 }); pre.push_back({ 1,0 }); //pre.push_back({ 0,2 }); cout<

转载地址:http://frsm.baihongyu.com/

你可能感兴趣的文章
nginx配置域名和ip同时访问、开放多端口
查看>>
Nginx配置好ssl,但$_SERVER[‘HTTPS‘]取不到值
查看>>
Nginx配置如何一键生成
查看>>
Nginx配置实例-负载均衡实例:平均访问多台服务器
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Nginx配置负载均衡到后台网关集群
查看>>
ngrok | 内网穿透,支持 HTTPS、国内访问、静态域名
查看>>
NHibernate学习[1]
查看>>
NHibernate异常:No persister for的解决办法
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
查看>>
NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表或全表增量同步_实现指定整库同步_或指定数据表同步配置_04---大数据之Nifi工作笔记0056
查看>>
NIFI1.23.2_最新版_性能优化通用_技巧积累_使用NIFI表达式过滤表_随时更新---大数据之Nifi工作笔记0063
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现数据实时delete同步_实际操作04---大数据之Nifi工作笔记0043
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>