一、邻接矩阵的表示方法
#define MaxVertexNum 100 //最大顶点数
#define INFINITY 65535 //∞设为双字节无符号整型的最大值65535
typedef int Vertex; //用顶点下标表示顶点,为整型
typedef int WeightType; //边的权值设为整型
typedef char DataType; //顶点存储的数据类型为字符型
/*-----------图的定义-------------*/
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;//顶点数
int Ne; //边数
WeightType G[MaxVertexNum][MaxVertexNum]; //邻接矩阵
DataType Data[MaxVertexNum];//存顶点的数据
};
typedef PtrToGNode MGraph; //以邻接矩阵存储的图的类型
/*-----------边的定义-------------*/
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1,V2;//有向边<V1,V2>
WeightType Weight; //权值
};
typedef PtrToENode Edge;
二、初始化一个有顶点无边的图
MGraph CreateGraph(int VertexNum)
{//创建一个包含全部顶点的无边图
Vertex V,W;
MGraph Graph;
Graph=(MGraph)malloc(sizeof(struct GNode));
Graph->Nv =VertexNum;
Graph->Ne =0;
//初始化邻接矩阵
//顶点编号从0-Graph->Nv-1
for(V=0;V<Graph->Nv;V++)
for(W=0;W<Graph->Nv;W++)
Graph->G[V][W]=INFINITY;//都为∞
// PrintResult(Graph);
return Graph;
}
三、逐条插入边
void InsertEdge(MGraph Graph,Edge E)
{//逐条插入边
//插入边<V1,V2>
Graph->G [E->V1 ][E->V2 ]=E->Weight ;
//若是无向图,还需要插入边<V2,V1>
//Graph->G [E->V2 ][E->V1 ]=E->Weight ;
}
四、完整代码
//邻接矩阵存储的有向图
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxVertexNum 100 //最大顶点数
#define INFINITY 65535 //∞设为双字节无符号整型的最大值65535
typedef int Vertex; //用顶点下标表示顶点,为整型
typedef int WeightType; //边的权值设为整型
typedef char DataType; //顶点存储的数据类型为字符型
/*-----------图的定义-------------*/
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;//顶点数
int Ne; //边数
WeightType G[MaxVertexNum][MaxVertexNum]; //邻接矩阵
DataType Data[MaxVertexNum];//存顶点的数据
};
typedef PtrToGNode MGraph; //以邻接矩阵存储的图的类型
/*-----------边的定义-------------*/
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1,V2;//有向边<V1,V2>
WeightType Weight; //权值
};
typedef PtrToENode Edge;
/*-----------创建-------------*/
MGraph CreateGraph(int VertexNum)
{//创建一个包含全部顶点的无边图
Vertex V,W;
MGraph Graph;
Graph=(MGraph)malloc(sizeof(struct GNode));
Graph->Nv =VertexNum;
Graph->Ne =0;
//初始化邻接矩阵
//顶点编号从0-Graph->Nv-1
for(V=0;V<Graph->Nv;V++)
for(W=0;W<Graph->Nv;W++)
Graph->G[V][W]=INFINITY;//都为∞
// PrintResult(Graph);
return Graph;
}
/*-----------插入-------------*/
void InsertEdge(MGraph Graph,Edge E)
{//逐条插入边
//插入边<V1,V2>
Graph->G [E->V1 ][E->V2 ]=E->Weight ;
//若是无向图,还需要插入边<V2,V1>
//Graph->G [E->V2 ][E->V1 ]=E->Weight ;
}
/*-----------读入数据创建图-------------*/
MGraph BuildGraph()
{
MGraph Graph;
Edge E;
Vertex V;
int Nv,i;
printf("请输入图的顶点数:");
scanf("%d",&Nv);
Graph=CreateGraph(Nv);//初始化有Nv个顶点但没有边的图
printf("请输入边的个数:");
scanf("%d",&(Graph->Ne));
if(Graph->Ne !=0){//有边
//插入边的起点,终点,权值
printf("请输入%d个边的起点 终点 权值:\n",Graph->Ne );
E=(Edge)malloc(sizeof(struct ENode));//开辟空间
for(i=0;i<Graph->Ne ;i++){
printf("%d : ",i);
scanf("%d %d %d",&E->V1,&E->V2 ,&E->Weight );
InsertEdge(Graph,E);
}
}
//如果顶点有数据的话,读入数据
//for(V=0;V<Graph->Nv ;V++)
// scanf("%c",&(Graph->Data[V]));
return Graph;
}
void PrintResult(MGraph Graph)
{
int V,W;
for(V=0;V<Graph->Nv ;V++){
for(W=0;W<Graph->Nv ;W++){
if(Graph->G[V][W]==INFINITY)printf("∞ ");
else printf("%d ",Graph->G[V][W]);
}
printf("\n");
}
}
int main()
{
MGraph Graph;
Graph=BuildGraph();
PrintResult(Graph);
return 0;
}
五、运行
有向图:
![](https://img-blog.csdnimg.cn/img_convert/030e25a84c52f0f63f9f8145c790de92.png)
无向图:
![](https://img-blog.csdnimg.cn/img_convert/04d4f358aab71d96c224415e08660d0d.png)
![](https://img-blog.csdnimg.cn/img_convert/134766b2bd7433116b7b39db045abd7c.jpeg)