判断图连通性的问题
#include <stdio.h>#define MAX_VERTEX_NUM 20
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int VRType;
typedef char VertexType;
typedef char InfoType;
typedef enum {DG,DN,UDG,UDN}GraphKind;
int visited[MAX_VERTEX_NUM];
typedef struct ArcCell
{
VRType adj;
InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
void DFS(MGraph G,int i,int &a)
{
int j;
visited[i]=TRUE;
a++;
for(j=0;j<5;j++)
if(G.arcs[i][j].adj==1&&!visited[j])
DFS(G,j,a);
}
int DFSTraverse(MGraph G)
{
int b=0;
DFS(G,0,b);
if(b==5)
return OK;
else
return ERROR;
}
int main()
{
MGraph G,G1;
int i,n,j;
G.arcs[4][4].adj=(0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0);
G1.arcs[4][4].adj=(0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0);
for(i=0;i<5;i++)
visited[i]=0;
n=DFSTraverse(G);
j=DFSTraverse(G1);
if(n==1)
printf("yes\n");
else
printf("no\n");
if(j==1)
printf("yes\n");
else
printf("no\n");
return 0;
} 结果应该是第一个是连通的 第二个不是 我调试了一下 for(j=0;j<5;j++)
if(G.arcs[i][j].adj==1&&!visited[j])
DFS(G,j,a);这句话一直没执行过 不知道为什么 是我二维数组的初始化错了么