操作用邻接表存储的图

问题及代码

问题描述: 假设图G采用邻接表存储,分别设计实现以下要求的算法  
输入描述:若干测试数据。 
程序输出:DFS,BFS的遍历序列。  

**利用下图作为测试用图,输出结果。**


图基本算法库


(1)输出出图G中每个顶点的出度代码:

#include "graph.h"  
  
//返回图G中编号为v的顶点的出度  
int OutDegree(ALGraph *G,int v)  
{  
    ArcNode *p;  
    int n=0;  
    p=G->adjlist[v].firstarc;  
    while (p!=NULL)  
    {  
        n++;  
        p=p->nextarc;  
    }  
    return n;  
}  
//输出图G中每个顶点的出度  
void OutDs(ALGraph *G)  
{  
    int i;  
    for (i=0; i<G->n; i++)  
        printf("  顶点%d:%d\n",i,OutDegree(G,i));  
}  
int main()  
{  
    ALGraph *G;  
    int A[7][7]=  
    {  
        {0,1,1,1,0,0,0},  
        {0,0,0,0,1,0,0},  
        {0,0,0,0,1,1,0},  
        {0,0,0,0,0,0,1},  
        {0,0,0,0,0,0,0},  
        {0,0,0,1,1,0,1},  
        {0,1,0,0,0,0,0}  
    };  
    ArrayToList(A[0], 7, G);  
    printf("各顶点出度:\n");  
    OutDs(G);  
    return 0;  
}

运行结果

这里写图片描述


(2)求出图G中出度最大的一个顶点,输出该顶点编号代码:

#include "graph.h"  
  
//返回图G中编号为v的顶点的出度  
int OutDegree(ALGraph *G,int v)  
{  
    ArcNode *p;  
    int n=0;  
    p=G->adjlist[v].firstarc;  
    while (p!=NULL)  
    {  
        n++;  
        p=p->nextarc;  
    }  
    return n;  
}  
  
//输出图G中每个顶点的出度  
void OutDs(ALGraph *G)  
{  
    int i;  
    for (i=0; i<G->n; i++)  
        printf("  顶点%d:%d\n",i,OutDegree(G,i));  
}  
  
//输出图G中出度最大的一个顶点  
void OutMaxDs(ALGraph *G)  
{  
    int maxv=0,maxds=0,i,x;  
    for (i=0; i<G->n; i++)  
    {  
        x=OutDegree(G,i);  
        if (x>maxds)  
        {  
            maxds=x;  
            maxv=i;  
        }  
    }  
    printf("顶点%d,出度=%d\n",maxv,maxds);  
}  
  
int main()  
{  
    ALGraph *G;  
    int A[7][7]=  
    {  
        {0,1,1,1,0,0,0},  
        {0,0,0,0,1,0,0},  
        {0,0,0,0,1,1,0},  
        {0,0,0,0,0,0,1},  
        {0,0,0,0,0,0,0},  
        {0,0,0,1,1,0,1},  
        {0,1,0,0,0,0,0}  
    };  
    ArrayToList(A[0], 7, G);  
  
    printf("最大出度的顶点信息:");  
    OutMaxDs(G);  
  
    return 0;  
}

运行结果

这里写图片描述


(3)计算图G中出度为0的顶点数;代码:

#include "graph.h"  
  
//返回图G中编号为v的顶点的出度  
int OutDegree(ALGraph *G,int v)  
{  
    ArcNode *p;  
    int n=0;  
    p=G->adjlist[v].firstarc;  
    while (p!=NULL)  
    {  
        n++;  
        p=p->nextarc;  
    }  
    return n;  
}  
  
//输出图G中每个顶点的出度  
void OutDs(ALGraph *G)  
{  
    int i;  
    for (i=0; i<G->n; i++)  
        printf("  顶点%d:%d\n",i,OutDegree(G,i));  
}  
  
//输出图G中出度为0的顶点数  
void ZeroDs(ALGraph *G)  
{  
    int i,x;  
    for (i=0; i<G->n; i++)  
    {  
        x=OutDegree(G,i);  
        if (x==0)  
            printf("%2d",i);  
    }  
    printf("\n");  
}  
  
int main()  
{  
    ALGraph *G;  
    int A[7][7]=  
    {  
        {0,1,1,1,0,0,0},  
        {0,0,0,0,1,0,0},  
        {0,0,0,0,1,1,0},  
        {0,0,0,0,0,0,1},  
        {0,0,0,0,0,0,0},  
        {0,0,0,1,1,0,1},  
        {0,1,0,0,0,0,0}  
    };  
    ArrayToList(A[0], 7, G);  
  
    printf("出度为0的顶点:");  
    ZeroDs(G);  
  
    return 0;  
}

运行结果

这里写图片描述


(4)判断图G中是否存在边<i,j>

#include "graph.h"  
  
//返回图G中编号为v的顶点的出度  
int OutDegree(ALGraph *G,int v)  
{  
    ArcNode *p;  
    int n=0;  
    p=G->adjlist[v].firstarc;  
    while (p!=NULL)  
    {  
        n++;  
        p=p->nextarc;  
    }  
    return n;  
}  
  
//输出图G中每个顶点的出度  
void OutDs(ALGraph *G)  
{  
    int i;  
    for (i=0; i<G->n; i++)  
        printf("  顶点%d:%d\n",i,OutDegree(G,i));  
}  
  
//输出图G中出度最大的一个顶点  
void OutMaxDs(ALGraph *G)  
{  
    int maxv=0,maxds=0,i,x;  
    for (i=0; i<G->n; i++)  
    {  
        x=OutDegree(G,i);  
        if (x>maxds)  
        {  
            maxds=x;  
            maxv=i;  
        }  
    }  
    printf("顶点%d,出度=%d\n",maxv,maxds);  
}  
//输出图G中出度为0的顶点数  
void ZeroDs(ALGraph *G)  
{  
    int i,x;  
    for (i=0; i<G->n; i++)  
    {  
        x=OutDegree(G,i);  
        if (x==0)  
            printf("%2d",i);  
    }  
    printf("\n");  
}  
  
//返回图G中是否存在边<i,j>  
bool Arc(ALGraph *G, int i,int j)  
{  
    ArcNode *p;  
    bool found = false;  
    p=G->adjlist[i].firstarc;  
    while (p!=NULL)  
    {  
        if(p->adjvex==j)  
        {  
            found = true;  
            break;  
        }  
        p=p->nextarc;  
    }  
    return found;  
}  
  
int main()  
{  
    ALGraph *G;  
    int A[7][7]=  
    {  
        {0,1,1,1,0,0,0},  
        {0,0,0,0,1,0,0},  
        {0,0,0,0,1,1,0},  
        {0,0,0,0,0,0,1},  
        {0,0,0,0,0,0,0},  
        {0,0,0,1,1,0,1},  
        {0,1,0,0,0,0,0}  
    };  
    ArrayToList(A[0], 7, G);  
  
    printf("边<2,6>存在吗?");  
    if(Arc(G,2,6))  
        printf("是\n");  
    else  
        printf("否\n");  
    printf("\n");  
      
    return 0;  
}

运行结果

这里写图片描述


知识点总结:

图算法库的应用。


学习心得:

一步步的实现这个程序有过程感。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
邻接表是一种常用的存储结构,它通过链表来表示每个顶点的邻接点集合。邻接表的具体实现可以使用数组加链表的方式,也可以使用纯链表的方式。 邻接表存储结构的优点是可以很方便地遍历某个顶点的邻接点,而且可以节省空间。但是,对于需要查找两个顶点之间是否有边的操作邻接表的效率就不如邻接矩阵了。 下面是邻接表存储结构定义: ```c // 邻接表存储结构定义 #define MAX_VERTEX_NUM 100 // 最大顶点数 typedef struct ArcNode { // 边结点 int adjvex; // 邻接点在顶点数组的下标 struct ArcNode *next; // 指向下一个边结点的指针 // 也可以在这里存储边的权值等信息 } ArcNode; typedef struct VNode { // 顶点结点 char data; // 顶点的数据 ArcNode *firstarc; // 指向第一个边结点的指针 } VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { // 邻接表 AdjList vertices; // 顶点数组 int vexnum, arcnum; // 顶点数和边数 } ALGraph; ``` 邻接表的遍历可以使用深度优先搜索(DFS)或广度优先搜索(BFS)算法。下面是DFS的遍历代码: ```c // 邻接表的深度优先搜索遍历 void DFS(ALGraph *G, int v, bool visited[]) { visited[v] = true; printf("%c ", G->vertices[v].data); // 访问顶点v ArcNode *p = G->vertices[v].firstarc; while (p != NULL) { // 遍历v的所有邻接点 int w = p->adjvex; if (!visited[w]) { DFS(G, w, visited); } p = p->next; } } ``` BFS的遍历代码如下: ```c // 邻接表的宽度优先搜索遍历 void BFS(ALGraph *G, int v, bool visited[]) { visited[v] = true; printf("%c ", G->vertices[v].data); // 访问顶点v queue<int> Q; // 使用队列存储待访问的顶点 Q.push(v); while (!Q.empty()) { int u = Q.front(); Q.pop(); ArcNode *p = G->vertices[u].firstarc; while (p != NULL) { // 遍历u的所有邻接点 int w = p->adjvex; if (!visited[w]) { visited[w] = true; printf("%c ", G->vertices[w].data); // 访问顶点w Q.push(w); } p = p->next; } } } ``` 以上就是邻接表存储和遍历操作。需要注意的是,邻接表实现也可以使用其他数据结构,这里给出的只是一种常见的方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想变成橙子的西红柿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值