一 深度优先遍历,参考前面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. Android使用ViewFlipper实现左右滑动效果面

    在我的博客中,上次是使用ViewPager实现左右滑动的效果的,请看文章:Android使用ViewPager实现左右滑动效果. 这次我来使用ViewFlipper实现这种效果,好了,先看看效果吧: ...

  2. sublime test3 使用技巧

    sublimeText3使用技巧 常用快捷键 ctrl+d :选中光标处的文本单元,继续按ctrl+d选中相同文本单元 alt+F3 :功能和ctrl+d类似,用于批量修改相同文本 shift+↑ ↓ ...

  3. objective-C学习笔记(八) 集合类型 Collection Types

    OBJC的集合类型: 1.数组 Array 2.Set 3.键值对 Dictionary 数组:OC中的数组被定义为class,引用类型.索引从0开始,访问越界会抛出运行时异常. NSArray的元素 ...

  4. js中使用控件名和数组下标方式获取控件的值时失败

    在做界面展示时涉及到表单行项目的增加和删除时,我们一帮都使用js的脚本实现表单行的增加和删除,那么在进行表单的提交的时我们会再页面上进行提交数据的初步校验,进行数据的初步校验时,就要动态获取控件的值. ...

  5. JavaScript基础知识----零基础js入门练习题

    1,什么是Javascript? 答:Javascipt是一种脚本语言,由web浏览器进行解释和执行.   2,JavaScript是由那几个部分组成?  答:主要分为以下三种:     ECMASc ...

  6. 使用 Sublime Text 3 开发 React

    下载, 安装, 破解就不用说了, 直接进主题: 1, 安装Package Control 默认的Sublime 3中没有Package Control,要进行安装之后才能用这个去安装其他的插件. 简单 ...

  7. C# Regex ignoring non-capturing group

    E.g I want match the keword "3398" after "red" from the string "This is red ...

  8. CSS3 模拟笑脸

    参考 http://www.html5tricks.com/demo/html5-css3-smile-face/index.html 它还做了舌头... 一开始我都是用JS实现的动画  当然了  眼 ...

  9. ruby2.0(rails)以后版本的debug

    很喜欢RUBY(RAILS),认识也好久好久了,但是说实话,从来没用ROR写过什么东西,都是小打小闹,做些自娱自乐的东西,碰到什么问题,基本仔细看看,加上几个LOG就找到原因了,从来没想过要DEBUG ...

  10. 帝国cms让当前栏目显示不同样式(图文)

    在使用帝国cms制作栏目导航条时,我们可能会需要根据当前栏目,使当前栏目样式不同. 如图: 此类导航,源代码一般为 <li>全站首页</li> <li class=&qu ...