建立图(邻接矩阵、邻近表任选其一)的存储结构、实现图的深度优先遍历和广度优先遍历。

2023-10-27

#include <iostream>
#include <stdlib.h>
using namespace std;
const int DefaultVertices=100;
const int maxWeight=1000;
typedef int E;
typedef char T;
class Graphmtx{
private:
    T *VerticesList;
    E **Edge;
    int numVertices;
    int maxVertices;
    int numEdge;
public:
    Graphmtx(int sz=DefaultVertices);
    ~Graphmtx(){
        delete []VerticesList;
        delete []Edge;
    }
    T getValue(int i){
        if(i>=0&&i<numVertices){
            return VerticesList[i];
        }
        else{
            cout<<"error"<<endl;
            exit(1);
        }
    }
    int getNumVertices(){
        return numVertices;
    }
    int getVertexPos(T vertex){
        for(int i=0;i<numVertices;i++){
            if(VerticesList[i]==vertex)
                return i;
        }
        return -1;
    }
    E getWeight(int v1,int v2){
        return (v1>-1&&v2>-1)?Edge[v1][v2]:0;
    }
    int getFirstNeighbor(int v);
    int getNextNeighbor(int v,int w);
    bool insertVertex(const T vertex);
    bool removeVertex(int v);
    bool insertEdge(int v1,int v2,E cost);
    bool removeEdge(int v1,int v2);
    void show();
};
Graphmtx::Graphmtx(int sz){
    maxVertices=sz;
    numVertices=0;
    numEdge=0;
    int i,j;
    VerticesList=new T[maxVertices];
    Edge=new E*[maxVertices];
    for(i=0;i<maxVertices;i++){
        Edge[i]=new E[maxVertices];
    }
    for(i=0;i<maxVertices;i++){
        for(j=0;j<maxVertices;j++){
            Edge[i][j]=((i==j)?0:maxWeight);
        }
    }
}
int Graphmtx::getFirstNeighbor(int v){
    if(v>-1){
        for(int col=0;col<numVertices;col++){
            if(Edge[v][col]&&Edge[v][col]<maxWeight)
                return col;
        }
    }
    return -1;
}
int Graphmtx::getNextNeighbor(int v,int w){
    if(v>-1&&w>-1){
        for(int col=w+1;col<numVertices;col++)
            if(Edge[v][col]&&Edge[v][col]<maxWeight)
                return col;
    }
    return -1;
}
bool Graphmtx::insertVertex(const T vertex){
    if(numVertices==maxVertices)
        return false;
    VerticesList[numVertices++]=vertex;
    return true;
}
bool Graphmtx::removeVertex(int v){
    if(v<0||v>=numVertices){
        cout<<"error"<<endl;
        return false;
    }
    if(numVertices==1)
        return false;
    int i,j;
    VerticesList[v]=VerticesList[numVertices-1];
    for(i=0;i<numVertices;i++){
        Edge[i][v]=Edge[i][numVertices-1];
    }
    numVertices--;
    for(j=0;i<numVertices;j++)
        Edge[v][i]=Edge[numVertices][j];
    return true;
}
bool Graphmtx::insertEdge(int v1,int v2,E cost){
    if(v1<0||v1>=numVertices||v2<0||v2>=numVertices){
        cout<<"error"<<endl;
        return false;
    }
    Edge[v1][v2]=cost;
    Edge[v2][v1]=cost;
    return true;
}
bool Graphmtx::removeEdge(int v1,int v2){
    if(v1<0||v1>=numVertices||v2<0||v2>=numVertices){
        cout<<"error"<<endl;
        return false;
    }
    Edge[v1][v2]=maxWeight;
    Edge[v2][v1]=maxWeight;
    numEdge--;
    return true;
}
void Graphmtx::show(){
    for(int i=0;i<numVertices;i++){
        for(int j=i;j<numVertices;j++){
            if(Edge[i][j]>0&&Edge[i][j]<maxWeight)
                cout<<VerticesList[i]<<"-"<<VerticesList[j]<<":"<<Edge[i][j]<<endl;
        }
    }
    cout<<endl;
}
class SeqQueue {
protected:
     int rear, fron;
     int*elements;
     int maxSize;
public:
    SeqQueue(int sz = 20){
        fron=0; rear=0; maxSize=sz;
        elements = new int[maxSize];
    }
    int push(int x){
        elements[rear] = x;
        rear = (rear+1) % maxSize;
        return 1;
    }
    int pop(int& x){
        if (IsEmpty()) return 0;
        x = elements[fron];
        fron = (fron+1) % maxSize;
        return 1;
    }
    int IsEmpty() const { return fron == rear;}
};
void DFS (Graphmtx& G, int v, bool *visited) {
    cout << G.getValue(v) << ' ';
    visited[v] = true;
    int w = G.getFirstNeighbor (v);
    while (w != -1) {
      if ( !visited[w] ) DFS(G, w, visited);
     w = G.getNextNeighbor (v, w);
    }
}
void DFS (Graphmtx& G, const T& v) {
    int i, loc, n = G.getNumVertices();
    bool *visited = new bool[n];
    for (i = 0; i < n; i++) visited [i] = false;
 loc = G.getVertexPos(v);
    DFS(G,loc,visited);
    delete [] visited;
}
void BFS (Graphmtx& G, const T& v) {
    int i, w, n = G.getNumVertices();
    bool *visited = new bool[n];
    for (i = 0; i < n; i++) visited[i] = false;
    int loc = G.getVertexPos (v);
    cout<<G.getValue(loc)<<' ';
    visited[loc]=true;
    SeqQueue Q;Q.push (loc);
    while (!Q.IsEmpty() ) {
    Q.pop (loc);
    w = G.getFirstNeighbor (loc);
            while (w != -1) {
                if (visited[w]==false){
                    cout<<G.getValue(w)<<' ';
                    visited[w]=true;
                    Q.push (w);
                }
                w = G.getNextNeighbor (loc, w);
             }
      }
      delete [] visited;
}


int main(){
    Graphmtx g;
    int n1,n2;
    cin>>n1;
    char ch;
    cin>>ch;
    g.insertVertex(ch);
    for(int i=1;i<n1;i++){
        char ch;
        cin>>ch;
        g.insertVertex(ch);
    }
    cin>>n2;
    for(int i=0;i<n2;i++){
        int v1,v2,cost;
        cin>>v1>>v2>>cost;
        g.insertEdge(v1,v2,cost);
    }
    cout<<"DFS:";
    DFS(g,ch);
    cout<<endl;
    cout<<"BFS:";
    BFS(g,ch);cout<<endl;
    cout<<"edges are:"<<endl;
    g.show();
    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

建立图(邻接矩阵、邻近表任选其一)的存储结构、实现图的深度优先遍历和广度优先遍历。 的相关文章

随机推荐

  • C++ 实验8 继承

    编写一个学生和教师数据输入和显示程序 学生数据有编号 姓名 班级和成绩 教师数据有编号 姓名 职称和部门 要求将编号 姓名输入和显示设计成一个类person 并作为学生类student和教师类teacher的基类 类图如下 代码如下 头文件
  • Win10笔记本(机械革命)亮度调节快捷键失效-已解决

    Win10笔记本 机械革命 亮度调节快捷键失效 已解决 1 确定你已经安装了核心显卡驱动 驱动精灵检查一下 2 右击此电脑 管理 系统工具 设备管理器 监视器 单击展开 卸载dpms 卸载Generic Monitor 选中删除相关驱动 3
  • iText包对每页pdf文件加水印

    https ishare iask sina com cn f 31zwqlKmIwM html
  • 用户编写的python程序、无需修改就可以_python的笔记(一)

    Python的基本特点一种动态解释型的编程语言 规范的代码 Python 采用强制缩进的方式使得代码具有极佳的可读性 高级语言特性 封装内存管理等 可移植性 程序如果避免使用依赖于系统的特性 那么无需修改就可以在任何平台上运行 解释性 直接
  • 带你入门TypeScript

    一 为何学习TS 1 TypeScript 在社区的流行度越来越高 它非常适用于一些大型项目 也非常适用于一些基础库 极大地帮助我们提升了开发效率和体验 2 TypeScript 可以编译出纯净 简洁的 JavaScript 代码 并且可以
  • python画玫瑰图_python windrose(风玫瑰图)

    conda install c https conda anaconda org conda forge windrose b 用pip install windrose可以成功 但是安装的路径 python找不到 from windros
  • 多表联查优化

    多表联查优化我总结有以下几点 优化sql语句 索引优化 反范式设计 业务代码优化 使用缓存 优化sql语句 sql性能分析 查看执行频次 查看执行频次 select insert delete update shwo global sess
  • Python应该怎么学,如何系统地自学Python?

    这是一份kaggle上的银行的数据集 研究该数据集可以预测客户是否认购定期存款y 这里包含20个特征 1 分析框架 2 数据读取 数据清洗 导入相关包 import numpy as np import pandas as pd 读取数据
  • 厉害了,用 Java 也能实现图片识别!

    点击上方 Java基基 选择 设为星标 做积极的人 而不是积极废人 源码精品专栏 原创 Java 2020 超神之路 很肝 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 Rock
  • VS2010 编译 sqlite3 生成动态库和链接库

    如果想以dll的方式使用sqlite而新建空的dll工程 添加sqlite源文件 会发现能生成dll 但缺乏lib函数信息映射库 单独使用dll文件是比较麻烦的 而网上多数做法是通过lib exe手动生成lib 这当然不是我想要的 结合几篇
  • 给NVMe设备发送一个SCSI READ_10命令

    0 READ 10命令 READ 6命令只能支持块大小为512B设备的2GB范围的寻址 因此官方推荐将READ 6迁移到READ 10 READ 10具有2TB的寻址能力 对于800G的NVMe设备来说当然是极好的 其实READ 10具有更
  • 【解决】手机安卓已经导入burp证书,但仍提示此证书并非来自被信任的机构

    安卓手机已经安装burpsuite证书 但app应用软件仍无法访问https联网 浏览器可以访问http但https提示此证书并非来自被信任的机构 这问题应该是安卓独有的 博主之前的模拟器访问完全没问题 最近突然无法访问 用控制变量法 我改
  • 2013年11月26日星期二(t3dlib1剩余部分---2)

    接下来 继续进行T3DLIB1剩余部分 倒着进行 先进行bob碰撞检测 int DDraw BOB Collision BOBS BOB PTR bob1 BOB PTR bob2 if bob1 bob2 return 0 int wid
  • 【Unity3D游戏开发学习笔记】(七)上帝之眼—第三人称摄像机的简单实现(跟随视角,自由视角)

    陆陆续续又开始更新自己的博客 看来自我驱动能力还是不够啊 废话不多说了 之前的内容大概说了一下Unity的一些基础知识 接下来我们将要对一些基本功能做一些学习 大家都知道 一个游戏 少不了摄像机的参与 这不是废话么 没摄像机怎么玩 画面都不
  • linux脚本的注释符号是什么,Shell中的变量和符号

    Shell中的变量 变量 shell 变量 可以保存如路径名 文件名或者一个数字 变量名称可以由字母 数字和下划线组成 但是不能以数字开头 如果变量名是 2name 则是错误的 在Bash中 变量的默认类型都是字符串型 如果要进行数值运算
  • log4j2史诗级漏洞攻击重现

    早上来到公司 就听到安全团队的同事说log4j2有个高危漏洞 起初并不是很在意 想着一个日志框架能有啥高危漏洞嘛 但是仔细一看 居然是远程执行命令的漏洞 上次看到这个名字还是struts2 修复方法也很简单 升级log4j依赖版本到2 15
  • vue【element ui】el-date-picker 日期选择器控件 限制可选的开始时间和结束时间

    项目场景 总结一下日期控件实现开始日期 结束日期的选择范围限制 以便更符合实际情况 需求 1 开始时间和结束时间都不能选当前日期之后的时间 当前时间 2022年5月16日 2 先选开始时间的话 结束时间是在开始时间的后一周内选择 也就是不能
  • 实习中了解的互联网数仓

    大数据平台 之前在两家互联网企业都做过数仓相关方面的实习岗位 一家中大厂 一家大厂 在这里简单分享一些数仓在企业中实际的运作 方便一些对数仓有兴趣但尚未在企业中数仓岗位实践过的同学了解 数据开发平台 一般来说 中型或大型企业都会有自己的大数
  • 【Shell编程】条件判断

    系列文章 Shell编程 Shell中的正则表达式 Shell编程 字符截取命令cut printf命令 Shell编程 字符截取命令awk sed命令 目录 系列文章 按照文件类型进行判断 实例 测试上一条命令是否执行成功 编写一个she
  • 建立图(邻接矩阵、邻近表任选其一)的存储结构、实现图的深度优先遍历和广度优先遍历。

    include