一 深度优先遍历,参考前面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. delphi关于文件操作集锦

        关于文件操作集锦 取得该快捷方式的指向EXE关键词:快捷方式 LNK unit Unit1; interface usesWindows, Messages, SysUtils, Varian ...

  2. 跨浏览器resize事件分析

    resize事件 原生事件分析 window一次resize事件: IE7 触发3次, IE8 触发2次, IE9 触发1次, IE10 触发1次 Chrome 触发1次 FF 触发2次 Opera ...

  3. FPGA STA(静态时序分析)

    1 FPGA设计过程中所遇到的路径有输入到触发器,触发器到触发器,触发器到输出,例如以下图所看到的: 这些路径与输入延时输出延时,建立和保持时序有关. 2. 应用背景 静态时序分析简称STA,它是一种 ...

  4. LCM Cardinality

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31675#problem/E 暴力 // File Name: uva10892.cpp ...

  5. java String.Format详解

    JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String format, Object... args) 使用指定的语言 ...

  6. 在基类中的析构函数声明为virtual

    #include <iostream> using namespace std; class Father { public: ~Father() { cout << &quo ...

  7. 解决 Tomcat reload WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but fail

    转自:http://www.cnblogs.com/interdrp/p/5632529.html 我的错误如下: 06-Sep-2016 18:57:10.595 WARNING [localhos ...

  8. JS图表组件 highcharts 简单的介绍

    把highcharts拿来做个简单的介绍,希望更多的朋友可以用到这个用来做图表的js插件. preparation Highcharts Highcharts是一个制作图表的纯Javascript类库 ...

  9. .net 基础错误-string.replace 方法

    1.string string.Replace(string oldValue,string newValue) 返回一个新的字符串,其中当前示例中出现的所有指定字符串都替换另一个指定字符串 错误:总 ...

  10. php mysql实现栏目分类递归

    header("content-type:text/html;charset=utf-8"); $dbhost = "localhost";   // 数据库主 ...