Judge loop in directed graph
1 深度优先方法
首先需要更改矩阵初始化函数init_graph()
然后我们需要初始化vist标记数组
深度优先访问图,然后根据是否存在back edge判断是否存在环路
算法如下:
#include <iostream>
using namespace std; #define MAX_VERTEX_NUM 128
enum color{WHITE, GRAY = 1, BLACK};
bool M[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int colour[MAX_VERTEX_NUM];
int dfsNum[MAX_VERTEX_NUM], num;
int indegree[MAX_VERTEX_NUM];
int vexnum, edgenum;
bool loop; void init_graph(){
cout<<"enter vertex number:"<<endl;
cin>>vexnum;
cout<<"enter edge number:"<<endl;
cin>>edgenum; int i, j;
while(edgenum){
cout<<"add new edge:"<<endl;
cin>>i>>j;
M[i - 1][j - 1] = true;
//initialize in vertex degree
indegree[i - 1]++;
indegree[j - 1]++;
edgenum--;
}
} void dfs(int u, int p){
colour[u] = GRAY;
dfsNum[u] = num++;
cout<<"old relation:"<<endl;
cout<<"child: "<<u + 1<<" parent :"<<p + 1<<endl;
for( int v = 0; v < vexnum; v++){
if(M[u][v] && v != p){
if(colour[v] == WHITE){
cout<<"new relation:"<<endl;
cout<<"parent "<<u + 1<<"(color: "<<colour[u]<<")"
<<"and child "<<v + 1<<"(color: "<<colour[v]<<")"<<endl;
dfs(v, u);
cout<<"parent "<<u + 1<<"(color: "<<colour[u]<<")"
<<"and child "<<v + 1<<"(color: "<<colour[v]<<")"<<endl;
}
else if(colour[v] == GRAY){
cout<<"back edge between"<<u + 1<<" and "<<v + 1<<endl;
loop = true;
// break;
}
else if(colour[v] == BLACK){
cout<<"cross edge between"<<u + 1<<" and"<<v + 1<<endl;;
loop = true;
// break;
}
}
}
colour[u] = BLACK;
}
void print_dfs_num(){
for(int v = 0; v < vexnum; v++)
cout<<dfsNum[v]<<" ";
} int main()
{
init_graph();
dfs(0, -1);
print_dfs_num();
cout<<endl;
if(loop)
cout<<"There is loop in graph!"<<endl; int ch;
cin>>ch;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Judge loop in directed graph的更多相关文章
- judge loop in undirected graph
一 深度优先遍历,参考前面DFS(white and gray and black) 二 根据定点以及边数目进行判断 如果m(edge)大于n(vertex),那么肯定存在环 算法如下: 1 删除所有 ...
- Directed Graph Loop detection and if not have, path to print all path.
这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...
- [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nod ...
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- dataStructure@ Find if there is a path between two vertices in a directed graph
Given a Directed Graph and two vertices in it, check whether there is a path from the first given ve ...
- CodeChef Counting on a directed graph
Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...
- Geeks - Detect Cycle in a Directed Graph 推断图是否有环
Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...
- Skeleton-Based Action Recognition with Directed Graph Neural Network
Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...
- Find the Weak Connected Component in the Directed Graph
Description Find the number Weak Connected Component in the directed graph. Each node in the graph c ...
随机推荐
- 动态更换view类的背景----StateListDrawable的应用
StateListDrawable可以根据View的不同状态,更换不同的背景 可以应用如EditText,Button等中,以Button为例 系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种 ...
- 多版本jQuery的使用剖析
</div> </div> <!-- basic scripts --> <!--[if !IE]> --> <!-- <![endi ...
- 标准C函数库的使用方法
本篇介绍若干经常使用的标准C函数的使用方法,主要介绍stdio(标准输入输出).math(数字函数库).time(时间函数库).stdlib(标准函数库)string(标准字符串函数)等. 最后更新 ...
- CSS换行2
1.可以使用强制换行符号<br />换行.如果在一个文章里可以在文章需要换行的地方加入<br />即可实现自动换行-常说的小换行,与换行前没有间隔.实例如下图 换行说明图无间隔 ...
- VS2015自定义注释内容
一直想自动添加一些注释信息,找了好多种方式:各种插件什么的,最后偶然发现可以修改vs的模板可以做到,下面介绍如何改 首先找到vs的安装目录,如下是我的安装目录: D:\Program Files\VS ...
- ViewState是什么
在做ASP.NET的时候遇到ViewState,当时不知道他是什么意思. 就在当前页面中保存数据的. 像session.是会话级别的.只要会话没有过期.session中存的数据就在. viewstat ...
- GDI+ 对象释放崩溃的问题
确保在Gdiplus::GdiplusShutdown(m_gdiplusToken); 之前delete 掉GDI+的对象,例如:delete *pBitmap; 如果先Gdiplus::Gdipl ...
- 简单的UIScrollView 下拉刷新
这里只贴主要代码 #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate&g ...
- PHP新手必须掌握的入门与实战技巧
作为当今主流的开发语言,PHP集简单.免费.高效等特点于一身.对于想加入PHP大军的新手来说,从何学起.如何学习? 你需要掌握PHP的基础知识.常用功能模块.面向对象.MVC等相关技能.学会了这些技能 ...
- IE 弹出框处理经验
//各屏幕弹出窗样式 // 1366*768var style_1366x768 = "dialogWidth:950px;dialogHeight:650px;help:no;center ...