一 深度优先遍历,参考前面DFS(white and gray and black)

二 根据定点以及边数目进行判断

如果m(edge)大于n(vertex),那么肯定存在环

算法如下:

1 删除所有入度小于等于1的顶点, 并且将和这些顶点相关的顶点入度减1

2 将入度变为1的顶点全部删除,重复上述动作,如果最后还有顶点那么图中存在环

具体代码如下:

#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; 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++;
for( int v = 0; v < vexnum; v++){
if(M[u][v] && v != p){
if(colour[v] == WHITE) dfs(v, u);
else if(colour[v] == GRAY)
cout<<"back edge between"<<u + 1<<" and"<<v + 1<<endl;
else if(colour[v] == BLACK)
cout<<"cross edge between"<<u + 1<<" and"<<v + 1<<endl;;
}
}
colour[u] = BLACK;
}
void print_dfs_num(){
for(int v = 0; v < vexnum; v++)
cout<<dfsNum[v]<<" ";
}
*/ void LoopJudge(){
bool loop = false; int twice = 2;
int k, i, j;
cout<<"line: "<<__LINE__<<endl;
for( k = twice; k > 0; k--){
cout<<"line: "<<__LINE__<<"k: "<<k<<endl;
for( i = 0; i < vexnum; i++){
cout<<"line: "<<__LINE__<<"i: "<<i<<endl;
if(indegree[i] <= 1){
indegree[i] = 0; //delete vertex in degree equal one
for( j = 0; j < vexnum; j++){
cout<<"line: "<<__LINE__<<"j: "<<j<<endl;
if(M[i][j]){
M[i][j] = false;
M[j][i] = false;
indegree[j]--;
}//if(M[i][j])
}//for(int j = 0; j < vexnum; j++)
}//if(indegree[i] <= 1)
}//for(int i = 0; i < vexnum; i++)
} for( k = 0; k < vexnum; k++){
if(indegree[k] != 0){
loop = true;
}
} if(loop)
cout<<"There is loop in undirected graph!"<<endl;
else
cout<<"There is no loop in undirected graph!"<<endl;
} int main()
{
init_graph();
//dfs(0, -1);
//print_dfs_num();
LoopJudge(); int ch;
cin>>ch;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

judge loop in undirected graph的更多相关文章

  1. Judge loop in directed graph

    1 深度优先方法 首先需要更改矩阵初始化函数init_graph() 然后我们需要初始化vist标记数组 深度优先访问图,然后根据是否存在back edge判断是否存在环路 算法如下: #includ ...

  2. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  3. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  4. Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  5. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  6. [Locked] Number of Connected Components in an Undirected Graph

    Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...

  7. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  8. [LintCode] Find the Connected Component in the Undirected Graph

    Find the Connected Component in the Undirected Graph Find the number connected component in the undi ...

  9. 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

随机推荐

  1. smarty如何处理状态值的显示

    比如状态,有效或者无效.这个数据库中保存的是1或者2这样的字段. 显示在列表的时候不能是1或者2吧. 以前,我都是在后台foreach,处理的.感觉处理之后,前台就不灵活了.这个值就被替换成了文字. ...

  2. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  3. 疯狂的补贴,广州司机都被Uber触动

    “上线的司机起码少了一半.” 或许是因为长期工作超过12个小时的缘故,39岁的广州人民优步司机姜德昌看上去双眼浮肿,但精力充沛.这是5月8日的一个下午,一周之前他所服务的Uber广州分公司被查封. 据 ...

  4. VB.NET版机房收费系统---SqlHelper

    SqlHelper,最早接触这个词儿的时候,好像是13年的暑假,那个夏天来的比往年来的稍晚一些,呵呵,sqlhelper,翻译成中文就是数据库助手,帮手.百度百科这样对她进行阐述: SqlHelper ...

  5. JavaScript 实现Map

    var map=new Map(); map.put("a","A");map.put("b","B");map.put ...

  6. 基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple)

    ===================================================== 基于最简单的FFmpeg封装工艺的系列文章上市: 最简单的基于FFmpeg的封装格式处理:视 ...

  7. [置顶] 内存映射失败MapViewOfFile 失败 返回 8

    问题描述1 在使用内存映射方式读写数据时,将文件A的内容拷贝至文件B中,偶尔会出来文件拷贝后的文件,内容为空,或部分为空 问题分析1 怀疑是内存映射方式读写数据的稳定性(可笑的怀疑,内存映射可以Win ...

  8. 2.Freshman阶段学习内容的确定

    我刷知乎.在知乎上答题的程序员,不是很牛逼就是更牛逼,说起各种系统.各种系统的各种版本.各种语言.数据库.算法.IT届的各种圣战都有板有眼.信手拈来.头头是道,不得不服.这导致了一些非常严重的问题:我 ...

  9. IS-A 和 HAS-A

    IS-A关系 IS-A就是说:一个对象是另一个对象的一个分类. 下面是使用关键字extends实现继承. public class Animal{ } public class Mammal exte ...

  10. POJ 1743 Musical Theme(不可重叠最长重复子串)

    题目链接:http://poj.org/problem?id=1743 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一 ...