一 深度优先遍历,参考前面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. 快速的CDN加速服务

    jQuery Migrate jQuery官网CDN地址jQuery版本迁移辅助插件,用jquery不同版本开发的程序在修改jquery版本出现的兼容问题可以使用jQuery Migrate解决此问题 ...

  2. 串的模式匹配——Brute-Force算法

    Brute-Force算法的基本思路为:从目标串s=“s0s1...sn-1”的第一个字符开始和模式串t=“t0t1t2...tn-1”中的第一个字符比较,若相等,则继续逐个比较后续字符: 否则从目标 ...

  3. nefu 462 fib组合

    nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.ed ...

  4. Cocos2d-x lua游戏开发之安装Lua到mac系统

    注意:mac ox .lua version :5.15 下载lua官网的lua, 注意:最好是5.15下面.5.2的lua不支持table的getn()方法,这让我情何以堪.(获取table长度.相 ...

  5. Swift 中类的初始化器与继承

    首先,Swift 为类定义了两种初始化器来确保类中所有的储存属性都能得到一个初始化值.这两种初始化器就是「指定初始化器」(Designated Initializer)与「便利初始化器」(Conven ...

  6. iOS面试题05-父子控制器、内存管理

    内存管理.父子控制器面试题 1.建立父子关系控制器有什么用 回答:1>监听屏幕选中 2>如果想拿到你当前的很小的一个控制器所在的导航控制器必须要跟外面比较大的控制器建立父子关系,才能一层一 ...

  7. 利用 squid 反向代理提高网站性能

    http://www.ibm.com/developerworks/cn/linux/l-cn-squid/ http://www.squid-cache.org/ http://www.beijin ...

  8. (转)openURL的使用方法

    view plaincopy to clipboardprint? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ap ...

  9. Winform获取当前程序名称或路径

    以下几种方法获取当前程序名称或路径: // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径.      // 获 ...

  10. imx:MfgTool

    MfgTool使用方法: MfgTool很妖,写进去的img的名字一定要符合配置文件里面的命名标准. 具体要参见:        MFG_TOOL\Profiles\Linux\OS Firmware ...