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

  1. judge loop in undirected graph

    一 深度优先遍历,参考前面DFS(white and gray and black) 二 根据定点以及边数目进行判断 如果m(edge)大于n(vertex),那么肯定存在环 算法如下: 1 删除所有 ...

  2. Directed Graph Loop detection and if not have, path to print all path.

    这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...

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

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

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

  6. CodeChef Counting on a directed graph

    Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...

  7. Geeks - Detect Cycle in a Directed Graph 推断图是否有环

    Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...

  8. Skeleton-Based Action Recognition with Directed Graph Neural Network

    Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...

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

随机推荐

  1. 《火球——UML大战需求分析》(0.1)——开篇废话

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  2. 关于结构体和C++类的内存地址问题

    关于结构体和C++类的内存地址问题   今天终于有时间写点东西了~ 太爽了  *_*   很多人都知道C++类是由结构体发展得来的,所以他们的成员变量(C语言的结构体只有成员变量)的内存分配机制是一样 ...

  3. gcc 的include path和lib path调整

    `gcc -print-prog-name=cc1plus` -v `g++ -print-prog-name=cc1plus` -v   ------------------------------ ...

  4. NGUI使用教程(1) 安装NGUI插件

    前言 鉴于当前游戏开发的大势,Unity3d的发展势头超乎我的预期,作为一个Flash开发人员,也是为Flash在游戏开发尤其是手游开发中的地位感到担忧....所以 近期一段时间都在自己学习unity ...

  5. g711u与g729比較编码格式

    •711a-编解码格式为G.711 alaw •g711u-编解码格式为G.711 ulaw (the default) •g729-编解码格式为G.729 •g729a-编解码格式为G.729a 上 ...

  6. Android TextView 实现文字大小不同和文字颜色不同

    效果图如下: 关键代码如下: StringBuffer sb = new StringBuffer(); if(day > 0) { sb.append("<a href=\&q ...

  7. 初探ListView

    ListView可能是Android开发中最常用的一个控件,但要用的纯熟还需要不断的锻炼. 建立简单的ListView 1.在布局文件(.xml)中添加<ListView>标签 2.在Ma ...

  8. 获取当前WEB应用全路径

    <%String path = request.getContextPath();String basePath =request.getScheme()+"://"+req ...

  9. C++ 字符串指针与字符串数组

    在做面试100题中第21题时,发现char *astr="abcdefghijk\0";和char astr[]={"abcdefghijk"};有点区别,以前 ...

  10. BZOJ 1114 Number theory(莫比乌斯反演+预处理)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738 题意:给你一个整数序列a1, a2, a3, ... , ...