图--邻接矩阵实现

图:多对多的关系
存储结构:1)邻接矩阵
2)邻接表
3) 十字链表:是有向图的另一种链式存储结构。是将有向图的邻接表和邻接矩阵结合起来的一种链表
4) 邻接多重表:是无向图的另一种链式存储结构。

实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include<stdlib.h>

//图的邻接矩阵表示方法
#define INF INT_MAX
#define MAX_VERTEX_NUM 20
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef int VRType;
typedef int InfoType;
typedef int VertexType;

typedef struct{
VRType adj; //VRType是顶点关系类型,对无权图 用1 或 0 表示相邻否
//对于带权图,则为权值类型
InfoType *info;
}ArcCell, AdjMaxtrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
VertexType vexs[MAX_VERTEX_NUM];
AdjMaxtrix arcs;
int vexnum, arcnum;
GraphKind kind;
}MGraph;

int graph[6][6] = {
INF, 5, INF, 7, INF, INF,
INF, INF, 4, INF, INF, INF,
8, INF, INF, INF, INF, 9,
INF, INF, 5, INF, INF, 6,
INF, INF, INF, 5, INF, INF,
3, INF, INF, INF, 1, INF
};
int createDG(MGraph &M){
M.vexnum = 6;
M.arcnum = 10;
for (int i = 0; i < M.vexnum; i++){
for (int j = 0; j < M.vexnum; j++){
M.arcs[i][j].adj = graph[i][j];
}
}
return 1;
}

void DisplayMatix(MGraph &G){
for (int i = 0; i < G.vexnum; i++){
for (int j = 0; j < G.vexnum; j++){
if (G.arcs[i][j].adj == INF){
printf("oo ");
}
else{
printf("%d ", G.arcs[i][j].adj);
}
}
printf("\n");
}
}
int main(){
MGraph G;
createDG(G);
DisplayMatix(G);
return 1;
}

文章目录
,