图的深度优先遍历
来源:互联网 发布:淘宝企业店铺推广 编辑:程序博客网 时间:2024/04/27 07:05
根据老师将解,以及从网络上学习的一点总结
使用邻接矩阵
#include <iostream> #include <malloc.h> using namespace std;#define VERTEXNUM 5 void createGraph(int (*edge)[VERTEXNUM], int start, int end); void displayGraph(int (*edge)[VERTEXNUM]); void DFT(int (*edge)[VERTEXNUM],int* vertexStatusArr); void DFTcore(int (*edge)[VERTEXNUM],int i,int* vertexStatusArr); int main(void){ //动态创建存放边的二维数组 int (*edge)[VERTEXNUM] = (int (*)[VERTEXNUM])malloc(sizeof(int)*VERTEXNUM*VERTEXNUM); int i,j; for(i=0;i<VERTEXNUM;i++){ for(j=0;j<VERTEXNUM;j++){ edge[i][j] = 0; } } //存放顶点的遍历状态,0:未遍历,1:已遍历 int* vertexStatusArr = (int*)malloc(sizeof(int)*VERTEXNUM); for(i=0;i<VERTEXNUM;i++){ vertexStatusArr[i] = 0; } //创建图 createGraph(edge,0,3); createGraph(edge,0,4); createGraph(edge,3,1); createGraph(edge,3,2); createGraph(edge,4,1); cout<<"邻接矩阵如下"<<endl; displayGraph(edge); //深度优先遍历 cout<<"遍历后"<<endl; DFT(edge,vertexStatusArr); free(edge); return 0; } //创建图 void createGraph(int (*edge)[VERTEXNUM], int start, int end){ edge[start][end] = 1; } //打印存储的图 void displayGraph(int (*edge)[VERTEXNUM]){ int i,j; for(i=0;i<VERTEXNUM;i++){ for(j=0;j<VERTEXNUM;j++){ cout<<edge[i][j]; } cout<<endl; } } //深度优先遍历 void DFT(int (*edge)[VERTEXNUM], int* vertexStatusArr){ int i; for(i=0;i<VERTEXNUM;i++){ DFTcore(edge,i,vertexStatusArr); } cout<<endl; } void DFTcore(int (*edge)[VERTEXNUM],int i,int* vertexStatusArr){ if(vertexStatusArr[i] == 1){ return; } cout<<i; vertexStatusArr[i] = 1; int j; for(j=0;j<VERTEXNUM;j++){ if(edge[i][j] == 1){ DFTcore(edge, j, vertexStatusArr); } } }
使用邻接链表
#include <iostream> #include <malloc.h> using namespace std; #define VERTEXNUM 5 //存放顶点的邻接表元素 typedef struct edge{ int vertex; struct edge* next; }st_edge; void createGraph(st_edge** edge, int start, int end); void displayGraph(st_edge** edge); void delGraph(st_edge** edge); void DFT(st_edge** edge,int* vertexStatusArr); void DFTcore(st_edge** edge,int i,int* vertexStatusArr); int main(void){ //动态创建存放边的指针数组 st_edge** edge = (st_edge**)malloc(sizeof(st_edge*)*VERTEXNUM); int i; for(i=0;i<VERTEXNUM;i++){ edge[i] = NULL; } //存放顶点的遍历状态,0:未遍历,1:已遍历 int* vertexStatusArr = (int*)malloc(sizeof(int)*VERTEXNUM); for(i=0;i<VERTEXNUM;i++){ vertexStatusArr[i] = 0; } //创建图 createGraph(edge,0,3); createGraph(edge,0,4); createGraph(edge,3,1); createGraph(edge,3,2); createGraph(edge,4,1); cout<<"邻接表如下"<<endl; displayGraph(edge); cout<<"遍历后"<<endl; //深度优先遍历 DFT(edge,vertexStatusArr); //释放邻接表占用的内存 delGraph(edge); edge = NULL; free(vertexStatusArr); vertexStatusArr = NULL; return 0; } //创建图 void createGraph(st_edge** edge, int start, int end){ st_edge* newedge = (st_edge*)malloc(sizeof(st_edge)); newedge->vertex = end; newedge->next = NULL; edge = edge + start; while(*edge != NULL){ edge = &((*edge)->next); } *edge = newedge; } //打印存储的图 void displayGraph(st_edge** edge){ int i; st_edge* p; for(i=0;i<VERTEXNUM;i++){ cout<<i<<":"; p = *(edge+i); while(p != NULL){ cout<<p->vertex; p = p->next; } cout<<endl; } } //释放邻接表占用的内存 void delGraph(st_edge** edge){ int i; st_edge* p; st_edge* del; for(i=0;i<VERTEXNUM;i++){ p = *(edge+i); while(p != NULL){ del = p; p = p->next; free(del); } edge[i] = NULL; } free(edge); } //深度优先遍历 void DFT(st_edge** edge,int* vertexStatusArr){ int i; for(i=0;i<VERTEXNUM;i++){ DFTcore(edge,i,vertexStatusArr); } cout<<endl; } void DFTcore(st_edge** edge,int i,int* vertexStatusArr){ if(vertexStatusArr[i] == 1){ return; } printf("%d ",i); vertexStatusArr[i] = 1; st_edge* p = *(edge+i); while(p != NULL){ DFTcore(edge, p->vertex, vertexStatusArr); p = p->next; } }
0 0
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的深度优先遍历
- 图的遍历-(深度优先&广度优先)
- 图的遍历:深度优先、广度优先
- 图的遍历-邻接矩阵-dfs
- HDU 5299 L
- mysql 的时间操作函数
- java一些基本知识(配置、debug调试、快捷键、junit test)
- 数据结构-栈:顺序栈与链栈的C++模板类实现
- 图的深度优先遍历
- 深入浅出SQL:4 聪明的表设计
- BZOJ4502: 串
- Java实现的字符串全排列算法
- mysql的安装,卸载,hive的安装。
- QT_RCC
- Sublime突然运行不了python文件_提示FileNotFoundError(2,'')
- android 短/彩信数据表不能插入
- poj 1061 青蛙的约会