图的遍历

本文介绍了一种使用邻接表表示图的数据结构,并通过C++实现深度优先搜索(DFS)和广度优先搜索(BFS)。展示了如何创建图、显示图的邻接表形式以及进行两种基本的图遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include 
<iomanip>
#define MAXVEX 100

using std::cout;
using std::cin;
using std::endl;
using std::setw;

struct ArcNode 
{
    
int adjvex;
    
char info;
    
struct ArcNode *nextarc;
}
;

struct vexnode
{
    
char data;
    
struct ArcNode *firstarc;
}
;

typedef 
struct vexnode AdjList[MAXVEX];

void creatbgraph(AdjList &g, int &n)
{
    
int e, i, s, d;
    
struct ArcNode *p;
    cout 
<< "结点数(n)和边数(e):";
    cin 
>> n >> e;
    
for (i = 0; i < n; i++)
    
{
        cout 
<< "" << i << "个结点信息:";
        cin 
>> g[i].data;
        g[i].firstarc 
= NULL;
    }

    
for (int i = 0; i < e; i++)
    
{
        cout 
<< "" << setw(2<< i + 1 << "条边 起点序号,终点序号:";
        cin 
>> s >> d;
        p 
= new struct ArcNode;
        p
->adjvex = d;
        p
->info = g[d].data;
        p
->nextarc = g[s].firstarc;
        g[s].firstarc 
= p;
    }

}


void dispbgraph(AdjList &g, int n)
{
    
int i;
    
struct ArcNode *p;
    cout 
<< "图的邻接表表示如下:" << endl;
    
for (i = 0; i < n; i++)
    
{
        cout 
<< "  [" << i << "," << g[i].data << "]: ";
        p 
= g[i].firstarc;
        
while (p != NULL)
        
{
            cout 
<< "(" << p->adjvex << "," << p->info << ") ->";
            p 
= p->nextarc;
        }

        cout 
<< "^" << endl;
    }

}


int visited[MAXVEX];
void dfs(AdjList &adj, int v0)
{
    
struct ArcNode *p;
    visited[v0] 
= 2;
    cout 
<< v0 << " ";
    p 
= adj[v0].firstarc;
    
while (p != NULL)
    
{
        
if (visited[p->adjvex] == 0)
            dfs(adj, p
->adjvex);
        p 
= p->nextarc;
    }

}

int queue[MAXVEX], visit[MAXVEX];
void bfs(AdjList adj, int vi)
{
    
int front = 0, rear = 0, v;
    
struct ArcNode *p;
    visit[vi] 
= 1;
    cout 
<< vi << " ";
    rear 
++;
    queue[rear] 
= vi;
    
while (front != rear)
    
{
        front 
= (front + 1% MAXVEX;
        v 
= queue[front];
        p 
= adj[v].firstarc;
        
while (p != NULL)
        
{
            
if (visit[p->adjvex] == 0)
            
{
                visit[p
->adjvex] = 1;
                cout 
<< p->adjvex << " ";
                rear 
= (rear + 1% MAXVEX;
                queue[rear] 
= p->adjvex;
            }

            p 
= p->nextarc;
        }

    }

}


int main()
{
    AdjList bgraph;
    
int n;
    cout 
<< "创建图如下 ";
    creatbgraph(bgraph, n);
    cout 
<< "将图以邻接表的形式输出如下 ";
    dispbgraph(bgraph, n);
    
int m;
    cout 
<< "请输入深度遍历图的起点: ";
    cin 
>> m;
    cout 
<< "深度遍历顺序如下 ";
    dfs(bgraph, m);
    cout 
<< ' ';
    
int p;
    cout 
<< "请输入广度遍历图的起点: ";
    cin 
>> p;
    cout 
<< "广度遍历顺序如下 ";
    bfs (bgraph, p);
    cout 
<< ' ';
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值