数据结构--图--图的存储结构/DFS/BFS
来源:互联网 发布:安卓微信数据迁移 编辑:程序博客网 时间:2024/05/16 16:24
图的存储结构
图的存储结构和DFS/BFS代码放在一起写了~
邻接矩阵
图的存储结构/图的建立
#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0#define TURE 1#define FALSE 0#define OVERFLOW -2#define MAX_VERTEX_SIZE 20#define INT_MAX 65535#define INFINITY INT_MAXtypedef int Status;typedef int VertexType;typedef int VRType;typedef enum{DG,DN,UDG,UDN} GraphKind;typedef struct VNode{ VertexType data;}VNode,AdjList[MAX_VERTEX_SIZE];typedef struct ArcCell{ VRType adj;}ArcCell,AdjMatrix[MAX_VERTEX_SIZE][MAX_VERTEX_SIZE];typedef struct{ int vexnum,arcnum; AdjMatrix arcs; AdjList vertices; GraphKind kind;}MGraph;Status CreateGraph(MGraph *G){ int isign,jsign,i,j; VNode v1,v2; int value; (*G).kind = UDN; printf("输入顶点数 弧的数\n"); scanf("%d%d",&(*G).vexnum, &(*G).arcnum); printf("输入顶点信息\n"); for(i = 0; i < (*G).vexnum; ++i){ scanf("%d",&(*G).vertices[i].data); } for(i = 0; i < (*G).vexnum; ++i){ for(j = 0; j < (*G).vexnum; ++j){ if(i == j) (*G).arcs[i][j].adj = 0; else (*G).arcs[i][j].adj = INFINITY; } } printf("输入弧的信息\n"); for(i = 0; i < (*G).arcnum; ++i){ isign = -1; jsign = -1; scanf("%d%d%d",&v1.data,&v2.data,&value); for(j = 0; j < (*G).vexnum; ++j){ if((*G).vertices[j].data == v1.data) isign = j; if((*G).vertices[j].data == v2.data) jsign = j; } (*G).arcs[isign][jsign].adj = (*G).arcs[jsign][isign].adj = value; } return OK;}
邻接表
图的存储结构/图的建立/BFS/DFS
#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define OVERFLOW -2#define STACK_INIT_SIZE 10#define STACKINCREMENT 2#define MAX_VERTEX_SIZE 20typedef int Status;typedef int VRType;typedef int VertexType;typedef enum{DG,DN,UDG,UDN} GraphKind;typedef VertexType QElemType;int visited[MAX_VERTEX_SIZE];typedef struct QNode{ QElemType data; struct QNode *next;}QNode,*Queueptr;typedef struct{ Queueptr rear,front;}LinkQueue;typedef struct ArcNode{ int adjvex; struct ArcNode *next; VRType weight;}ArcNode;typedef struct VNode{ VertexType data; ArcNode *firstarc;}VNode,AdjList[MAX_VERTEX_SIZE];typedef struct{ int vexnum,arcnum; AdjList vertices; GraphKind kind;}ALGraph;Status InitQueue(LinkQueue *Q);Status DestroyQueue(LinkQueue *Q);Status EnQueue(LinkQueue *Q, QElemType e);Status DeQueue(LinkQueue *Q, QElemType *e);Status QueueEmpty(LinkQueue Q);Status CreateGraph(ALGraph *G){ ArcNode *p,*q; VNode v1,v2; int i,j,sign1,sign2; (*G).kind = DG; printf("有向图\n"); printf("请输入顶点数,弧的数\n"); scanf("%d%d",&(*G).vexnum,&(*G).arcnum); printf("请初始化顶点\n"); for(i = 0; i < (*G).vexnum; ++i){ scanf("%d",&(*G).vertices[i].data); (*G).vertices[i].firstarc = NULL; } printf("请初始化弧\n"); printf("输入格式:顶点1 顶点2 (表示顶点1邻接到顶点2)\n"); for(i = 0; i < (*G).arcnum; ++i){ sign1 = -1; sign2 = -1; scanf("%d%d",&v1.data,&v2.data); for(j = 0; j < (*G).vexnum; ++j){ if(v1.data == (*G).vertices[j].data) sign1 = j; if(v2.data == (*G).vertices[j].data) sign2 = j; } p = (ArcNode*)malloc(sizeof(ArcNode)); if(!p) exit(OVERFLOW); p->next = NULL; p->adjvex = sign2; q = (*G).vertices[sign1].firstarc; if(!q) (*G).vertices[sign1].firstarc = p; else{ while(q->next != NULL) q = q->next; q->next = p; } } return OK;}//测试函数-测试建图是否正确void test(ALGraph G){ int i; ArcNode *p; for(i = 0; i < G.vexnum; ++i){ printf("i = %d\n",i); for(p = G.vertices[i].firstarc; p; p = p->next) printf("p->adjvex = %d\n",p->adjvex); }}//图的销毁函数 //存在问题/*Status DestroyGraph(ALGraph *G){ int i; ArcNode *p,*q; for(i = 0; i < (*G).vexnum; ++i){ p = (*G).vertices[i].firstarc; while(p){ q = p->next; free(p); p = q; } } return OK;}Status DestroyGraph(ALGraph *G){ int i; for(i = 0; i < (*G).vexnum; ++i){ p = (*G).vertices[i].firstarc; while(p){ q = p->next; free(p); p = q; } (*G).vertices[i].firstarc = NULL; } free(G);}*/Status Visit(VertexType e){ printf("%d",e); return OK;}Status DFS(ALGraph G,int i){ ArcNode *p; visited[i] = 1; Visit(G.vertices[i].data); for(p = G.vertices[i].firstarc; p; p = p->next){ if(!visited[p->adjvex]) DFS(G,p->adjvex); } return OK;}Status DFSTraverse(ALGraph G){ int i; for(i = 0; i < G.vexnum; ++i) visited[i] = 0; for(i = 0; i < G.vexnum; ++i){ if(!visited[i]) DFS(G,i); } return OK;}Status BFS(ALGraph G, int i){ LinkQueue Q; ArcNode *p; int x; InitQueue(&Q); visited[i] = 1; Visit(G.vertices[i].data); EnQueue(&Q,i); while(!QueueEmpty(Q)){ DeQueue(&Q,&x); p = G.vertices[x].firstarc; while(p){ if(!visited[p->adjvex]){ EnQueue(&Q,p->adjvex); visited[p->adjvex] = 1; Visit(G.vertices[p->adjvex].data); } p = p->next; } } DestroyQueue(&Q); return OK;}Status BFSTraverse(ALGraph G){ int i; for(i = 0; i < G.vexnum; ++i) visited[i] = 0; for(i = 0; i < G.vexnum; ++i){ if(!visited[i]) BFS(G,i); } return OK;}int main(){ ALGraph G; CreateGraph(&G); printf("\nDFSTraverse\n"); DFSTraverse(G); printf("\nBFSTraverse\n"); BFSTraverse(G); LinkQueue Q; InitQueue(&Q);// DestroyGraph(&G); return 0;}Status InitQueue(LinkQueue *Q){ (*Q).rear = (*Q).front = (Queueptr)malloc(sizeof(QNode)); if(!(*Q).front) exit(OVERFLOW); (*Q).front->next = NULL; return OK;}Status DestroyQueue(LinkQueue *Q){ while((*Q).front){ (*Q).rear = (*Q).front->next; free((*Q).front); (*Q).front = (*Q).rear; } return OK;}Status EnQueue(LinkQueue *Q, QElemType e){ Queueptr p = (Queueptr)malloc(sizeof(QNode)); if(!p) exit(OVERFLOW); p->data = e; p->next = NULL; (*Q).rear->next = p; (*Q).rear = p;}Status DeQueue(LinkQueue *Q, QElemType *e){ Queueptr p = (*Q).front->next; if((*Q).front == (*Q).rear){ printf("队空\n"); return ERROR; } *e = p->data; if((*Q).rear == p){ (*Q).rear = (*Q).front; } (*Q).front->next = p->next; free(p); return OK;}Status QueueEmpty(LinkQueue Q){ if(Q.front == Q.rear) return TRUE; else return FALSE;}
阅读全文
0 0
- 数据结构--图--图的存储结构/DFS/BFS
- 数据结构 - 图的存储结构表示及其遍历 (DFS && BFS)
- 《大话数据结构》图的BFS和DFS
- 图的遍历--使用邻接表作为存储结构的遍历(DFS、BFS)C语言
- 图的遍历(DFS、BFS)使用邻接矩阵(数组)作为存储结构--C语言
- 数据结构---->图的存储结构
- 数据结构 - 图的存储结构
- 数据结构---图的存储结构
- 【数据结构】图的存储结构
- 图的dfs、bfs
- 【数据结构】图的遍历之DFS和BFS
- 【数据结构】Java实现图的DFS和BFS
- 数据结构实验3(图的DFS和BFS实现)
- 数据结构(17)--图的遍历DFS和BFS
- 数据结构—连通图的遍历—DFS和BFS
- 数据结构之图的遍历(BFS+DFS)
- 树的存储结构和图的存储结构以及图的深度优先DFS搜索和BFS广度优先搜索
- 邻接矩阵实现图的存储,DFS,BFS遍历
- 2-2grep\计划任务
- 2017-8-10
- java中同步和异步有什么异同
- c++中的new和delete
- POJ-3051
- 数据结构--图--图的存储结构/DFS/BFS
- 没搞懂今天到底学的是啥.....
- 小数到分数的转换
- java 无法下载问题
- (学习笔记)spring入门之Bean的依赖注入
- SQL解惑-谜题32计算税收
- [python爬虫] Selenium高级篇之窗口移动、弹出对话框自登录
- Machine learning 5---贝叶斯分类
- 洛谷P1064 金明的预算方案