图的深度优先遍历

根据老师将解,以及从网络上学习的一点总结

使用邻接矩阵

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值