#include <stdio.h>
#define Max 20
typedef int VexType;
typedef VexType Mgraph[Max][Max];
/*函数声明*/
void creat_mg(Mgraph G);
void out_mg(Mgraph G);
Mgraph G1; //邻接矩阵的二维数组名
int n, e; //n为顶点数,e为边数
/*主函数*/
int main() {
creat_mg(G1);
out_mg(G1);
return 0;
}
//建立邻接矩阵
void creat_mg(Mgraph G) {
int i,j,k;
//输入图的相关信息
printf("请输入图的顶点数:");
scanf("%d",&n);
printf("请输入图的边数:");
scanf("%d",&e);
//将图中的所有值赋值为0,即任意两个点都是不相关的
//初始化 领接矩阵
//空出G[][0],便于记录
for(i = 1; i <= n ; i++) {
for(j =1 ; j <= n ; j++)
G[i][j] = 0;
}
//赋值
for(k = 1; k <= e; k++) {
printf("请输入之间有路径的两个顶点vi , vj");
printf("\n");
scanf("%d%d",&i,&j);
G[i][j] = 1;
G[j][i] = 1;
}
}
//输出邻接矩阵
void out_mg(Mgraph G) {
int i ,j ,k;
//输出矩阵
for(i = 1; i <= n; i++) {
printf("\n");
for(j = 1; j <= n; j++)
printf("%5d",G[i][j]);
}
//输出所在边
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(G[i][j] != 0)
printf("\n 存在边<%d,%d>",i,j);
printf("\n\n\n 程序运行结束");
}
#include <stdio.h>
#define Max 20
typedef int