新网免费做网站,郓城县网站建设,wordpress pdf缩略图,深圳搜索引擎文章目录1.什么是图#xff1f;2.图的抽象数据结构3.如何在程序中表示一个图#xff1f;3.1 邻接矩阵3.2 邻接表4.图的遍历4.1 深度优先搜索4.2 广度优先搜索5.图的C语言实现1.什么是图#xff1f; 2.图的抽象数据结构 3.如何在程序中表示一个图#xff1f;
3.1 邻接矩阵 …
文章目录1.什么是图2.图的抽象数据结构3.如何在程序中表示一个图3.1 邻接矩阵3.2 邻接表4.图的遍历4.1 深度优先搜索4.2 广度优先搜索5.图的C语言实现1.什么是图 2.图的抽象数据结构 3.如何在程序中表示一个图
3.1 邻接矩阵 3.2 邻接表 4.图的遍历
4.1 深度优先搜索 4.2 广度优先搜索 5.图的C语言实现
#includestdio.h
#includestdlib.h#define WeightType int
#define MaxVertexNum 3
typedef struct GNode *PtrToGNode;
typedef PtrToGNode MGraph;
typedef int Vertex;
typedef struct ENode *PtrToENode;
typedef PtrToENode Edge;struct GNode{int Nv; //顶点数int Ne; //边数WeightType G[MaxVertexNum][MaxVertexNum];
};
struct ENode{Vertex V1,V2;WeightType Weight;
};//1.初始化
MGraph CreateGraph(int VertexNum)
{Vertex V,W;MGraph Graph;Graph(MGraph)malloc(sizeof(struct GNode));Graph-NvVertexNum;Graph-Ne0;for(V0;VGraph-Nv;V){for(W0;WGraph-Nv;W){Graph-G[V][W]0;}}return Graph;
}//2.插入边
void InsertEdge(MGraph Graph,Edge E)
{//插入边V1,V2Graph-G[E-V1][E-V2]E-Weight;//无向图还要插入V2,V1Graph-G[E-V2][E-V1]E-Weight;
}int main()
{int i,j;MGraph Graph;Edge E[3];GraphCreateGraph(3);for(i0;i3;i){for(j0;j3;j){printf(G[%d][%d]%d ,i,j,Graph-G[i][j]);}}printf(\n);for(i0;i3;i){E[i](Edge)malloc(sizeof(struct ENode));}E[0]-V10;E[0]-V21;E[0]-Weight2;E[1]-V11;E[1]-V22;E[1]-Weight3;E[2]-V10;E[2]-V22;E[2]-Weight6;for(i0;i3;i){InsertEdge(Graph,E[i]);}for(i0;i3;i){ for(j0;j3;j){printf(G[%d][%d]%d ,i,j,Graph-G[i][j]);}}printf(\n);return 0;
}