当前位置: 首页 > news >正文

图——邻接表基本操作算法实现

一、存储结构

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
//邻接表
#define MAXVertexNum 100	//最大顶点数
typedef char VerTexType;
typedef int ArcType;
typedef int Otherinfo;typedef struct ArcNode{    //边表结点 int adjvex;  //该边所指向的顶点的位置 struct ArcNode *nextarc;//指向下一条边的指针 Otherinfo weight;  //网的边权值 
}ArcNode;typedef struct VNode{    //顶点结点VerTexType data;  //顶点信息ArcNode *firstarc;   //指向第一条依附该顶点的边的指针 
}VNode,AdjList[MAXVertexNum];//AdjList表示邻接表类型typedef struct{    AdjList vertices;    //邻接表int vexnum,arcnum;    //图的当前顶点数和弧数bool isUndirected;
}ALGraph;

二、初始化操作

//初始化图
void InitGraph(ALGraph &G, bool isUndirected=1) {G.vexnum = 0;      // 初始化顶点数为 0G.arcnum = 0;      // 初始化边数为 0G.isUndirected = isUndirected;  // 设置图的类型(有向图或无向图)// 初始化每个顶点的边表为空for (int i = 0; i < MAXVertexNum; i++) {G.vertices[i].firstarc = NULL;  // 每个顶点的第一条边指针都设置为 NULL}
}

三、查找图中指定顶点的位置

/*在图的顶点数组 vertices 中遍历所有顶点,查找与顶点 v 相等的顶点。如果找到了,就返回该顶点的索引;如果没有找到该顶点,则返回 -1*/

int LocateVertex(ALGraph G, VerTexType v) {for (int i = 0; i < G.vexnum; i++) {if (G.vertices[i].data == v) return i;}return -1;
}

四、插入顶点操作

void InsertVertex(ALGraph &G, VerTexType v) {if (G.vexnum >= MAXVertexNum) return;  // 如果顶点数已满,返回// 将顶点 v 插入到顶点表中G.vertices[G.vexnum].data = v;G.vertices[G.vexnum].firstarc = NULL;  // 插入的新顶点没有边G.vexnum++;  // 顶点数加 1
}

五、插入边操作

void AddEdge(ALGraph &G, VerTexType v1, VerTexType v2, Otherinfo weight=1) {int i = LocateVertex(G, v1);  // 查找顶点 v1 的位置int j = LocateVertex(G, v2);  // 查找顶点 v2 的位置if (i == -1 || j == -1) return;  // 如果没有找到顶点,返回// 创建新的边结点 ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));p->adjvex = j;  // 记录边指向的顶点p->weight = weight;  // 设置边的权重p->nextarc = G.vertices[i].firstarc;  // 新边指向原来第一个边G.vertices[i].firstarc = p;  // 更新顶点 v1 的第一条边指针// 如果是无向图,则插入反向的边if (G.isUndirected) {ArcNode *q = (ArcNode *)malloc(sizeof(ArcNode));q->adjvex = i;q->weight = weight;q->nextarc = G.vertices[j].firstarc;G.vertices[j].firstarc = q;}G.arcnum++;  // 边数加 1
}

六、删除边操作

/* 
与链表不同在于这里传入的参数是图,类型是ALGrapg结构体 
而链表传入的是头指针,类型是指针结构 LinkList=Lnode*
InsertList(LinkList &L) InsertList(L) ===  InsertList(LinkList *L) InsertList(&L) ===  InsertList(LNode **L) InsertList(&L)
由于并不能直接在 RemoveEdge传入某个顶点的头指针
因此 在函数体内部 只能采用  ArcNode *(*p) = &G.vertices[i].firstarc;
*/

//删除边 
void RemoveEdge(ALGraph &G, VerTexType v1, VerTexType v2) {  int i = LocateVertex(G, v1);int j = LocateVertex(G, v2);if (i == -1 || j == -1) return;// 查找并删除 v1 -> v2 的边ArcNode **p = &G.vertices[i].firstarc;  //*p接受firstarc这个指针的地址 while (*p) {if ((*p)->adjvex == j) {ArcNode *temp = *p;*p = temp->nextarc;free(temp);G.arcnum--;  // 边数减 1 break;}p = &(*p)->nextarc;}// 如果是无向图,还需要删除反向边if (G.isUndirected) {p = &G.vertices[j].firstarc;while (*p) {if ((*p)->adjvex == i) {ArcNode *temp = *p;*p = temp->nextarc;free(temp);break;}p = &(*p)->nextarc;}}}

七、删除顶点操作

// 删除顶点 
void DeleteVertex(ALGraph &G, VerTexType v) {int idx = LocateVertex(G, v);if (idx == -1) return;// 删除有向图中所有指向该顶点的边,无向图中双方向的边 for (int i = 0; i < G.vexnum; i++) {if (i == idx) continue;RemoveEdge(G, G.vertices[i].data, v);}// 删除有向图中该顶点的边if (G.isUndirected==0) {ArcNode *p = G.vertices[idx].firstarc;  //此处不动顶点结点,可以不动用&G while (p) {ArcNode *temp = p;p = p->nextarc;free(temp);G.arcnum--;}}// 移动顶点数组for (int i = idx; i < G.vexnum - 1; i++) {G.vertices[i] = G.vertices[i + 1];}G.vexnum--;// 更新所有边的邻接点索引for (int i = 0; i < G.vexnum; i++) {ArcNode *p = G.vertices[i].firstarc;while (p) {if (p->adjvex > idx) {p->adjvex--;}p = p->nextarc;}}
}

八、列出图中节点 x 的邻接的边 

// 列出图中节点 x 的邻接的边 
void Neighbors(ALGraph G, VerTexType v) {int i = LocateVertex(G, v);if (i == -1) return;printf("邻接的边 %c: ", v);ArcNode *p = G.vertices[i].firstarc;while (p) {if(G.isUndirected==0) printf("<%c,%c> ", v,G.vertices[p->adjvex].data);if(G.isUndirected==1) printf("(%c,%c) ", v,G.vertices[p->adjvex].data);p = p->nextarc;}if(G.isUndirected==0){for (int j = 0; j < G.vexnum; j++) {if (j == i) continue;ArcNode *p = G.vertices[j].firstarc;  while (p) {if (p->adjvex == i) {printf("<%c,%c>", G.vertices[j].data,v);}p = p->nextarc;}}	}printf("\n");
}

九、输出查看邻接矩阵

//输出操作 
void DisplayGraph(ALGraph G) {for (int i = 0; i < G.vexnum; i++) {printf("%c: ", G.vertices[i].data);ArcNode *p = G.vertices[i].firstarc;while (p) {printf("-> %c(%d) ", G.vertices[p->adjvex].data, p->weight);p = p->nextarc;}printf("\n");}
}

十、主函数

int main() {ALGraph G;InitGraph(G,0); InsertVertex(G, 'A');InsertVertex(G, 'B');InsertVertex(G, 'C');InsertVertex(G, 'D');InsertVertex(G, 'E');AddEdge(G, 'A', 'B');AddEdge(G, 'A', 'D');AddEdge(G, 'A', 'C');AddEdge(G, 'C', 'A');AddEdge(G, 'B', 'E');AddEdge(G, 'C', 'D');DisplayGraph(G);printf("\n边数: %d\n", G.arcnum);Neighbors(G,'C');DeleteVertex(G, 'C');printf("\n删除C后:\n");DisplayGraph(G);printf("\n边数: %d\n", G.arcnum);return 0;
}
http://www.dtcms.com/a/302805.html

相关文章:

  • 【SpringMVC】SpringMVC的概念、创建及相关配置
  • 对比分析 OceanBase 与数据库中间件
  • Java 数学工具类 Math
  • 6、CentOS 9 安装 Docker
  • 香港Web3媒体Techub News活动大事记:时间线全记录
  • 将 NI Ettus USRP X410 的文件系统恢复出厂设置
  • CMake简单教程
  • 智能指挥调度系统:数字化时代的协同决策中枢
  • 从0到1学PHP(一):PHP 基础入门:开启后端开发之旅
  • 基于 OpenCV 与 sklearn 的数字识别:KNN 算法实践
  • 【CDA干货】金融超市电商App经营数据分析案例
  • 星辰大海的征途:星宸科技的中国芯片突围战
  • 【行测】常识判断1
  • 【Unity笔记03】#if的用法和命名空间
  • EXCEL怎么提取表名
  • 在CentOS上以源码编译的方式安装PostgreSQL
  • 【51单片机2位数码管跑马灯】2022-9-25
  • 时间数字转换器TDC的FPGA方案及核心代码
  • 51单片机如何实现round函数
  • Java 大视界 -- 基于 Java 的大数据实时流处理在智能电网分布式能源接入与电网稳定性保障中的应用(368)
  • 【Linux】重生之从零开始学习运维之mysql用户管理
  • live-server的使用以及离线环境安装
  • CMake、CMakeLists.txt 基础语法
  • Linux系统之Ansible安装与入门
  • WPF,窗口拖动事件与窗口内控件点击事件
  • c++ 中的字符串相关的操作
  • python办自动化--利用vba或者python按需求读取excel文件指定列,更改列名后,按照要求将列排序,最后填充空白单元格
  • k8s中Nvidia节点驱动的配置问题
  • Go 语言-->指针
  • 2025年人工智能三大突破:多模态推理、具身智能与全球治理