博客
关于我
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/

你可能感兴趣的文章
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>