构造一个具有n个顶点和e条边的图的时间复杂度是O(n^2+e*n),
其中对邻接矩阵的初始化耗费了O(n^2)的时间
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef int Status;
#define OK 1
#define ERROR 0
typedef int VRType; //定义顶点关系类型为整形
typedef int InfoType; //定义弧相关信息为整形
typedef char VertexType; //定义顶点类型为字符型
#define INFINITY INT_MAX //定义最大整形为无穷
#define MAX_VERTEX_NUM 20 //最大顶点个数
typedef enum {DG,DN,UDG,UDN} GraphKind; //定义{有向图,有向网,无向图,无向网}等枚举常量,同时定义GraphKind为枚举变量类型
typedef struct ArcCell
{
VRType adj; //VRType是顶点关系类型。对无权图用1或0表示是否相邻,对带权图,则为权值类型
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;
Status CreateGraph(MGraph *G);
Status CreateDG(MGraph *G);
Status CreateDN(MGraph *G);
Status CreateUDG(MGraph *G);
Status CreateUDN(MGraph *G);
int LocateVex(MGraph G,VertexType v);
int main()
{
MGraph *G;
G=(MGraph *)malloc(sizeof(MGraph));
CreateGraph(G);
int i,j;
for(i=0;i<G->vexnum;i++)
{
for(j=0;j<G->vexnum;j++)
{
if(G->arcs[i][j].adj==INFINITY)
printf("N ");
else
printf("%d ",G->arcs[i][j].adj);
}
printf("\n");
}
return 0;
}
int LocateVex(MGraph G,VertexType v)
{