图的邻接表建立以及DFS和BFS
来源:互联网 发布:coc淘宝买宝石安全吗 编辑:程序博客网 时间:2024/06/07 00:14
- 图的存储方式用的邻接表,邻接表链表用的头插法插入数据,图的定点数组用的自定义数据类型数组即
struct []
。 - BFS用一个队列实现,学习使用就没有用标准库的队列,用的自己写的一个队列,包含在用户自定义头文件
myQueue.h
当中。 - DFS使用递归方法遍历图
下面是图的建立和遍历代码,最后面附上自己写的头文件myQueue.h
:
#include <iostream>#include <stdio.h>#include <stdlib.h>#include "myQueue.h"using namespace std;#define TRUE 1#define FALSE 0#define MAXVERTEX 100int visit[MAXVERTEX] = {FALSE};typedef char VertexType;typedef int EdgeType;struct EdgeNode{ int VertexIndex; EdgeType data; EdgeNode *next;};typedef struct VertexNode{ VertexType data; EdgeNode *firstedge;} VertexList[MAXVERTEX];struct GraphAdjList{ VertexNode VertexNodeList[MAXVERTEX]; int numVertex; int numEdge;};void initAdjListGraph(GraphAdjList *G){ G->numEdge = 0; G->numVertex = 0;}void CreateAdjListGraph(GraphAdjList *G){ cout << "输入顶点个数和边的个数(空格分隔,如3 4 后回车键结束):" << endl; cin >> G->numVertex >> G->numEdge; cout << "输入定点的命名(空格分隔,如a b c d e 后回车键结束):" << endl; for (int i = 0; i < G->numVertex; i++) { cin >> G->VertexNodeList[i].data; G->VertexNodeList[i].firstedge = NULL; } cout << "输入边的关系(如 0 1 回车键 1 2 回车键。。。。直至所有的边输入完毕):" << endl; for (int k = 0; k < G->numEdge; k++) { int i, j; cin >> i >> j; EdgeNode *NewEdgeNode = (EdgeNode *)malloc(sizeof(EdgeNode)); NewEdgeNode->VertexIndex = j; NewEdgeNode->next = G->VertexNodeList[i].firstedge; (G->VertexNodeList[i]).firstedge = NewEdgeNode; }}void DFS(GraphAdjList *G, int i){ EdgeNode *p; visit[i] = TRUE; p = G->VertexNodeList[i].firstedge; cout << G->VertexNodeList[i].data << " "; while (p != NULL) { if (!visit[p->VertexIndex]) { DFS(G, p->VertexIndex); } p = p->next; }}void BFS(GraphAdjList *G){ Queue q; //这里没有用标准库的queue,头文件中自己写的队列 initQueue(&q); EdgeNode *p; for (int i = 0; i < G->numVertex; i++) { visit[i] = FALSE; //初始化全局数组,防止DFS搜索改变数组造成的影响 } for (int i = 0; i < G->numVertex; i++) { if (!visit[i]) { visit[i] = TRUE; cout << G->VertexNodeList[i].data << " "; Enqueue(&q, i); while (!isEmpty(&q)) //直至所有定点遍历完毕退出循环 { i = Dequeue(&q); p = G->VertexNodeList[i].firstedge; while (p != NULL) //直至邻接表后的链表所有定点进入队列退出循环 { if (!visit[p->VertexIndex]) { visit[p->VertexIndex] = TRUE; cout << G->VertexNodeList[p->VertexIndex].data << " "; Enqueue(&q, p->VertexIndex); } p = p->next; } } } }}int main(){ GraphAdjList G; initAdjListGraph(&G); CreateAdjListGraph(&G); cout << "深度优先搜索结果:" << endl; DFS(&G, 0); cout << endl; cout << "广度优先搜索结果:" << endl; BFS(&G); return 0;}
下面是头文件myQueue.h
,包含在主程序中即可,下面为头文件代码:
#ifndef _HEADERNAME_H //防止头文件重复包含#define _HEADERNAME_H#include <stdio.h>#include <iostream>#include <stdlib.h>#endifusing namespace std;#define FALSE 0 //C语言没有bool类型,这里是用户自定义类型#define TRUE 1typedef int elemtype;struct Node{ elemtype data; Node *next;};struct Queue{ Node *front; Node *rear;};void initQueue(Queue *q){ q->front = q->rear = (Node *)malloc(sizeof(Node)); if (!q->front) { return; } q->front->next = NULL;}int isEmpty(Queue *q){ if (q->front == q->rear) { return TRUE; } else { return FALSE; }}void Enqueue(Queue *q, elemtype data){ Node *repareInsert = (Node *)malloc(sizeof(Node)); if (repareInsert == NULL) { exit(0); } q->rear->data = data; q->rear->next = repareInsert; q->rear = repareInsert;}elemtype Dequeue(Queue *q){ if (isEmpty(q)) { exit(0); } Node *FrontTmp = q->front; elemtype data = q->front->data; q->front = q->front->next; free(FrontTmp); return data;}void MakeEmpty(Queue *q){ while (!isEmpty(q)) { Dequeue(q); }}void DisposeQueue(Queue *q){ Node *FrontTmp = q->front; while (FrontTmp != q->rear) { cout << FrontTmp->data << endl; FrontTmp = FrontTmp->next; }}
0 0
- 图的邻接表建立以及DFS和BFS
- 邻接表图的建立与BFS、DFS
- 图的邻接表 表示 DFS 和BFS C++实现
- 图的BFS,DFS(邻接表)
- 第七章 图(邻接矩阵和邻接表建立图并实现DFS、BFS)
- DFS和BFS 邻接矩阵和邻接表
- 图的建立和BFS、DFS
- 图的遍历(BFS、DFS的邻接矩阵和邻接表实现)
- 图的邻接表存储及DFS,BFS遍历
- java版 图的邻接表、邻接矩阵、BFS、DFS 实现
- 邻接表的构建、DFS、BFS搜索
- 邻接表 的 DFS 与 BFS
- 图的dfs递归(非递归)遍历和bfs遍历(邻接表)
- 邻接表实现--图的深度优先遍历DFS和广度优先遍历BFS
- 用邻接表存储有向图实现的dfs和bfs
- DFS BFS遍历图 邻接表实现
- java实现邻接表图、DFS、BFS
- 图的建立 BFS DFS
- 2
- 使用jquery.autocomplete.js插件实现自动补全进行查询
- React-navigation导航系统(5)-Router
- tomcat配置问题
- 字符串中Emoji表情处理(博客园迁移)
- 图的邻接表建立以及DFS和BFS
- JS HTML5事件
- React-navigation导航系统(6)-Views
- Centos 7.0设置/etc/rc.local无效问题解决
- java并发编程实践之对象的共享
- com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token
- <<Linux内核设计与实现>>读书笔记(六)-内核数据结构
- mybatis <forEach>标签的使用
- 关于多线程的几点总结(博客园迁移)