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. ListView 行高设置不起作用

    通常我们在自定义listview的时候会遇到这个问题,我们设置了每行的layout的高度,但是没起作用,那是因为在adapter里面getview的时候出的问题,通常我们写: view = mInfl ...

  2. JAVA排序(一) Comparable接口

    昨天接到一个实习公司的电话面试,来的很突然,没有准备. 由于以前没用过,在被他问及是否用过JAVA的排序工具Comparable与Comparator时,没有回答上来,只能实话实说没有用过. 感觉太丢 ...

  3. 用auto_ptr类模板帮助动态内存管理

    动态内存使用最多的是在C++应用程序的代码中.有过编程经验的程序员虽然都知道new操作符的使用一定要与delete匹配,在某些场合仍然可能有内存溢出.当异常被掷出时,程序的正常控制流程被改变,因此导致 ...

  4. 每天学点Linux:二

    关于输入输出和重定向: 默认情况下标准I/O的输入为键盘,输出为显示屏.输入和输出可以通过符号‘>’进行重定向. 例如可以通过命令:$>newfile 来创建一个新文件(如果newfile ...

  5. Git库文件的状态

    git库所在的文件夹(即.git所在的文件夹)中的文件的状态: (1)untracked:未跟踪,此文件在文件夹中,但并没有加入git库,不参与版本控制. 通过”git add”,”git commi ...

  6. 【课程分享】ASP.NET MVC5&amp;微信公众平台整合开发实战(响应式布局、JQuery Mobile,Windows Azure、微信核心开发)

    对这个课程有兴趣的,能够联系我QQ2748165793 基础知识储备 ASP.NET MVC 5基础(6讲) 第一讲-初识ASP.NET MVC并搭建整合开发环境 第二讲-深入MVC开发模式 第三讲- ...

  7. iOS中Block介绍(一)基础

    ios开发block的使用指南,以及深入理解block的内存管理,也适用于osx开发.讨论范围:block的使用,内存管理,内部实现.不包含的内容:gc arc下的block内存,block在c++中 ...

  8. CSS换行2

    1.可以使用强制换行符号<br />换行.如果在一个文章里可以在文章需要换行的地方加入<br />即可实现自动换行-常说的小换行,与换行前没有间隔.实例如下图 换行说明图无间隔 ...

  9. 使用coding.net来托管源码(可以免费存放私有项目的哦)(转载)

    coding.net是国内新兴的一个项目管理平台,功能主要包括:代码托管.在线运行环境.监控代码质量,兼有一定的社交功能.在线运行环境支持Java.Ruby.Node.js.PHP.Python.Go ...

  10. iOS中UIWebView使用JS交互 - 机智的新手

    iOS中偶尔也会用到webview来显示一些内容,比如新闻,或者一段介绍.但是用的不多,现在来教大家怎么使用js跟webview进行交互. 这里就拿点击图片获取图片路径为例: 1.测试页面html & ...