图的遍历 DFS && BFS

假设你已经了解了DFS和BFS
废话不多说 上代码
遍历的图如图所示
该遍历为无向图
若需有向图只需将arc数组修改即可
遍历的图在下方
在这里插入图片描述

邻接矩阵写DFS

在这里插入图片描述
遍历每一行找到arc数组中有1的
将visited转为true

#include <iostream>
using namespace std;
char vertex[] = {'A', 'B', 'C', 'D', 'E'};
struct VertexNode
{
    char data;
};
void AdjMatrix(char arc[][5])
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            arc[i][j] = 0;
        }
    }
    arc[0][1] = arc[0][3] = arc[0][4] = 1;
    arc[1][3] = arc[1][0] = 1;
    arc[2][3] = 1;
    arc[3][0] = arc[3][2] = arc[3][1] = 1;
    arc[4][0] = 1;
}
void DFS_matrix(char DFS[][5], int i, bool *visited)
{
    visited[i] = true;
    for (int j = 0; j < 5; j++)
    {
        if (!visited[j] && DFS[i][j] == 1)
        {
            cout << vertex[j] << ",";
            DFS_matrix(DFS, j, visited);
        }
    }
}
void DFS_AdjMatrix(char DFS[][5])
{
    bool visited[5] = {0};
    for (int i = 0; i < 5; i++)
    {
        if (!visited[i])
        {
            cout << vertex[i] << ",";
            DFS_matrix(DFS, i, visited);
        }
    }
}
int main(int argc, char const *argv[])
{
    char DFS[5][5] = {0};
    AdjMatrix(DFS);
    DFS_AdjMatrix(DFS);
    system("pause");
    return 0;
}

链表写DFS

链表简图
在这里插入图片描述

#include <iostream>
using namespace std;
char vertex[] = {'A', 'B', 'C', 'D', 'E'};
typedef struct VertexNode
{
    char data;
    struct ArcNode * firstArc;
}VertexNode;

typedef struct ArcNode
{
    char data;
    struct ArcNode *nextarc;
}ArcNode;

ArcNode *insertArcNode(char name)
{
    ArcNode *p = new ArcNode;
    p->data = name;
    p->nextarc = NULL;
    return p;
}

VertexNode * AdjList()
{
    ArcNode *p=NULL;
    VertexNode * List_head = new VertexNode[5];
    int c=0;
    List_head[c].data= 'A';
    p=List_head[c].firstArc=insertArcNode('B');
    p=p->nextarc=insertArcNode('D');
    p=p->nextarc=insertArcNode('E');
    c++;
    List_head[c].data='B';
    p=List_head[c].firstArc=insertArcNode('A');
    p=p->nextarc=insertArcNode('D');
    c++;
    List_head[c].data='C';
    p=List_head[c].firstArc=insertArcNode('D');
    c++;
    List_head[c].data='D';
    p=List_head[c].firstArc=insertArcNode('A');
    p=p->nextarc=insertArcNode('B');
    p=p->nextarc=insertArcNode('C');
    c++;
    List_head[c].data='E';
    p=List_head[c].firstArc=insertArcNode('A');
    return List_head;
}
void DFS_list(VertexNode * Graph,int i,bool *visited)
{
    visited[i]=true;
    cout<<vertex[i]<<",";
    ArcNode *p= Graph[i].firstArc;
    while (p!=NULL)
    {
        int temp = p->data -'A';
        if(!visited[temp]) DFS_list(Graph,temp,visited);
        p=p->nextarc;
    }
    
}
void DFS_AdjList(VertexNode *Graph)
{
    bool visited[5]={0};
    for (int i = 0; i < 5; i++)
    {
        if(!visited[i])
        {
            DFS_list(Graph,i,visited);
        }
    }
}
int main(int argc, char const *argv[])
{
    VertexNode *Graph = AdjList();
    DFS_AdjList(Graph);
    system("pause");
    return 0;
}

BFS

#include <iostream>
#include <queue>
using namespace std;
char vertex[] = {'A', 'B', 'C', 'D', 'E'};
typedef struct VertexNode
{
    char data;
    struct ArcNode * firstArc;
}VertexNode;

typedef struct ArcNode
{
    char data;
    struct ArcNode *nextarc;
}ArcNode;
ArcNode *insertArcNode(char name)
{
    ArcNode *p = new ArcNode;
    p->data = name;
    p->nextarc = NULL;
    return p;
}
VertexNode * AdjList()
{
    ArcNode *p=NULL;
    VertexNode * List_head = new VertexNode[5];
    int c=0;
    List_head[c].data= 'A';
    p=List_head[c].firstArc=insertArcNode('B');
    p=p->nextarc=insertArcNode('D');
    p=p->nextarc=insertArcNode('E');
    c++;
    List_head[c].data='B';
    p=List_head[c].firstArc=insertArcNode('A');
    p=p->nextarc=insertArcNode('D');
    c++;
    List_head[c].data='C';
    p=List_head[c].firstArc=insertArcNode('D');
    c++;
    List_head[c].data='D';
    p=List_head[c].firstArc=insertArcNode('A');
    p=p->nextarc=insertArcNode('B');
    p=p->nextarc=insertArcNode('C');
    c++;
    List_head[c].data='E';
    p=List_head[c].firstArc=insertArcNode('A');
    return List_head;
}
void BFS_list(VertexNode *Graph, int i, bool *visited, queue<char> &Q)
{
    cout << Q.front() << ",";
    Q.pop();
    ArcNode *p = Graph[i].firstArc;
    while (p != NULL)
    {
        /*(p->data - 'A')代表顶点的序号*/
        if (*(visited + (p->data - 'A')) == 0) //检测邻接点是否被访问过
        {
            *(visited + (p->data - 'A')) == true;
            Q.push(p->data);
        }
        p = p->nextarc;
    }
}
void BFS_AdjList(VertexNode *Graph)
{
    bool visited[5]={0};
    queue<char> Q;
    for(int i = 0;i<5;i++)
    {
        if(!visited[i])
        {
            visited[i]=true;
            Q.push(vertex[i]);
            BFS_list(Graph,i,visited,Q);
        }
    }
}
int main(int argc, char const *argv[])
{
    VertexNode *Graph = AdjList();
    BFS_AdjList(Graph);
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值