数据结构之图的创建,广搜和深搜

说明:

本文使用邻接矩阵表示一个无向图,在此基础上尝试了三种搜索方法,广度优先,深度优先和非递归的深度优先

 

发现博客还是挺难写的

 

// project1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<string.h>
#include<stdio.h>
#include<queue>
#include<stack>

#define MAX 100

//邻接矩阵表示法
typedef struct{
    char vexs[MAX];//定点数组
    int edges[MAX][MAX];//边的数组
    int vexnum,edgenum;
}MGraph;

//创建图矩阵
void createMGraph(MGraph *g){    
    printf("input vexnum and edgenum:\n");
    scanf("%d,%d",&g->vexnum,&g->edgenum);

    fflush(stdin);
    printf("input every vertex id:\n");
    for(int i=0;i<g->vexnum;i++){//初始化顶点信息
        scanf("%c",&g->vexs[i]);
        fflush(stdin);
    }
    for(int i=0;i<g->vexnum;i++)//初始化矩阵
        for(int j=0;j<g->vexnum;j++)
            g->edges[i][j]=0;
    printf("input edges with format i,j\n");
    char svex,tvex;
    for(int k=0;k<g->edgenum;k++){
        scanf("%c,%c",&svex,&tvex);
        fflush(stdin);
        int i,j;
        for(i=0;g->vexs[i]!=svex;i++);//找到编号
        for(j=0;g->vexs[j]!=tvex;j++);
        g->edges[i][j]=1;
        g->edges[j][i]=1;//无向图,双向
    }
}

int findVex(MGraph* g,char vex){
    for(int i=0;i<g->vexnum;i++)
        if(g->vexs[i]==vex)
            return i;
    return -1;
}

//宽度优先搜索图矩阵
void BFS_MGgraph(MGraph *g,char beg_vex){
    bool visited[MAX]={false};//记录某顶点是否已访问
    int pos;
    using namespace std;
    queue<char> que;

    //注意要在入队时做访问标记,如果出队才做标记则队中会有多个相同的顶点
    if((pos=findVex(g,beg_vex))!=-1)
        visited[pos]=true;
    que.push(beg_vex);//初始点入队
    while(! que.empty()){//当队列不空
        char vex=que.front();que.pop();
        printf("%c ",vex);
        //寻找vex的子节点,然后将其入队
        for(int i=0;i<g->vexnum;i++){
            pos=findVex(g,g->vexs[i]);
            if(pos!=-1 && visited[pos]==false && g->edges[findVex(g,vex)][pos]==1){//未访问过且有边相连
                //入队
                visited[pos]=true;
                que.push(g->vexs[i]);
            }
        }
    }
}

//DFS搜索图矩阵
bool visited2[MAX]={false};
void DFS_MGraph(MGraph* g,char beg_vex){
    printf("%c ",beg_vex);
    int pos=findVex(g,beg_vex);
    if(pos!=-1)    
        visited2[pos]=true;
    //寻找beg_vex的子节点
    for(int i=0;i<g->vexnum;i++){
        pos=findVex(g,g->vexs[i]);
        if(pos!=-1 && visited2[pos]==false && g->edges[findVex(g,beg_vex)][pos]==1){//未访问过且有边相连
            DFS_MGraph(g,g->vexs[i]);
        }
    }
}

//非递归DFS搜索图矩阵
bool visited3[MAX]={false};
void ncur_DFS_MGraph(MGraph* g,char beg_vex){
    using namespace std;
    stack<char> stk;

    stk.push(beg_vex);//初始点入栈
    int pos=findVex(g,beg_vex);
    //注意要在入栈时做访问标记,如果出栈才做标记则队中会有多个相同的顶点
    if(pos!=-1)    
        visited3[pos]=true;
    
    while(! stk.empty()){
        char vex=stk.top();stk.pop();
        printf("%c ",vex);

        //寻找vex的子节点
        for(int i=g->vexnum-1;i>=0;i--){
            pos=findVex(g,g->vexs[i]);
            if(pos!=-1 && visited3[pos]==false && g->edges[findVex(g,vex)][pos]==1){//未访问过且有边相连
                stk.push(g->vexs[i]);
                visited3[i]=true;
            }
        }
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    
    MGraph *g=new MGraph();
    createMGraph(g);
    char beg_vex='f';
    BFS_MGgraph(g,beg_vex);
    printf("\n");
    DFS_MGraph(g,beg_vex);
    printf("\n");
    ncur_DFS_MGraph(g,beg_vex);
    printf("\n");
    delete g;
    return 0;
}



 

 

 

posted @ 2013-11-20 14:00  IT_cnblogs  阅读(419)  评论(0编辑  收藏  举报