1. 图的广度优先遍历

news/2024/6/29 12:16:37 标签: 宽度优先, 算法

题目

本实验实现邻接表表示下无向图的广度优先遍历。

程序的输入是图的顶点序列和边序列(顶点序列以*为结束标志,边序列以-1,-1为结束标志)。程序的输出为图的邻接表和广度优先遍历序列。例如:

程序输入为:
a
b
c
d
e
f
*
0,1
0,4
1,4
1,5
2,3
2,5
3,5
-1,-1

程序的输出为:
the ALGraph is
a 4 1
b 5 4 0
c 5 3
d 5 2
e 1 0
f 3 2 1
the Breadth-First-Seacrh list:aebfdc

测试输入期待的输出时间限制内存限制额外进程
测试用例 1以文本方式显示
  1. a↵
  2. b↵
  3. c↵
  4. d↵
  5. e↵
  6. f↵
  7. *↵
  8. 0,1↵
  9. 0,4↵
  10. 1,4↵
  11. 1,5↵
  12. 2,3↵
  13. 2,5↵
  14. 3,5↵
  15. -1,-1↵
以文本方式显示
  1. the ALGraph is↵
  2. a 4 1↵
  3. b 5 4 0↵
  4. c 5 3↵
  5. d 5 2↵
  6. e 1 0↵
  7. f 3 2 1↵
  8. the Breadth-First-Seacrh list:aebfdc↵
1秒64M

C++代码 

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct Node {
    char data;
    int dataNum;
    bool visited;
    vector<int> edges;
};
vector<Node> nodeList;

void bfs(int startIndex) {
    queue<int> nodeQueue;
    nodeQueue.push(startIndex);
    nodeList[startIndex].visited = true;

    while (!nodeQueue.empty()) {
        int currentIndex = nodeQueue.front();
        nodeQueue.pop();
        cout << nodeList[currentIndex].data;

        for (int i = nodeList[currentIndex].dataNum-1; i >=0; i--) {
            int nextIndex = nodeList[currentIndex].edges[i];
            if (!nodeList[nextIndex].visited) {
                nodeQueue.push(nextIndex);
                nodeList[nextIndex].visited = true;
            }
        }
    }
}

int main() {
    char ch;
    int numNodes = 0, indexA, indexB;

    // 输入节点
    while (cin >> ch && ch != '*') {
        cin.ignore(); // 跳过换行符
        Node node{ ch, 0, false, {} };
        nodeList.push_back(node);
        numNodes++;
    }

    // 输入边
    while (cin >> indexA >>ch>> indexB && !(indexA == -1 && indexB == -1)) {
        nodeList[indexA].edges.push_back(indexB);
        nodeList[indexA].dataNum++;
        nodeList[indexB].edges.push_back(indexA);
        nodeList[indexB].dataNum++;
    }

    // 输出邻接表
    cout << "the ALGraph is\n";
    for (int i = 0; i < numNodes; ++i) {
        cout << nodeList[i].data;
        for (int j = nodeList[i].dataNum-1; j >=0 ; j--) {
            cout << " " << nodeList[i].edges[j];
        }
        cout << endl;
    }

    // 执行BFS并输出结果
    cout << "the Breadth-First-Seacrh list:";

    for (int i = 0; i < nodeList.size(); i++)
    {
        if (!nodeList[i].visited )
        {
            bfs(i);
        }
    }
   
    cout << endl;

}


http://www.niftyadmin.cn/n/5213735.html

相关文章

kafka 集群 KRaft 模式搭建

Apache Kafka是一个开源分布式事件流平台&#xff0c;被数千家公司用于高性能数据管道、流分析、数据集成和关键任务应用程序 Kafka 官网&#xff1a;https://kafka.apache.org/ Kafka 在2.8版本之后&#xff0c;移除了对Zookeeper的依赖&#xff0c;将依赖于ZooKeeper的控制器…

个体卫生室电子处方操作流程,私人诊所用什么电子处方系统软件,佳易王诊所电子处方软件配方模板如何设置

个体卫生室电子处方操作流程&#xff0c;私人诊所用什么电子处方系统软件&#xff0c;佳易王诊所电子处方软件配方模板如何设置 1、一般电子处方系统的操作流程为&#xff1a;由医师使用软件开电子处方&#xff0c;打印后核对信息医师签字&#xff0c;然后由药剂师审核单据&am…

Linux网络——网络层

目录 一.IP协议&#xff08;IPv4&#xff09; 二.子网划分 三.特殊的IP地址 四.IP地址的数量限制 五.私有IP地址和公网IP地址 六.路由 七.分片 一.IP协议&#xff08;IPv4&#xff09; IP协议&#xff1a;提供一种能力使得数据从一个主机发送到另一个主机的能力。 TCP协…

【TC3xx芯片】TC3xx芯片的Endinit功能详解

目录 前言 正文 1.功能概述 2. WDTxCON0 的密码访问&#xff08;Password Access to WDTxCON0&#xff09; 2.1 Static Password 2.2 Automatic Password Sequencing 2.3 Time-Independent Pasword 2.4 Time Check Password 3. WDTxCON0的检查访问&#xff08;Check A…

【PyQt】QPixmap与numpy.array互转

这里给出QPixmap→numpy.ndarray的两条转换(一个是使用PIL.Image而另一个不用)&#xff0c; 以及numpy.ndarray→QPixmap两条转换(同样也是用不用PIL.Image的区别)。 代码运行结果&#xff1a; from PyQt5.QtCore import QPoint,QRect,Qt from PyQt5.QtWidgets import QLabel …

做直播服务器要什么样的配置呢?

现在直播行业越来越火爆&#xff0c;大大小小的平台或者企业都选择通过直播卖货的方式出售产品&#xff0c;直播的内容还有观看直播的人数等等都影响了服务器的配置需求&#xff0c;今天小编就给大家讲一讲吧&#xff01; 1、内存&#xff1a;直播服务器需要足够的内存才能支持…

python小数据分析小结及算法实践集锦

在缺乏大量历史数据的新兴技术和产业中&#xff0c;商业分析可能会面临一些挑战。然而&#xff0c;有一些技术和方法可以帮助分析者在数据不充分的情况下进行科学化商业分析&#xff0c;并为决策提供支持。 1. 当面对缺乏大量历史数据的新兴技术和产业时所采常用的技术和方法 …

SpringBoot应用手册

工作内容,不对外开放 文章目录 一、ApplicationContextInitializer实现向容器中注入属性实现方式一:使用spring.factories实现方式二:主启动类上添加实现方式三:配置文件中配置注意点:二、自定义监听器第一种方式:使用spring.factories第二种方式:主启动类上添加第三种方…