图的邻接矩阵表示

代码:

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100 // 最大顶点数设为 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]; // 存顶点的数据
    /* 注意: 若顶点无数据, 此时 Data[] 可以不用出现 */
};
typedef PtrToGNode MGraph;
// 边的定义
typedef struct ENode * PtrToENode;
struct ENode {
    Vertex V1, V2; // 有向边 <v1, V2>
    WeightType Weight; // 权重
};
typedef PtrToENode Edge;

MGraph CreateGraph(int VertexNum)
{
    // 初始化一个有 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;

    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;

    scanf("%d", &Nv); // 读入顶点个数
    Graph = CreateGraph(Nv); // 初始化有 Nv 个顶点但没有边的图

    scanf("%d", &(Graph->Ne)); // 读入边数
    if (Graph->Ne != 0) // 如果有边
    {
        E = (Edge)malloc(sizeof(struct ENode)); // 建立边节点
        // 读入边, 格式为 "起点 终点 权重", 插入邻接矩阵
        for (i = 0; i < Graph->Ne; i++) {
            scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
            // 注意: 如果权重不是整型, Weight 的读入格式要改
            InsertEdge(Graph, E);
        }
    }
    // 如果顶点有数据的话, 读入数据
    /*for (V = 0; V < Graph->Nv; V++)
        scanf("%c", &(Graph->Data[V]));*/

    return Graph;
}

// 简单遍历图
void PrintGraph(MGraph G)
{
    int i, j;

    for (i = 0; i < G->Nv; i++)
    {
        for (j = 0; j < G->Nv; j++)
        {
            if (G->G[i][j] == INFINITY)
            {
                printf("∞ ");
                continue;
            }
            printf("%d ", G->G[i][j]);
        }
        printf("
");
    }
}

// 测试一组数据, 测试的图有 5 个顶点, 8 条有向边
// <1, 0, 9> <0, 2, 6> <2, 4, 7> <4, 3, 6> <3, 1, 5> <1, 2, 4> <0, 3, 3> <3, 4, 8>
int main()
{

    MGraph G = BuildGraph();
    PrintGraph(G);

    return 0;
}

// 测试数据
/*
5
8
1 0 9
0 2 6
2 4 7
4 3 6
3 1 5
1 2 4
0 3 3
3 4 8
*/

输出结果:

20201117124430

图的示意:

20201117203615

20201120005929

参考:《数据结构第 2 版》(陈越) & 浙江大学数据结构 MOOC

原文地址:https://www.cnblogs.com/fanlumaster/p/13993632.html