图的表示

删除回忆录丶 提交于 2019-11-27 21:23:12
  1. 邻接矩阵

输入格式:

Nv,Ne  //定点数,边数

V1,V2,weight   //一条边权重

 1 typedef struct GNode *PtrToGNode;
 2 struct GNode{
 3     int Nv;//node number
 4     int Ne;//edge number
 5     int G[maxvertexnumber][maxvertexnumber];//matrix
 6     //int Data[maxvertexnumber]; data
 7 }
 8 typedef PtrToGNode MGraph;
 9 //初始化一个有Nv个顶点,没有边的图
10 MGraph CreatGraph(int Nv){
11     int V,W;
12     MGraph Graph;
13     Graph=(MGraph)malloc(sizeof(struct GNode));
14     Graph->Nv=Nv;
15     Graph->Ne=0;
16     for(V=0;V<Graph->Nv;V++){
17         for(W=0;W<Graph->Nv;W++){
18             Graph->G[V][W]=0;
19         }
20     }
21     return Graph;
22 }
23 //向图中插入边
24 struct ENode{
25     int V1,V2;//两个顶点
26     int weight;//权重
27 }
28 typedef struct ENode *Edge;
29 void InsertEdge(MGraph Graph,Edge E){
30     Graph->G[E->V1][E->V2]=E->weight;
31     //无向图
32     Graph->G[E->V2][E->V1]=E->weight;
33 }
34 
35 MGraph BuildGraph(){
36     MGraph Graph;
37     int Nv,i;
38     Edge E;
39     scanf("%d",&Nv);
40     Graph=CreatGraph(Nv);
41     scanf("%d",&(Graph->Ne));
42     if(Graph->Ne!=0){
43         E=(Edge)malloc(sizeof(struct ENode));
44         for(i=0;i<Graph->Ne;i++){
45             scanf("%d %d %d",&(E->V1),&(E->V2),&(E->weight));
46             InsertEdge(Graph,E);
47         }
48     }
49     return Graph;
50 }

 

2.邻接表(输入格式与上面相同)

 1 typedef struct VNode *PtrToVnode;
 2 struct VNode{
 3     int AdjV;       //邻接表一个节点
 4     int Weight;
 5     PtrToVnode Next;
 6 }
 7 
 8 typedef struct PtrNode{
 9     PtrToVnode FirstEdge;
10 }AdjList[MaxVertexNumber];
11 
12 struct GNode{
13     int Nn,Ne;
14     AdjList G;
15 }
16 typedef struct GNode *LGraph;
17 //初始化一个有Nn个顶点,无边的图
18 CreatGraph(int Nn){
19     int i;
20     LGraph Graph;
21     Graph=(LGraph)malloc(sizeof(struct GNode));
22     Graph->Nn=Nn;
23     Graph->Ne=0;
24     for(i=0;i<Graph->Nn;i++){
25         Graph->G[i].FirstEdge=NULL;
26     }
27     return Graph;
28 }
29 
30 struct ENode{
31     int V1,V2;//两个顶点
32     int weight;//权重
33 }
34 typedef struct ENode *Edge;
35 
36 void InsertEdge(LGraph Graph,Edge E){
37     PtrToVnode NewNode;
38     NewNode=(PtrToVnode)malloc(sizeof(struct VNode));
39     NewNode->AdjV=E->V2;
40     NewNode->Weight=E->weight;
41     NewNode->next=Graph->G[E->V1].FirstEdge;
42     Graph->G[E->V1].FirstEdge=NewNode;
43     //无向图+
44     NewNode=(PtrToVnode)malloc(sizeof(struct VNode));
45     NewNode->AdjV=E->V1;
46     NewNode->Weight=E->weight;
47     NewNode->next=Graph->G[E->V2].FirstEdge;
48     Graph->G[E->V2].FirstEdge=NewNode;
49 }
50 
51 LGraph BuildGraph(){
52     LGraph Graph;
53     int Nn,i;
54     Edge E;
55     scanf("%d",&Nn);
56     Graph=CreatGraph(Nn);
57     scanf("%d",&(Graph->Ne));
58     if(Graph->Ne!=0){
59         E=(Edge)malloc(sizeof(struct ENode));
60         for(i=0;i<Graph->Ne;i++){
61             scanf("%d %d %d",&(E->V1),&(E->V2),&(E->weight));
62             InsertEdge(Graph,E);
63         }
64     }
65     return Graph;
66 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!