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 ...
随机推荐
- 原生javascript实现ajax,post参数
var json = { userid: userid, cid: cid, openid: openid, type: 1 }; // 原生ajax json = (function(obj){ / ...
- Zoie Merge Policy
Zoie有一个ZoieMergePolicy如若价格值不是特别的.这是为lucene早期的版本号merge在不考虑删除doc会计并加以改进,和LogMergePolicy只是做同样的也合并相邻节段,而 ...
- SMACSS:一个关于CSS的最佳实践-2.Base Rules
回顾 在上一篇SMACSS:一个关于CSS的最佳实践-Overview中,讲到SMACSS将CSS Rules分为5个Categories: Base Layout Module State Them ...
- C#复习二(Twenty First Day)
呵呵,又来到了今天的总结.这次主要复习了一下字符串的一些处理.今天就来总结一下. 理论: String 字符串,字符串可以看成字符数组,不可变特性(通过for循环,修改string中的元素,失败!) ...
- 记录一次webbrowser无法加载 activex 遇到的问题
关联配置: win7 x64 Adobe Reader XI activex 安装目录X84 笔者项目运行Any CPU 无论如何也加载不出PDF 刚开始还以为自己封装的控件XWebBrowser的问 ...
- window.parent与window.opener的区别与使用
window.parent 是iframe页面调用父页面对象 举例: a.html 如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrma ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- PrintWriter与outputStream区别
网上截取: printWriter:我们一般用来传的是对像 而outputStream用来传的是二进制,故上传文件时,一定要使用此. PrintWriter以字符为单位,支持汉字,OutputStre ...
- Dojo实现Tabs页报错(一)
1.在用Dojo写tab页的过程中出现了一些错误 dojo源码如下: <%-- Document : grid Created on : 2013-12-15, 18:05:47 Author ...
- jquery初学笔记
官方网站:http://jquery.com/ 一个简单的JQuery实例: <!DOCTYPE html> <html lang="en" xmlns=&quo ...