问题描述 :
目的:使用C++模板设计并逐步完善图的邻接矩阵抽象数据类型(ADT)。
内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接矩阵ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)
(2)设计并实现一个算法,向已存在的图中添加一个顶点。图的存储结构采用邻接矩阵。添加的顶点的位序在顶点集的末端。将其加入到ADT中。
函数原型:
bool InsertVer(const TypeOfVer &data); //往G中添加一个顶点
注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
输入说明 :
建图的输入数据格式参见建图的算法说明。
第一行:图的类型
第二行:结点数
第三行:结点集
第四行:边数
第五行:边集
第六行:待插入的顶点的元素值
输出说明 :
第一行:图的类型
第二行:插入前的顶点集
空行
第三行:插入前的邻接矩阵
空行
第四行:插入后的顶点集
空行
第五行:插入后的邻接矩阵
输入范例 :
DG
6
A B C D E F
6
0 1
0 2
0 3
1 4
2 4
3 5
G
输出范例 :
DG
A B C D E F
0 1 1 1 0 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0
A B C D E F G
0 1 1 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;
template <class TypeOfVer, class TypeOfEdge>
class adjmatrix_graph {
public:
int Vers;
int Edges;
vector<vector<TypeOfEdge>> edge;
vector<TypeOfVer> ver;
TypeOfEdge noEdge;
string GraphKind;
bool DIRECTION = false, WEIGHT = false;
adjmatrix_graph()
{
Vers = 0;
Edges = 0;
edge.clear();
ver.clear();
noEdge = 0;
}
bool PrintMatrix() {
for (int i = 0; i < Vers; i++)
{
for (int j = 0; j < Vers - 1; j++)
cout << edge[i][j] << " ";
cout << edge[i][Vers - 1] << " ";
cout << endl;
}
return 1;
}
bool GraphisEmpty() { return Vers == 0; }
string GetGraphKind() { return GraphKind; }
TypeOfVer GetVer(int u) {
return ver[u];
}
int GetFirstAdjVex(int u) {
for (int i = 0; i < Vers; ++i)if (edge[u][i] != noEdge)return i;
return -1;
}
int GetNextAdjVex(int u, int v) {
for (int i = v + 1; i < Vers; ++i)
if (edge[u][i] != noEdge)return i;
return -1;
}
//bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值 onst TypeOfVer& data); //往G中添加一个顶点
//int LocateVer(TypeOfVer data); //返回G中指定顶点的位置
void CreateGraph(bool need_emp)
{
cin >> GraphKind;//图的类型
cin >> Vers;//结点数
ver.resize(Vers);
for (int i = 0; i < Vers; i++)//结点集
cin >> ver[i];
if (need_emp)
cin >> noEdge;//无边标记
vector<TypeOfEdge> line;//邻接矩阵初始化
for (int j = 0; j < Vers; j++)
{
for (int i = 0; i < Vers; i++)
line.push_back(noEdge);
edge.push_back(line);
}
cin >> Edges;//边数
vector<int> x_p, y_p, w_p;
for (int i = 0; i < Edges; i++)
{
int c_x, c_y;
cin >> c_x >> c_y;
x_p.push_back(c_x);
y_p.push_back(c_y);
}
if (GraphKind == "DG")//DG(有向图)
DIRECTION = true, WEIGHT = false;
if (GraphKind == "DN")//DN(有向网)
DIRECTION = true, WEIGHT = true;
if (GraphKind == "UDG")//UDG(无向图)
DIRECTION = false, WEIGHT = false;
if (GraphKind == "UDN")//UDN(无向网)
DIRECTION = false, WEIGHT = true;
if (WEIGHT)
for (int i = 0; i < Edges; i++)
{
int c_w;
cin >> c_w;
w_p.push_back(c_w);
}
for (int i = 0; i < Edges; i++)
{
if (DIRECTION == false)//无向图操作
{
if (WEIGHT == true)//带权值的网的操作
edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = w_p[i];
else//无权值操作
edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = 1;
}
else
{
if (WEIGHT == true)//带权值的网的操作
edge[x_p[i]][y_p[i]] = w_p[i];
else//无权值操作
edge[x_p[i]][y_p[i]] = 1;
}
}
}
bool InsertVer(const TypeOfVer& data) {
ver.push_back(data);
for (int i = 0; i < Vers; ++i) {
edge[i].resize(Vers + 1);
}
vector<TypeOfEdge>tmp(Vers + 1);
edge.push_back(tmp);
++Vers;
return 1;
}
int GetVerNum() { return Vers; }
int GetEdgeNum() { return Edges; }
bool Insert_Edge(int u, int v) {
if (edge[u][v] != noEdge)return false;
edge[u][v] = 1;
return true;
}
bool Insert_Edge(int u, int v, TypeOfEdge w) {
if (edge[u][v] != noEdge)return false;
edge[u][v] = w;
return true;
}
//bool DeleteVer(const TypeOfVer& data); //往G中删除一个顶点
bool Delete_Edge(int u, int v) {
if (DIRECTION) {
if (edge[u][v] == noEdge)return false;
edge[u][v] = noEdge;
--Edges;
return true;
}
else {
if (edge[u][v] == noEdge || edge[v][u] == noEdge)return false;
edge[u][v] = edge[v][u] = noEdge;
--Edges;
return true;
}
}
bool Delete_Edge(int u, int v, TypeOfEdge w) {
if (DIRECTION) {
if (edge[u][v] != w)return false;
edge[u][v] = noEdge;
--Edges;
return true;
}
else {
if (edge[u][v] != w || edge[v][u] != w)return false;
edge[u][v] = edge[v][u] = noEdge;
--Edges;
return true;
}
}
void DFS_Traverse(int u) {
//todo
for (int i = 0; i < Vers; ++i)if (edge[u][i] != noEdge)DFS_Traverse(i);
}
void BFS_Traverse(int u) {
unordered_map<int, bool>vis;
queue<int>q;
bool first = true;
q.push(u);
while (q.size()) {
int obj = q.front();
q.pop();
if (!vis[obj]) {
if (!first)cout << "->";
cout << ver[obj];
}
vis[obj] = true;
for (int i = 0; i < Vers;++i)if (edge[obj][i]!= noEdge && !vis[i])q.push(i);
first = false;
}
cout << endl;
}
~adjmatrix_graph() {}
void Print_Format(bool Type) {
if (Type)cout << GetGraphKind() << endl;
for (auto i : ver) {
cout << i << " \n"[i == ver.back()];
}
cout << endl;
PrintMatrix();
}
};
int main()
{
adjmatrix_graph<char, int>g;
g.CreateGraph(0);
char newch;
cin >> newch;
g.Print_Format(1);
g.InsertVer(newch);
cout << endl;
g.Print_Format(0);
return 0;
}