方法概要:
nodeset_dst[i][k][ ]表示i节点经过K跳可达节点集合;
步骤1:从0跳(节点自身)开始根据邻接矩阵逐跳添加节点:即如果节点i经过k-1跳可达节点m,且m存在到n的有向边;则i经过k跳可达n(注意处理i到n的其他更短路径),将n添加到nodeset_dst[i][k][ ]数组中;并将nodeset_map[i][n]低4bit置1,表示n在i的可达目的节点集合中,nodeset_map[n][i]高4bit置1,表示i在可达n的源点集合中。
步骤2:按步骤1循环处理每一个节点,在为节点i添加可达目的节点j时,如果发现j位于可达节点i的源节点集合中,则找到环。
源代码:
/*check whether there is a cycle in graph*/#include <stdio.h>
#include <string.h>
#define TOTAL_NODE_NUMBER 6
char adj_matrix[TOTAL_NODE_NUMBER][TOTAL_NODE_NUMBER] = {
1,0,0,0,0,0,
0,1,0,0,0,1,
0,1,1,0,0,0,
1,0,0,1,0,0,
1,0,0,0,1,0,
0,0,1,1,1,1
};
/*nodeset_dst[i][k][]表示i节点经过K跳可达节点集合
*nodeset_dst_cnt[i][k]记录节点i从发经过k跳可达节点的个数
*nodeset_map[i][j]; 0-3bit非0表示节点j在节点i的可达目的节点集合中,4-7bit非0表示节点j在可达节点i的源节点集合中。
*shortest_path[i][j][]记录从i到j经过的最短路径*/
char nodeset_dst[TOTAL_NODE_NUMBER][TOTAL_NODE_NUMBER][TOTAL_NODE_NUMBER];
char nodeset_dst_cnt[TOTAL_NODE_NUMBER][TOTAL_NODE_NUMBER-1];
char nodeset_map[TOTAL_NODE_NUMBER][TOTAL_NODE_NUMBER];
/*shortest_path[i][j].node_set表示从i到j的路径包含到节点,含i,j
*shortest_path[i][j].node_cnt表示从i到j到路径包含的节点个数,含i,j*/
typedef struct stPath {
char node_cnt;
char node_set[TOTAL_NODE_NUMBER];
} stPath;
stPath shortest_path[TOTAL_NODE_NUMBER][TOTAL_NODE_NUMBER];
void add_to_shortest_path(char firstnode, char lastnode, char newnode) {
char tmp_cnt, i;
tmp_cnt = shortest_path[firstnode][lastnode].node_cnt;
for(i = 0; i < tmp_cnt; i++) {
shortest_path[firstnode][newnode].node_set[i] = shortest_path[firstnode][lastnode].node_set[i];
}
shortest_path[firstnode][newnode].node_set[i] = newnode;
shortest_path[firstnode][newnode].node_cnt = i + 1;
}
void graph_init() {
int node_index;
memset(nodeset_dst, -1, sizeof(nodeset_dst));
memset(nodeset_dst_cnt, 0, sizeof(nodeset_dst_cnt));
memset(nodeset_map, 0, sizeof(nodeset_map));
memset(shortest_path, -1, sizeof(shortest_path));
for(node_index = 0; node_index < TOTAL_NODE_NUMBER; node_index ++) {
nodeset_dst[node_index][0][0] = node_index;
nodeset_dst_cnt[node_index][0] = 1;
nodeset_map[node_index][node_index] |= 0x11;
shortest_path[node_index][node_index].node_set[0] = node_index;
shortest_path[node_index][node_index].node_cnt = 1;
}
}
void print_cycle(int src, int dst) {
int i;
printf("cycle is :\n");
for(i = 0; i < shortest_path[src][dst].node_cnt; i++) {
printf ("%d ", shortest_path[src][dst].node_set[i]);
}
for(i = 1; i < shortest_path[dst][src].node_cnt; i++) {
printf ("%d ", shortest_path[dst][src].node_set[i]);
}
printf("\n");
}
void main() {
int node_index, jump, i, j;
int last_node, node_cnt;
graph_init();
for(node_index = 0; node_index < TOTAL_NODE_NUMBER; node_index ++) { //遍历图中每个节点
for(jump = 1; ; jump ++){//遍历从一个节点A出发的跳数
for(i = 0; i < nodeset_dst_cnt[node_index][jump-1]; i ++) {//遍历到节点A距离jump-1跳的节点
last_node = nodeset_dst[node_index][jump -1][i];
for(j = 0; j < TOTAL_NODE_NUMBER; j ++) {//以到节点A距离jump-1跳的节点b作为起始地点查找jump跳的节点
if((last_node == j) || ((nodeset_map[node_index][j] & 0x0f) != 0)){
continue;
} else if(adj_matrix[last_node][j] != 0) {
//将节点j加入到node_index的jump跳可达节点中
add_to_shortest_path(node_index, last_node, j);
//printf("node:%d, jump:%d, lastnode:%d, newnode:%d\n",node_index, jump, last_node, j);
node_cnt = nodeset_dst_cnt[node_index][jump];
nodeset_dst[node_index][jump][node_cnt] = j;
nodeset_dst_cnt[node_index][jump] ++;
nodeset_map[node_index][j] |= 0x01;
nodeset_map[j][node_index] |= 0x10;
if((nodeset_map[node_index][j] & 0xf0) != 0){
//找到环了,打印shortest_path中的路径
print_cycle(node_index, j);
return;
}
}
}
}
if (nodeset_dst_cnt[node_index][jump] == 0)
break;
}
}
}
程序执行结果:
fhy@fhy-laptop:~/wkspc/interesting_puzzle$ gcc cycle_check_in_graph.c
fhy@fhy-laptop:~/wkspc/interesting_puzzle$ ./a.out
cycle is :
2 1 5 2
本文介绍了一种基于邻接矩阵的图算法,用于检测有向图中的环路。通过递增跳数来构建节点可达性集合,并利用该集合判断是否存在环路。文章提供了完整的源代码实现及运行结果。
1500

被折叠的 条评论
为什么被折叠?



