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. class类的sizeof计算

    class no_virtual { public: void fun1() const{} int fun2() const { return a; } private: int a; } clas ...

  2. Android Fragment 嵌套使用报错

    在新的SDK每次创建activity时,会自己主动生成  <pre name="code" class="java">public static c ...

  3. Linux下PHP与普通C程序通信

    Linux下的普通C程序之前可以使用FIFO(有名管道来进行进程间通信,因为这个管道以一个文件的形式存在于文件系统上,因此只要能读写这个文件就可以实现进程间通信. 首先使用mkfifo命令有文件系统上 ...

  4. $timeout, $interval

    $timeout, $interval   layout: posttitle: Angular@1.4.3 中文 API 服务篇 $timeout & $intervaldesc: '$ti ...

  5. SQL函数经常用到的mark一下

    在项目开发过程中存储过程会用到很多SQL函数,经常用到的mark一下 1.经常用到的mark 一下 经常需要把id字符以','分隔传入存储过程然后SQL语句用in去搜索但是经常是这样的情况id 经常是 ...

  6. POJ 3784.Running Median

    2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...

  7. c#计算文件的MD5值

    代码: /// <summary> /// 计算文件的 MD5 值 /// </summary> /// <param name="fileName" ...

  8. POJ 1840 Brainman(逆序对数)

    题目链接:http://poj.org/problem?id=1804 题意:给定一个序列a[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...

  9. [NewCoder]复杂链表的复制

    看下面一个链表结点的定义: struct ComplexListNode { int val; struct ComplexListNode *next; struct ComplexListNode ...

  10. [置顶] 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……

    两台主机A.B搭建mysql主从复制关系(A为master,B为slave)后,在slave上执行show slave status,结果中显示Last_IO_Error: error connect ...