图的相关操作(邻接表表示法)

图的相关操作(邻接表表示法)

一:邻接表的结构:

#define MAX_VERTEX_NUM 20   //定义最大顶点的数量
#define InfoType int     //图中弧或者是边的数据类型
typedef enum{DG,UDG}GraphKind;       //0-有向图,1-无向图
typedef int VetexType_Al;   //图中的顶点数据类型

/*
 * 表结点
 */
typedef struct ArcNode{
    int adjvex;             //顶点的序号
    struct ArcNode* nextarc;    //指向下一条弧的指针
    InfoType info;          //该弧的信息
}ArcNode;

/*
 * 头结点
 */
typedef struct VNode{
    VetexType_Al data;      //顶点信息
    ArcNode* firstarc;
}VNode;
typedef VNode AdjList[MAX_VERTEX_NUM];  //存放头信息的数组

/*
 * 图的邻接表存储表示
 */
typedef struct {
    AdjList vertices;   //头信息的数组
    int vexnum,arcnum;  //图中顶点的数量和边的数量
 //   int IncInfo;        //InInfo为0,则表示图中各弧不含其他信息
    GraphKind kind;     //图的种类
}ALGraph;

二:构造有向图与无向图的入口函数

/**
 * 创造函数的入口函数
 * @param G
 * @return
 */
int CreatGraph_AL(ALGraph* G){
    printf("Please input the kind of G:");
    scanf("%d",&G->kind);
    switch (G->kind) {
        case DG:
            return CreatDG_AL(G);
        case UDG:
            return CreatUDG_AL(G);
        default:break;
    }
    return 1;
}

三:构造有向图

//创建有向图
int CreatDG_AL(ALGraph* G){
    ArcNode* r[MAX_VERTEX_NUM];     //用作访问标记,用来定位
    ArcNode* p;                     //需要插入的结点

    printf("Please input the num of vex and arc:");
    scanf("%d,%d",&(G->vexnum),&(G->arcnum));
    getchar();

    //初始化数据
    printf("Please input the data of vex:\n");
    for (int i = 0; i < G->vexnum; ++i) {
        scanf("%d",&(G->vertices[i].data));
        getchar();
        G->vertices[i].firstarc = NULL;
        r[i] = NULL;
    }

    int v1,v2;
    //读取各边,开始制作邻接表
    for (int i = 0; i < G->arcnum; ++i) {
        printf("Please input the trail and head:");
        scanf("%d,%d",&v1,&v2);
        getchar();

        //这里的头和尾指的是,弧头的顶点和弧尾的顶点
        int trail = LocateVex_AL(*G,v1);
        int head = LocateVex_AL(*G,v2);

        //判断图中是否含有顶点
        if (head==-1||trail==-1){
            return 0;
        }
        //制作结点
        p = (ArcNode*)malloc(sizeof (ArcNode));
        if (!p){
            exit(0);    //分配内存失败
        }
        p->adjvex = head;
        p->nextarc = NULL;
        if (r[trail] == NULL){
            G->vertices[trail].firstarc = p;
        } else{
            r[trail]->nextarc = p;
        }
        r[trail] = p;
    }
    return 1;
}

四:构造无向图

//创建无向图
int CreatUDG_AL(ALGraph* G){
    ArcNode* r[MAX_VERTEX_NUM];
    ArcNode* p;
    ArcNode* q;

    printf("Please input the num of vex and arc:");
    scanf("%d,%d",&(G->vexnum),&(G->arcnum));
    getchar();

    //初始化
    for (int i = 0; i < G->vexnum; ++i) {
        printf("Please input the data of vex:");
        scanf("%d,%d",&(G->vertices[i].data));
        getchar();
        G->vertices [i].firstarc = NULL;
        r[i] = NULL;
    }

    int v1,v2;
    for (int i = 0; i < G->arcnum; ++i) {
        printf("Please input the trail and head:");
        scanf("%d,%d",&v1,&v2);
        getchar();

        //在无向图中就没有严格的弧的头尾概念,这里只是做一个区别而已
        int trail = LocateVex_AL(*G,v1);
        int head = LocateVex_AL(*G,v2);

        if (head == -1 || trail == -1 ){
            return 0;
        }

        //制作结点
        p = (ArcNode*)malloc(sizeof (ArcNode));
        if (!p){
            exit(0);
        }
        p->adjvex = head;
        p->nextarc = NULL;
        if (r[trail] == NULL){
            G->vertices[trail].firstarc = p;
        } else{
            r[trail]->nextarc = p;
        }
        r[trail] = p;

        q = (ArcNode*)malloc(sizeof (ArcNode));
        if (!q){
            exit(0);
        }
        q->adjvex = trail;
        q->nextarc = NULL;
        if (r[head] == NULL){
            G->vertices[head].firstarc = q;
        } else{
            r[head]->nextarc = q;
        }
        r[head] = q;
    }
    return 1;
}

五:获取顶点在数组中的下标

/**
 * 定位顶点在顶点数组中的位置,返回下标
 * @param G
 * @param v
 * @return      返回顶点在顶点数组中位置,下标,否则返回-1
 */
int LocateVex_AL(ALGraph G,VetexType_Al v){
    for (int i = 0; i < G.vexnum; ++i) {
        if (G.vertices[i].data == v){
            return i;
        }
    }
    return -1;
}

六:获取指定结点的第一个相邻顶点的下标值

/**
 * 获取与给定顶点的第一个相邻顶点在顶点数组中的下标
 * @param G
 * @param v
 * @return	返回顶点在顶点数组中的下标
 */
int FirstAdjVex_AL(ALGraph G,VetexType_Al v){
    int index = LocateVex_AL(G,v);
    if (index!=-1 && G.vertices[index].firstarc){
        return G.vertices[index].firstarc->adjvex;
    }
    return -1;
}

七:获取顶点v相对顶点w的下一个顶点的下标值

/**
 * 获取顶点v相对于顶点w的下一个邻接顶点,返回顶点在顶点数组中下标
 * @param G
 * @param v
 * @param w
 * @return
 */
int NextAdjVex_AL(ALGraph G,VetexType_Al v,VetexType_Al w){
    int indexv = LocateVex_AL(G,v);
    int indexw = LocateVex_AL(G,w);
    ArcNode* p;     //用来循环的指针
    if (indexv!=-1 && indexw!=-1 && G.vertices[indexv].firstarc){
        p = G.vertices[indexv].firstarc;
        while (p){
            if (p->adjvex == indexw){
                if (p->nextarc){
                    return p->nextarc->adjvex;
                } else{
                    return -1;
                }
            } else{
                p = p->nextarc;
            }
        }
    } else{
        return -1;
    }
    return -1;
}

八:深度优先遍历算法

/**
*使用邻接表和邻接矩阵表示的图,深度优先遍历的算法基本上没有改变,只是需要注意对于顶点数据的表示
*/

void visitVexAL(ALGraph G,int index){
    printf("%d\t",G.vertices[index].data);
}

//全局变量
bool visited[MAX_VERTEX_NUM];

//深度优先遍历算法
void DFSTraverse_AL(ALGraph G){
    //初始化访问变量
    for (int i = 0; i < G.vexnum; ++i) {
        visited[i] = false;
    }
    for (int i = 0; i < G.vexnum; ++i) {
        if (!visited[i]){   //没有被访问
            DFS_AL(G,i);
        }
    }

}
//深度优先遍历核心算法
void DFS_AL(ALGraph G,int indexv){

    visited[indexv] = true;
    visitVexAL(G,indexv);

    int w;
    //递归
    for (w = FirstAdjVex_AL(G,G.vertices[indexv].data);w>=0;
    w = NextAdjVex_AL(G,G.vertices[indexv].data,G.vertices[w].data)) {
        if (!visited[w]){
            DFS_AL(G,w);
        }
    }
}

九:广度优先遍历算法

/**
 * 广度优先遍历,使用数组来模拟队列的操作
 * 类似于二叉树的层次遍历
 * 邻接矩阵和邻接表的广度优先遍历算法基本一样,同样也只是需要注意对于顶点数据的表示
 * @param G
 */
void BFSTraverse_AL(ALGraph G){

    //对访问标志数组进行初始化操作
    for (int i = 0; i < G.vexnum; ++i) {
        visited[i] = false;
    }

    //用数组来模拟队列
    VetexType_Al queue[20];
    int top = 0;    //头
    int rear = 0;   //尾

    for (int i = 0; i < G.vexnum; ++i) {
        if (!visited[i]){   //如果没有别访问
            visited[i] = true;
            visitVexAL(G,i);    //访问

            //进队列中
            queue[rear] = G.vertices[i].data;
            rear = (rear+1)%20;
            while (rear-top != 0){      //队列非空时
                VetexType_Al v = queue[top];
                top = (top+1)%20;
                for (int w = FirstAdjVex_AL(G,v); w >=0 ; w = NextAdjVex_AL(G,v,G.vertices[w].data)) {
                    if (!visited[w]){
                        visited[w] = true;
                        visitVexAL(G,w);
                        queue[rear] = G.vertices[w].data;
                        rear = (rear+1)%20;
                    }
                }
            }
        }
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表是一种的表示方法,它使用链表来存储每个节点的邻居节点。在C语言中,可以使用结构体和指针来实现邻接表。以下是一个示例代码: ``` #include <stdio.h> #include <stdlib.h> // 邻接表节点结构体 typedef struct AdjListNode { int dest; struct AdjListNode* next; } AdjListNode; // 邻接表头结构体 typedef struct AdjList { AdjListNode* head; } AdjList; // 结构体 typedef struct Graph { int V; AdjList* array; } Graph; // 创建邻接表节点 AdjListNode* newAdjListNode(int dest) { AdjListNode* newNode = (AdjListNode*)malloc(sizeof(AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } // 创建 Graph* createGraph(int V) { Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->V = V; graph->array = (AdjList*)malloc(V * sizeof(AdjList)); for (int i = 0; i < V; i++) { graph->array[i].head = NULL; } return graph; } // 添加边 void addEdge(Graph* graph, int src, int dest) { AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } // 打印邻接表 void printGraph(Graph* graph) { for (int i = 0; i < graph->V; i++) { AdjListNode* node = graph->array[i].head; printf("邻接表节点 %d 的邻居节点:", i); while (node) { printf("%d ", node->dest); node = node->next; } printf("\n"); } } int main() { Graph* graph = createGraph(5); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); printGraph(graph); return 0; } ``` 这段代码实现了一个无向邻接表表示法,可以通过调用 `createGraph` 函数创建一个具有 `V` 个节点的,然后通过 `addEdge` 函数添加边,最后通过 `printGraph` 函数打印邻接表

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值