图【数据结构】

文章目录

  • 图的基本概念
  • 邻接矩阵
  • 邻接表
  • 图的遍历
    • BFS
    • DFS

图的基本概念

图是由顶点集合及顶点间的关系组成的一种数据结构
顶点和边:图中结点称为顶点

权值:边附带的数据信息

在这里插入图片描述

在这里插入图片描述
路径 : 在这里插入图片描述

简单路径 和 回路:在这里插入图片描述

子图:设图G = {V, E}和图G1 = {V1,E1},若V1属于V且E1属于E,则称G1是G的子图
在这里插入图片描述

连通图:在无向图中,若从顶点v1到顶点v2有路径,则称顶点v1与顶点v2是连通的。如果图中任意一对顶点都是连通的,则称此图为连通图

生成树:一个连通图的最小连通子图称作该图的生成树。有n个顶点的连通图的生成树有n个顶点和n-1条边

生成树就是用最少的边连通起来

最小生成树:构成生成树这些边加起来权值是最小的。

顶点的度:
**加粗样式**
在这里插入图片描述

邻接矩阵

在这里插入图片描述

#pragma once 
#include<map>
#include<vector>
#include<algorithm>
#include<assert.h>
#include<string>
#include <functional>
#include<iostream>
using namespace std;
//矩阵 
namespace matrix
{//V是顶点 ,W是 weight 权值 template <class V, class W, W MAX_W = INT_MAX, bool Direction = false>  //true是有向图 ,false是无向图class Graph{public://手动添加边Graph(const V* a, size_t n)  //用指针数组 存储顶点{_vertex.reserve(n);//初始化顶点和边for (size_t i = 0; i < n; i++){_vertex.push_back(a[i]);_indexMap[a[i]] = i; //通过顶点找下标}_matrix.resize(n);for (size_t i = 0; i < n; i++){//将邻接矩阵的权值设置为最大值_matrix[i].resize(n, MAX_W);}}size_t  GetVertexIndexMap(const V& v){auto  it = _indexMap.find(v);if (it != _indexMap.end()){return it->second;}else //没有找到 {assert(false);return -1;}}void AddEdge(const V& src, const V& dst, const W& w){int srci = GetVertexIndexMap(src);int dsti = GetVertexIndexMap(dst);_matrix[srci][dsti] = w;//无向图 if (Direction == false){_matrix[srci][dsti] = w;_matrix[dsti][srci] = w;}}void  Print(){//顶点 for (int i = 0; i < _vertex.size(); i++){cout << "[" << i << "]" << "->" << _vertex[i] << endl;}//矩阵 cout << endl;//打印横下标cout << "  ";for (size_t i = 0; i < _vertex.size(); i++){cout << i << " ";}cout << endl;for (int i = 0; i < _matrix.size(); i++){cout << i << " ";//打印竖下标for (int j = 0; j < _matrix[0].size(); j++){if (_matrix[i][j] == MAX_W){cout << "*"<<" ";}else{cout << _matrix[i][j] <<" ";}}cout << endl;}}public:vector<V> _vertex;  //顶点集合 map<V, int>  _indexMap;					//顶点映射下标 vector< vector<W> >	 _matrix;					//邻接矩阵};void TestGraph(){Graph<char, int, INT_MAX, true> g("0123", 4);g.AddEdge('0', '1', 1);g.AddEdge('0', '3', 4);g.AddEdge('1', '3', 2);g.AddEdge('1', '2', 9);g.AddEdge('2', '3', 8);g.AddEdge('2', '1', 5);g.AddEdge('2', '0', 3);g.AddEdge('3', '2', 6);g.Print();}
}

邻接表

邻接表:使用数组表示顶点的集合,使用链表表示边的关系

出边表:存储从各个顶点连接出去的边,出边表中下标为 i的位置存储的是从编号为i的顶点连接出去的边

入边表:存储连接到各个顶点的边,入边表中下标为i的位置存储的是连接到编号为 i的顶点的边
在这里插入图片描述
出边表和入边表的其中每个位置存储的都是一个链表
出边表中下标为i的位置表示从编号为i的顶点连接出去的边
入边表中下标为i 的位置表示连接到编号为i 的顶点的边

在实现邻接表时,一般只需要用一个出边表来存储从各个顶点连接出去的边即可,因为大多数情况下都是需要从一个顶点出发找与其相连的其他顶点,所以一般不需要存储入边表

//邻接表
namespace link_table
{template<class  W>struct Edge{int _dsti;//目标点的下标 W _w;//权值Edge<W> *_next; //用链表表示边的关系Edge(int dsti, const W& w):_dsti(dsti), _w(w), _next(nullptr)  {}};//V是顶点 ,W是 weight 权值 template <class V, class W, bool Direction = false>  //true是有向图 ,false是无向图class Graph{public:typedef Edge<W> Edge;//手动添加边Graph(const V* a, size_t n)  //用指针数组 存储顶点{_vertex.reserve(n);//初始化顶点和边for (size_t i = 0; i < n; i++){_vertex.push_back(a[i]);_indexMap[a[i]] = i; //通过顶点找下标}_table.resize(n, nullptr);}size_t  GetVertexIndexMap(const V& v){auto  it = _indexMap.find(v);if (it != _indexMap.end()){return it->second;}else //没有找到 {assert(false);return -1;}}void AddEdge(const V& src, const V& dst, const W& w){int srci = GetVertexIndexMap(src); int dsti = GetVertexIndexMap(dst);//头插Edge *eg = new Edge(dsti,w);//有向图 //添加从源顶点到目标顶点的边eg->_next = _table[srci];_table[srci] = eg;//无向图 //添加从目标顶点到源顶点的边if (Direction == false){//????Edge* eg = new Edge(srci, w);eg->_next = _table[dsti];_table[dsti] = eg;}}void  Print(){//顶点 for (int i = 0; i < _vertex.size(); i++){cout << "[" << i << "]" << "->" << _vertex[i] << endl;}cout << endl;//遍历顶点for (size_t i = 0; i < _vertex.size(); i++){cout << _vertex[i] << endl;}//遍历邻接表的目标点的下标和权值 for (size_t i = 0; i < _table.size(); i++){cout << _vertex[i] << "[" << i << "]->";Edge * cur = _table[i];while (cur != nullptr){cout << "[" << _vertex[cur->_dsti] << ":" << cur->_dsti << ":" << cur->_w << "]->";cur = cur->_next;}cout << "nullptr" << endl;}}public:vector<V> _vertex;  //顶点集合 map<V, int>  _indexMap;					//顶点映射下标 vector<  Edge*>	 _table;					//邻接表};void TestGraph(){string a[] = { "张三", "李四", "王五", "赵六" };Graph<string, int, true> g1(a, 4);g1.AddEdge("张三", "李四", 100);g1.AddEdge("张三", "王五", 200);g1.AddEdge("王五", "赵六", 30);g1.Print();}
}

图的遍历

BFS

在这里插入图片描述

void BFS(const V& src)  //遍历顶点,通过下标找顶点{size_t srci = GetVertexIndexMap(src);queue<int>  q;vector<bool>  v(_vertex.size(), false);  //防止走重复的路q.push(srci);v[srci] = true;while (!q.empty()){int front = q.front();q.pop();cout << _vertex[front] << endl;// 把front顶点的邻接顶点的下标入队列for (size_t i = 0; i < _vertex.size(); i++){if (_matrix[front][i] != MAX_W && v[i] == false){q.push(i);v[i] = true;}}}}

DFS

在这里插入图片描述

void _DFS(    size_t srci  ,vector<bool> & v){cout << srci << ":" << _vertex[srci] << endl;v[srci] = true;// 找一个srci相邻的没有访问过的点,去往深度遍历for (int i =0 ; i< _vertex.size()  ; i++ ){if (v[i] ==false && _matrix[srci][i] != MAX_W){_DFS(i ,v);}}}void  DFS(const V& src) //遍历顶点,通过下标找顶点{vector<bool>  v(_vertex.size(), false);  //防止走重复的路size_t  srci = GetVertexIndexMap(src);_DFS(srci, v);}

完整测试代码

#pragma once 
#include<map>
#include<vector>
#include<algorithm>
#include<assert.h>
#include<string>
#include <functional>
#include<iostream>
#include<queue>
using namespace std;
//矩阵 
namespace matrix
{//V是顶点 ,W是 weight 权值 //连通的,边的关系就用权值代替,如果两顶点不通,则使用无穷大代替template <class V, class W, W MAX_W = INT_MAX, bool Direction = false>  //true是有向图 ,false是无向图class Graph{public://手动添加边Graph(const V* a, size_t n)  //用指针数组 存储顶点{_vertex.reserve(n);//初始化顶点和边for (size_t i = 0; i < n; i++){_vertex.push_back(a[i]);_indexMap[a[i]] = i; //通过顶点找下标}_matrix.resize(n);for (size_t i = 0; i < n; i++){//将邻接矩阵的权值设置为最大值_matrix[i].resize(n, MAX_W);}}size_t  GetVertexIndexMap(const V& v){auto  it = _indexMap.find(v);if (it != _indexMap.end()){return it->second;}else //没有找到 {assert(false);return -1;}}void AddEdge(const V& src, const V& dst, const W& w){int srci = GetVertexIndexMap(src);int dsti = GetVertexIndexMap(dst);_matrix[srci][dsti] = w;//无向图 if (Direction == false){_matrix[srci][dsti] = w;_matrix[dsti][srci] = w;}}void  Print(){//顶点 for (int i = 0; i < _vertex.size(); i++){cout << "[" << i << "]" << "->" << _vertex[i] << endl;}//矩阵 cout << endl;//打印横下标cout << "  ";for (size_t i = 0; i < _vertex.size(); i++){cout << i << " ";}cout << endl;for (int i = 0; i < _matrix.size(); i++){cout << i << " ";//打印竖下标for (int j = 0; j < _matrix[0].size(); j++){if (_matrix[i][j] == MAX_W){cout << "*"<<" ";}else{cout << _matrix[i][j] <<" ";}}cout << endl;}}void BFS(const V& src)  //遍历顶点,通过下标找顶点{size_t srci = GetVertexIndexMap(src);queue<int>  q;vector<bool>  v(_vertex.size(), false);  //防止走重复的路q.push(srci);v[srci] = true;while (!q.empty()){int front = q.front();q.pop();cout << _vertex[front] << endl;// 把front顶点的邻接顶点的下标入队列for (size_t i = 0; i < _vertex.size(); i++){if (_matrix[front][i] != MAX_W && v[i] == false){q.push(i);v[i] = true;}}}}void _DFS(    size_t srci  ,vector<bool> & v){cout << srci << ":" << _vertex[srci] << endl;v[srci] = true;// 找一个srci相邻的没有访问过的点,去往深度遍历for (int i =0 ; i< _vertex.size()  ; i++ ){if (v[i] ==false && _matrix[srci][i] != MAX_W){_DFS(i ,v);}}}void  DFS(const V& src) //遍历顶点,通过下标找顶点{vector<bool>  v(_vertex.size(), false);  //防止走重复的路size_t  srci = GetVertexIndexMap(src);_DFS(srci, v);}public:vector<V> _vertex;  //顶点集合 map<V, int>  _indexMap;					//顶点映射下标 vector< vector<W> >	 _matrix;					//邻接矩阵};void TestGraph(){Graph<char, int, INT_MAX, true> g("0123", 4);g.AddEdge('0', '1', 1);g.AddEdge('0', '3', 4);g.AddEdge('1', '3', 2);g.AddEdge('1', '2', 9);g.AddEdge('2', '3', 8);g.AddEdge('2', '1', 5);g.AddEdge('2', '0', 3);g.AddEdge('3', '2', 6);g.Print();}void TestBDFS(){string a[] = { "张三", "李四", "王五", "赵六", "周七" };Graph<string, int> g1(a, sizeof(a) / sizeof(string));g1.AddEdge("张三", "李四", 100);g1.AddEdge("张三", "王五", 200);g1.AddEdge("王五", "赵六", 30);g1.AddEdge("王五", "周七", 30);g1.Print();//g1.BFS("张三");g1.DFS("张三");}
}//邻接表
namespace link_table
{template<class  W>struct Edge{int _dsti;//目标点的下标 W _w;//权值Edge<W> *_next; //用链表表示边的关系Edge(int dsti, const W& w):_dsti(dsti), _w(w), _next(nullptr)  {}};//V是顶点 ,W是 weight 权值 template <class V, class W, bool Direction = false>  //true是有向图 ,false是无向图class Graph{public:typedef Edge<W> Edge;//手动添加边Graph(const V* a, size_t n)  //用指针数组 存储顶点{_vertex.reserve(n);//初始化顶点和边for (size_t i = 0; i < n; i++){_vertex.push_back(a[i]);_indexMap[a[i]] = i; //通过顶点找下标}_table.resize(n, nullptr);}size_t  GetVertexIndexMap(const V& v){auto  it = _indexMap.find(v);if (it != _indexMap.end()){return it->second;}else //没有找到 {assert(false);return -1;}}void AddEdge(const V& src, const V& dst, const W& w){int srci = GetVertexIndexMap(src); int dsti = GetVertexIndexMap(dst);//头插Edge *eg = new Edge(dsti,w);//有向图 //添加从源顶点到目标顶点的边eg->_next = _table[srci];_table[srci] = eg;//无向图 //添加从目标顶点到源顶点的边if (Direction == false){//????Edge* eg = new Edge(srci, w);eg->_next = _table[dsti];_table[dsti] = eg;}}void  Print(){//顶点 for (int i = 0; i < _vertex.size(); i++){cout << "[" << i << "]" << "->" << _vertex[i] << endl;}cout << endl;//遍历顶点for (size_t i = 0; i < _vertex.size(); i++){cout << _vertex[i] << endl;}//遍历邻接表的目标点的下标和权值 for (size_t i = 0; i < _table.size(); i++){cout << _vertex[i] << "[" << i << "]->";Edge * cur = _table[i];while (cur != nullptr){cout << "[" << _vertex[cur->_dsti] << ":" << cur->_dsti << ":" << cur->_w << "]->";cur = cur->_next;}cout << "nullptr" << endl;}}public:vector<V> _vertex;  //顶点集合 map<V, int>  _indexMap;					//顶点映射下标 vector<  Edge*>	 _table;					//邻接表};void TestGraph(){string a[] = { "张三", "李四", "王五", "赵六" };Graph<string, int, true> g1(a, 4);g1.AddEdge("张三", "李四", 100);g1.AddEdge("张三", "王五", 200);g1.AddEdge("王五", "赵六", 30);g1.Print();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/535454.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C#语言入门】17. 事件详解(上)

【C#语言入门】17. 事件详解&#xff08;上&#xff09; 一、初步了解事件 定义&#xff1a;单词Event&#xff0c;译为“事件” 通顺的解释就是**“能够发生的什么事情”**&#xff0c;例如&#xff0c;“苹果”不能发生&#xff0c;但是“公司上市”这件事能发生。在C#中事…

strcat函数

函数理解记忆&#xff1a;str表示是<string.g>中的函数&#xff0c;cat表示附加。意思是将一个字符串的内容附加到另一个字符串的末尾。 注意要点&#xff1a;既然要附加&#xff0c;附加的字符串和被附加的字符串都要有\0。否则不知道附加多少&#xff0c;不知附加在哪…

深信服技术认证“SCCA-C”划重点:深信服应用交付AD

为帮助大家更加系统化地学习云计算知识&#xff0c;高效通过云计算工程师认证&#xff0c;深信服特推出“SCCA-C认证备考秘笈”&#xff0c;共十期内容。“考试重点”内容框架&#xff0c;帮助大家快速get重点知识 划重点来啦 *点击图片放大展示 深信服云计算认证&#xff08;S…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的交通信号灯识别系统(深度学习+UI界面+训练数据集+Python代码)

摘要&#xff1a;本研究详细介绍了一种采用深度学习技术的交通信号灯识别系统&#xff0c;该系统集成了最新的YOLOv8算法&#xff0c;并与YOLOv7、YOLOv6、YOLOv5等早期算法进行了性能评估对比。该系统能够在各种媒介——包括图像、视频文件、实时视频流及批量文件中——准确地…

2023 年安徽省职业院校技能大赛(高职组)

#需要资源或有问题的&#xff0c;可私博主&#xff01;&#xff01;&#xff01; #需要资源或有问题的&#xff0c;可私博主&#xff01;&#xff01;&#xff01; #需要资源或有问题的&#xff0c;可私博主&#xff01;&#xff01;&#xff01; 某企业根据自身业务需求&#…

【gpt实践】李某的AI课程值199吗

先说个人的答案&#xff1a;不值。但也不是说毫无价值&#xff0c;只是他的价值没那么高。 文末分享该课程&#xff0c;大家有兴趣可以看看&#xff0c;该课程是否有价值。 “清华博士”推出的199元的AI课程销售额竟然突破了5000万。这一数字让人惊叹&#xff0c;也引发了人们…

YOLOv9改进项目|关于本周更新计划的说明24/3/12

目前售价售价59.9&#xff0c;改进点30个 专栏地址&#xff1a; 专栏介绍&#xff1a;YOLOv9改进系列 | 包含深度学习最新创新&#xff0c;主力高效涨点&#xff01;&#xff01;&#xff01; 日期&#xff1a;24/3/12 本周更新计划说明&#xff1a; 1. 更新华为Gold YOLO中的…

阿里云服务器安全狗免费使用多引擎智能查杀引擎

云服务器具有按量付费、降低综合成本等诸多优势&#xff0c;受到很多企业的欢迎。 因此&#xff0c;目前使用的云服务器越来越多。 阿里云是目前云服务器中最具影响力的品牌&#xff0c;因此选择阿里云服务器的用户数量也是最多的。 那么阿里云服务器需要安装杀毒软件吗&#x…

城市轨道交通站应急照明疏散指示系统设计

彭姝麟 Acrelpsl 1.传统应急照明和疏散指示系统设计方案 车站应急照明由备用照明和疏散指示照明组成。车站正常照明与应急照明设计的照度不低于表 1 规定的值。按地铁防火设计标准要求&#xff0c;备用照明的设置方案&#xff1a;&#xff08;1&#xff09;在变电所、配电室、…

某电信公司组织结构优化咨询项目成功案例纪实

——构建前后端组织结构&#xff0c;提升组织运营效率 随着企业的不断发展&#xff0c;行业的竞争也越来越激烈&#xff0c;企业只能不断调整自身的战略才能更好的适应这样的大环境。在战略调整的过程中&#xff0c;企业往往会面临这样的问题&#xff1a;管理层的经营理念各不…

sqllab第九关通关笔记

知识点&#xff1a; 时间盲注&#xff1a;利用休眠时间进行判断是否注入成功利用bp时注意把timeout时间修改一下 首先判断注入类型 构造id1/0 返回正常信息&#xff0c;应该是字符型注入 构造id1 返回正常信息&#xff0c;欸&#xff0c;这就怪了 构造id1 正常显示内容&am…

Windows系统安装OpenSSH结合VS Code远程ssh连接Ubuntu【内网穿透】

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法|MySQL| ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-mEkKUraSFHLKkzIj {font-family:"trebuchet ms",verdana,arial,sans-serif;f…