数据结构:图(1. 图的存储)
图的概念
图(Graph) 是由顶点和连接顶点的边构成的离散结构
在计算机科学中,图是最灵活的数据结构之一
用于描绘数据之间的关系
图结构的实现
一个图就是一些顶点的集合
这些顶点通过一系列边连接
顶点 V
边 E
图 G(V,E)
图的种类
是否有方向
-
有向图
-
无向图
是否有权重
-
带权图
-
无权图
无向图
只记录顶点之间的连通性
顶点之间的链接是双向的
只要两个顶点链接就行了
A链接了B,B也一定链接了A
有向图
记录顶点的链接方向
顶点之间的链接是单向的
A链接了B,B不一定链接了A
带权图
图的连接的边有权重的大小
下面就是一个无向带权图,权重为一个顶点(市)到林外一个顶点的距离
有向无权图
有向带权图
无向无权图
无向带权图
图的定义
有两种主要的方法:邻接列表和邻接矩阵。
邻接矩阵
在邻接矩阵实现中,定义一个二维数组,由行和列都表示顶点,由两个顶点所决定的矩阵对应元素表示 这里两个顶点是否相连,相连就是1,不相连就是0 相连这个值表示的是相连边或者是权重。
邻接列表
在邻接列表实现中,每一个顶点会存储一个从它这里开始的边的列表
存储结构的时间复杂度
操作 | 邻接列表 | 邻接矩阵 |
---|---|---|
存储空间 | O ( V + E ) O(V+E) O(V+E) | O ( V 2 ) O(V^2) O(V2) |
添加顶点 | O(1) | O ( V 2 ) O(V^2) O(V2) |
添加边 | O(1) | O(1) |
检查相邻性 | O ( V + E ) O(V+E) O(V+E) | O(1) |
C++代码实现:
文件:
文件名 | 文件描述 |
---|---|
main.cpp | 主文件 |
graph.cpp | 邻接矩阵cpp |
graph.h | 邻接矩阵头文件 |
Graph_linked.cpp | 邻接表cpp |
Graph_linked.h | 邻接表头文件 |
main.cpp
#include <iostream>
#include "graph.h"
#include "Graph_linked.h"
using namespace std;
void test() //邻接矩阵测试
{
cout << boolalpha << "邻接矩阵表示:" << endl << endl;
graph g(4); //无向图
g.AddNode("郑州");
g.AddNode("开封");
g.AddNode("上海");
g.AddNode("北京");
g.Link("郑州", "开封", 68);
g.Link("郑州", "北京", 693);
g.Link("开封", "北京", 655);
g.Link("上海", "北京", 1206);
g.Link("上海", "郑州", 692);
g.Link("开封", "上海", 904);
cout << g.OutMatrix() << endl;
/*
郑州--68.8km---开封
郑州--693km---北京
开封--655.4km---北京
上海--1206.8km---北京
上海--692.5km---郑州
开封--904.5km---上海
*/
graph ug(4, graph::Directed);
ug.AddNode("A");
ug.AddNode("B");
ug.AddNode("C");
ug.AddNode("D");
ug.Link("A", "B", 10);
ug.Link("A", "D", 2);
ug.Link("C", "D", 9);
ug.Link("B", "D", 7);
ug.Link("C", "A", 6);
ug.Link("A", "C", 6);
cout << ug.OutMatrix() << endl;
/*
A--10-->B
A--2-->D
C--9-->D
B--7-->D
A--6---C
*/
}
void test2() //邻接表测试
{
cout << "邻接表 表示:" << endl << endl;
Graph_linked g(5); //无向图
g.AddNode("郑州");
g.AddNode("开封");
g.AddNode("上海");
g.AddNode("北京");
g.Link("郑州", "开封", 68);
g.Link("郑州", "北京", 693);
g.Link("开封", "北京", 655);
g.Link("上海", "北京", 1206);
g.Link("上海", "郑州", 692);
g.Link("开封", "上海", 904);
cout << g.OutTable() << endl;
Graph_linked ug(5, Graph_linked::Directed);
ug.AddNode("A");
ug.AddNode("B");
ug.AddNode("C");
ug.AddNode("D");
ug.Link("A", "B", 10);
ug.Link("A", "D", 2);
ug.Link("C", "D", 9);
ug.Link("B", "D", 7);
ug.Link("C", "A", 6);
ug.Link("A", "C", 6);
cout << ug.OutTable() << endl;
}
int main()
{
test();
test2();
}
graph.h 邻接矩阵的函数声明
#pragma once
#include <string>
#include <vector>
class graph
{
public:
enum Type
{
Directed = 1, //有向图
Undirected //无向图
};
private:
int size; //长度
std::vector<std::string> Vex; //存储节点数据
std::vector<std::vector<int>> matrix; //二维表格,连接关系
const Type type; //图的类型
public:
graph(int size = 5,Type type = Undirected); //构造函数 默认大小=5 无向图
bool AddNode(std::string val);//添加节点
bool Link(std::string v1, std::string v2, int weight = 1);//连接两个节点
bool Linked(std::string v1, std::string v2); //判断是否已经连接
bool Unlink(std::string v1, std::string v2); //断开连接
std::string OutMatrix();
private:
int _getPos(std::string v);
bool _check(int p1, int p2);
bool Linked(int p1, int p2); //判断是否已经连接
};
graph.cpp邻接矩阵的函数实现
#include "graph.h"
graph::graph(int size, Type type)
:size(size), type(type)
{
this->matrix.resize(size, std::vector<int>(size)); //length*length 的二维表格
}
bool graph::AddNode(std::string val)
{
if (Vex.size() + 1 > size) //放不下
return false;
Vex.push_back(val); //添加一个节点
return true;
}
bool graph::Link(std::string v1, std::string v2, int weight)
{
if (weight == 0)return false;
int p1 = _getPos(v1);
int p2 = _getPos(v2);
if (_check(p1, p2) == false || Linked(p1, p2) == true) //不合法 或者 已连接
return false;
switch (type)
{
case graph::Directed: //有向图
matrix[p1][p2] = weight;
break;
case graph::Undirected: //无向图
matrix[p1][p2] = weight;
matrix[p2][p1] = weight;
break;
}
return true;
}
bool graph::Linked(std::string v1, std::string v2)
{
int p1 = _getPos(v1);
int p2 = _getPos(v2);
if (_check(p1, p2) == false) return false; //不合法的节点
switch (type)
{
case graph::Undirected:
if (matrix[p1][p2] == 0 && matrix[p2][p1] == 0) //无向图
return false;
break;
case graph::Directed:
if (matrix[p1][p2] == 0) //有向图
return false;
break;
}
return true;
}
bool graph::Unlink(std::string v1, std::string v2)
{
int p1 = _getPos(v1);
int p2 = _getPos(v2);
if (Linked(p1, p2) == false || _check(p1, p2) == false)
return false;
switch (type)
{
case graph::Directed:
matrix[p1][p2] = 0; //有向图
break;
case graph::Undirected:
matrix[p1][p2] = 0; //无向图
matrix[p2][p1] = 0;
break;
}
return true;
}
std::string graph::OutMatrix()
{
std::string str = "\t";
//表头
for (int i = 0; i < Vex.size(); i++)
str += Vex[i] + "\t";
str += "\n";
for (int i = 0; i < Vex.size(); i++)
{
str += Vex[i] + "\t";
for (int j = 0; j < Vex.size(); j++)
{
str += std::to_string(matrix[i][j]) + "\t";
}
str += "\n";
}
return str;
}
int graph::_getPos(std::string v)
{
for (int i = 0; i < Vex.size(); i++)
if (Vex[i] == v)
return i;
return -1;
}
bool graph::_check(int p1, int p2)
{
return !(p1 == p2 || p1 == -1 || p2 == -1); //不合法 return false;
}
bool graph::Linked(int p1, int p2)
{
switch (type)
{
case graph::Undirected:
if (matrix[p1][p2] == 0 && matrix[p2][p1] == 0) //无向图
return false;
break;
case graph::Directed:
if (matrix[p1][p2] == 0) //有向图
return false;
break;
}
return true;
}
Graph_linked.h 邻接表的函数声明
#pragma once
#include <string>
#include <vector>
#include <list>
class Graph_linked
{
public:
enum Type
{
Directed = 1, //有向图
Undirected //无向图
};
struct edge
{
int adj; //邻接的节点序号
int weight;//权
edge(int weight = 1) :weight(weight) //默认参数 = 1
{}
};
private:
int size; //长度
std::vector<std::string> Vex; //存储节点数据
std::vector< std::list<edge> > edges; //list为边类型
const Type type; //图的类型
public:
Graph_linked(int size = 5, Type type = Undirected); //构造函数 默认大小=5 无向图
bool AddNode(std::string val);//添加节点
bool Link(std::string v1, std::string v2, int weight = 1);//连接两个节点
bool Linked(std::string v1, std::string v2); //判断是否已经连接
bool Unlink(std::string v1, std::string v2); //断开连接
std::string OutTable();
private:
int _getPos(std::string v);
bool _check(int p1, int p2);
bool Linked(int p1, int p2); //判断是否已经连接
};
Graph_linked.cpp邻接表的函数实现
#include "Graph_linked.h"
Graph_linked::Graph_linked(int size, Type type)
:size(size), type(type)
{
edges.resize(size, std::list<edge>());
}
bool Graph_linked::AddNode(std::string val)
{
if (Vex.size() + 1 > size) //放不下
return false;
Vex.push_back(val); //添加一个节点
return true;
}
bool Graph_linked::Link(std::string v1, std::string v2, int weight)
{
if (weight == 0)return false;
int p1 = _getPos(v1); //获取在Vex中的序号
int p2 = _getPos(v2); //获取在Vex中的序号
if (_check(p1, p2) == false || Linked(p1, p2) == true) //不合法 或者 已连接
return false;
edge e1,e2;
switch (type)
{
case Graph_linked::Directed: //有向图
e2.adj = p2;
e2.weight = weight;
edges[p1].push_back(e2); //v1添加一个连接v2的边
break;
case Graph_linked::Undirected: //无向图
e2.adj = p2;
e2.weight = weight;
edges[p1].push_back(e2); //v1添加一个连接v2的边
e1.adj = p1;
e1.weight = weight;
edges[p2].push_back(e1);//v2添加一个连接v1的边
break;
}
return true;
}
bool Graph_linked::Linked(std::string v1, std::string v2)
{
int p1 = _getPos(v1); //获取在Vex中的序号
int p2 = _getPos(v2); //获取在Vex中的序号
return Linked(p1, p2);
}
bool Graph_linked::Unlink(std::string v1, std::string v2)
{
int p1 = _getPos(v1); //获取在Vex中的序号
int p2 = _getPos(v2); //获取在Vex中的序号
if (_check(p1, p2) == false) //不合法 返回false
return false;
switch (type)
{
case Graph_linked::Undirected:
for (std::list<edge>::iterator it = edges[p1].begin(); it != edges[p1].end(); it++)
{
if (it->adj == p2)
{
edges[p1].erase(it);
break;
}
}
for (std::list<edge>::iterator it = edges[p2].begin(); it != edges[p2].end(); it++)
{
if (it->adj == p1)
{
edges[p2].erase(it);
return true;
}
}
break;
case Graph_linked::Directed:
for (std::list<edge>::iterator it = edges[p1].begin(); it != edges[p1].end(); it++)
{
if (it->adj == p2)
{
edges[p1].erase(it);
return true;
}
}
break;
}
return false; //在上面的语句中没有return true ,就return false
}
std::string Graph_linked::OutTable()
{
std::string str = "";
for (int i = 0; i < Vex.size(); i++)
{
str += Vex[i] + "-->\t";
for (std::list<edge>::iterator it = edges[i].begin(); it != edges[i].end(); it++)
{
//str += "(" + std::to_string(it->adj) + "," + std::to_string(it->weight) + ")" + "\t";
str += "(" + Vex[it->adj] + "," + std::to_string(it->weight) + ")" + "\t";
}
str += "\n";
}
return str;
}
int Graph_linked::_getPos(std::string v)
{
for (int i = 0; i < Vex.size(); i++)
if (Vex[i] == v)
return i;
return -1;
}
bool Graph_linked::_check(int p1, int p2)
{
return !(p1 == p2 || p1 == -1 || p2 == -1); //不合法 return false;
}
bool Graph_linked::Linked(int p1, int p2)
{
switch (type)
{
case Graph_linked::Undirected: //无向图
for (std::list<edge>::iterator it = edges[p1].begin(); it != edges[p1].end(); it++)
{
if (it->adj == p2) //存在连接v2的边
return true;
}
break;
case Graph_linked::Directed:
for (std::list<edge>::iterator it = edges[p1].begin(); it != edges[p1].end(); it++)
{
if (it->adj == p2) //存在连接v2的边
return true;
}
break;
}
return false;
}
控制台输出:
带权无向图:
带权有向图: