/* 利用深度优先遍历,找出图中的所有连通图(子图)
* 图用邻接表表示 *graph[], 利用边的信息来创建adjacency lists
*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVERTICES 20
#define MAXEDGES 20
typedef enum {
FALSE,
TRUE,
TRISTATE
} bool;
typedef struct node
{
int dst;
struct node *next;
}node_t;
/* print graph in adjacency lists */
void printGraph(node_t *graph[], int nvert)
{
int i, j;
for(i=0; i<nvert; i++)
{
node_t *ptr;
printf("node %d: ", i);
for(ptr = graph[i]; ptr; ptr=ptr->next)
printf("[%d]", ptr->dst);
putchar('\n');
}
}
/*****************************find all connected components****************************************/
/* 头插法创建邻接表(没有单独的头节点,只有一个头指针,即指针数组) */
/* 此处因为头指针会改变,所以要传入指针的指针 */
/* @ptr: adjacency list head (*graph[])
* @dst: 待插入的节点(在graph中index)
*/
void insertEdge(node_t **ptr, int dst)
{
node_t *newnode = (node_
找出图中的所有连通子图(创建图的邻接表,深度优先遍历查找子图)
最新推荐文章于 2022-10-29 11:30:05 发布
本文介绍了如何利用深度优先遍历算法,结合邻接表数据结构,找出图中的所有连通子图。首先,文章展示了如何创建邻接表表示图的结构,然后通过`buildGraph`函数构建图的邻接表。接着,`dfs`函数用于深度优先遍历,`compINC`函数遍历所有未访问的节点,找出并打印所有连通组件。最后,给出一个示例代码实现并输出结果。
摘要由CSDN通过智能技术生成