图的表示

  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 *ListNode;
 2 struct VNode{
 3     int AdjV;       //邻接表一个节点
 4     int Weight;
 5     ListNode Next;
 6 }
 7 
 8 typedef struct PtrNode{
 9     ListNode 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     ListNode NewNode;
38     NewNode=(listNode)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=(ListNode)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 }

 

posted @ 2019-08-19 15:51 有点锋芒 阅读(...) 评论(...) 编辑 收藏