邻接矩阵
图存储结构包含:结点(一维数组),边(二维数组)(也就是矩阵),顶点数和弧度数
#define Vmax 100 //顶点数目的最大值
typedef char Vex; //顶点的数据类型是char
typedef int Edg; //带权图 边的数据类型是权值
typedef struct{
Vex vex[Vmax];
Edg arc[Vmax][Vmax]; //矩阵
int vnum,arcnum; //顶点数和弧度数
}MGraph;
无向图是一个对称矩阵
带权图的邻接矩阵应该存放边的权值,用∞来表示不存在的边
有向图和无向图均用0&1来表示链接
适用性
比较稠密的图(边数e>nlogn)
空间复杂度 O(n^2)
创建图
void creatGraph(MGraph *G){
scanf("%d%d",&G->vexnum,&G->arcnum);
for(int i = 0;i<G->vexnum;i++)
scanf("%c",&G->vex[i]);
for(int j = 0;j<G->vexnum;j++){
for(int k = 0;k<G->vexnum;k++){
G->e[j][k] = 0;
}
}
int a,b;
for(int n = 0;n<G->arcnum;n++){
scanf("%d%d",&a,&b);
G->arc[a-1][b-1] = 1;
G->arc[b-1][a-1] = 1;
}
}