【C语言数据结构】图-邻接矩阵法


图-邻接矩阵法


代码实现

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//定义最大顶点数为100,也就是这个图里最多有100个顶点
#define MaxVertexNum 100

//给顶点类型设置为char(其实本质是给char取了个别名叫VertexType),也就是说顶点为'A'点,'B'点等。
typedef char VertexType;

//同理,给边的类型设置为int,其实对于邻接矩阵法来说,边为1代表有连接,边为0代表无连接。
typedef int EdgeType;

typedef struct {
    //建立一个一维数组,存储对应的顶点,也就是G=(V,E)中的V
    VertexType Vertex[MaxVertexNum];

    //建立一个二维数组,存储对应的边的集合,也就是G=(V,E)中的E
    EdgeType Edge[MaxVertexNum][MaxVertexNum];

    //定义顶点的数量和边的数量(英文中arc有弧的意思,这里指边)
    int vexnum, arcnum;
} MGraph;

void FindVertex(MGraph *G, VertexType x, VertexType y, int *index_x, int *index_y);

//初始化邻接矩阵的图
void InitGraph(MGraph *G) {
    G->vexnum = 0;
    G->arcnum = 0;
}

//添加顶点
bool InsertVertex(MGraph *G, VertexType vertex) {
    //如果顶点已满,添加失败
    if (G->vexnum >= MaxVertexNum)
        return false;

    G->Vertex[G->vexnum] = vertex;
    G->vexnum++;
    return true;
}

//判断是否有边(x,y)或<x,y>
void FindVertex(MGraph *G, VertexType x, VertexType y, int *index_x, int *index_y) {
    (*index_x) = -1;
    (*index_y) = -1;//定义x顶点和y顶点的索引
//遍历赋值,找出x和y的索引
    for (int i = 0; i < (*G).vexnum; i++) {
        if ((*G).Vertex[i] == x)
            (*index_x) = i;
        else if ((*G).Vertex[i] == y)
            (*index_y) = i;
        if ((*index_x) != -1 && (*index_y) != -1)
            break;
    }
}

bool Adjacent(MGraph G, VertexType x, VertexType y) {
    int index_x;
    int index_y;
    FindVertex(&G, x, y, &index_x, &index_y);

    //如果有索引未找到,那么就说明不存在边(或者说其中某个点不存在)
    if (index_x == -1 || index_y == -1)
        return false;

    //如果有边,那么edge就为1
    return G.Edge[index_x][index_y] == 1;
}

/*懒得写了,就简单的写一下构建图有关的函数就行了,以下函数先欠着,有时间再写:
    Neighbors(G,x)
    InsertVertex(G,x)
    DeleteVertex(G,x)
    AddEdge(G,x,y)
    RemoveEdge(G,x,y)
    FirstNeighbor(G,x)
    NextNeighbor(G,x,y)
    Get_edge_value(G,x,y)
    Set_edge_value(G,x,y,v)
*/

//创建边(无向图)
bool AddEdgeU(MGraph *G, VertexType x, VertexType y) {
    //先判断顶点是否存在
    int index_x;
    int index_y;
    FindVertex(G, x, y, &index_x, &index_y);
    //如果有索引未找到,那么就说明不存在某个顶点
    if (index_x == -1 || index_y == -1)
        return false;

    G->Edge[index_x][index_y] = 1;
    G->Edge[index_y][index_x] = 1;

    return true;
}

//创建边(有向图)
bool AddEdgeD(MGraph *G, VertexType x, VertexType y) {
    //先判断顶点是否存在
    int index_x;
    int index_y;
    FindVertex(G, x, y, &index_x, &index_y);
    //如果有索引未找到,那么就说明不存在某个顶点
    if (index_x == -1 || index_y == -1)
        return false;

    G->Edge[index_x][index_y] = 1;
    return true;
}

//打印矩阵
bool PrintGraph(MGraph G) {
    printf("\t");
    for(int i = 0;i<G.vexnum;i++)
        printf("%c\t",G.Vertex[i]);
    printf("\n");
    for (int i = 0; i < G.vexnum; i++) {
        printf("%c\t",G.Vertex[i]);
        for (int j = 0; j < G.vexnum; j++)
            printf("%d\t", G.Edge[i][j]);
        printf("\n");
    }
}

int main() {
    //创建图
    MGraph G;
    //初始化图
    InitGraph(&G);

    //插入顶点
    InsertVertex(&G, 'A');
    InsertVertex(&G, 'B');
    InsertVertex(&G, 'C');
    InsertVertex(&G, 'D');
    InsertVertex(&G, 'E');

    //插入边
    AddEdgeU(&G, 'A', 'B');
    AddEdgeU(&G, 'A', 'C');
    AddEdgeU(&G, 'B', 'D');
    AddEdgeU(&G, 'C', 'D');
    AddEdgeU(&G, 'C', 'E');
    AddEdgeU(&G, 'D', 'E');

    //打印图邻接矩阵
    PrintGraph(G);

    //判断是否存在A-B边
    if(!Adjacent(G,'A','B'))
        printf("不存在A-B边!\n");
    else{
        printf("存在A-B边!\n");
    }

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值