深度优先搜索、广度优先搜索的实现

来源:互联网 发布:java mvc设计模式闫宏 编辑:程序博客网 时间:2024/05/24 00:31

欢迎留言指出不足,我会及时修改

包括的操作:图的创建 图的矩阵的打印 深度优先搜索的递归算法 广度优先搜索的递归算法

测试使用的图
这里写图片描述

图的记录方式
常用邻接矩阵或邻接表,此例中的矩阵便是:
a 1 0 0 1 1
1 b 1 1 0 0
0 1 c 1 1 0
0 1 1 d 1 0
1 0 1 1 e 0
1 0 0 0 0 f
其中矩阵中的 matrix[i][i]位置表示图中的顶点, matrix[i][j]表示 matrix[i][i]与 matrix[j][j]的边的关系,可以见到矩阵是对称的,有就是说有一半的空间是被浪费的,所以根据情况不同应换用不同的存储方式

以下是代码及注释

#include<stdio.h>#include<stdlib.h>#define MAX 100             //图的顶点数不超过100typedef char EdgeType;     //将边定义为字符形,方便描述typedef char VertexType;    //将顶点定义为字符形,方便描述enum GrapheType{DG,UDG,DN,UDN};//图的类型 有向图 无向图 有向网 无向网bool visited[MAX] = { false };             //记录图中的顶点是否被访问过VertexType V[MAX];  //广度优先搜索需要用到的队列int First = 0;int Last = 0;//图的结构typedef struct MapGraphe{    int Number_of_Vertex;                 //记录顶点的数量    int Number_of_Edge;                  //记录边的数量    VertexType Vertices[MAX];          //记录顶点    EdgeType Edges[MAX][MAX];     //记录边    GrapheType Type;                        //记录图/网 的类别};//图的创建MapGraphe* Create(MapGraphe *Map){    int i, j;    char temp = '1';    Map->Type = UDN;                //类型定义为无网    printf("Input the number of the Vertices and Edges:  ");    //输入顶点和边的数量    scanf("%d%d",&Map->Number_of_Vertex,&Map->Number_of_Edge);    while (Map->Number_of_Edge > MAX || Map->Number_of_Vertex > MAX)        //防止输入数过大    {        printf("Input Error, try again\n");        scanf("%d%d",&Map->Number_of_Vertex,&Map->Number_of_Edge);    }    printf("Input the Vertex Information: \n");     //依次输入顶点信息      for (i = 0; i < Map->Number_of_Vertex; i++)    {        getchar();        printf("The %d Vertex: ", i);        scanf("%c",&Map->Vertices[i]);              }    for (i = 0; i < Map->Number_of_Vertex; i++)         //初始化邻接矩阵    for (j = 0; j < Map->Number_of_Vertex; j++)    {        if (i==j)            Map->Edges[i][i] = Map->Vertices[i];        else        Map->Edges[i][j] = '0';    }    printf("Input the Edge information and two Vertex(ex: edge,i,j)  (enter 0 to stop)\n");    while (true)    {        getchar();        scanf("%c%d%d",&temp,&i ,&j);        if (temp == '0')            return Map;        Map->Edges[i][j] = temp;        Map->Edges[j][i] = temp;    }    return Map;};//递归深度优先搜索void Depth_First_Serach(MapGraphe *Map,char Vertex_name){    int i = 0;    while (Vertex_name != Map->Vertices[i])//先查找是否有存在对应的节点名        i++;    if (i == Map->Number_of_Edge)//不存在,则退出        return ;    if (visited[i]==false)            //判断此节点是否被访问过    {        printf("%2c", Map->Edges[i][i]);        visited[i] = true;        for (int j = 0; j < Map->Number_of_Vertex; j++)//搜索与之有连接点的顶点        {            if (j == i)                continue;            if (Map->Edges[i][j] != '0')                Depth_First_Serach(Map, Map->Edges[j][j]);//开始递归搜索        }    }}//递归广度优先搜索void Breadth_First_Serach(MapGraphe *Map, char Vertex_name){    int i = 0;    while (Vertex_name != Map->Vertices[i])//先查找是否有存在对应的节点名        i++;    if (i == Map->Number_of_Edge)//不存在,则退出        return;    if (visited[i] == false)            //判断此节点是否被访问过    {        printf("%c ", Map->Edges[i][i]);        visited[i] = true;        for (int j = 0; j < Map->Number_of_Vertex; j++)//搜索与之有连接点的顶点        {            if (j == i)                continue;            if (Map->Edges[i][j] != '0')            if (visited[j]==false)                V[Last++] = Map->Edges[j][j];//发现之后进入队列等待搜索        }        Breadth_First_Serach(Map, V[First++]);    }}

测试中输入的数据
1 0 1
1 1 2
1 1 3
1 2 4
1 3 4
1 0 5

以下是测试的运行结果
广度优先搜索的测试结果
这里写图片描述

深度优先搜索的测试结果
这里写图片描述

结合本例中的使用的图
这里写图片描述

由此看来基本实现了两种搜索

0 0
原创粉丝点击