//头文件
//Edge.h
#ifndef EDGE_H
#define EDGE_H
#include<iostream>
using namespace std;
template<typename T>
class Edge{
public:
//边的起始与终点
int start, end;
//权重
T weight;
Edge();
Edge(int st, int en, T wei);
bool operator >(const Edge& str);
bool operator <(const Edge& str);
};
template<typename T>
Edge<T>::Edge()
{
}
template<typename T>
Edge<T>::Edge(int st, int en, T wei)
{
this->start = st;
this->end = en;
this->weight = wei;
}
template<typename T>
bool Edge<T>::operator<(const Edge& str)
{
return this->weight < str.weight;
}
template<typename T>
bool Edge<T>::operator>(const Edge& str)
{
return this->weight > str.weight;
}
#endif // !EDGE_H
//Graph.h
#ifndef GRAPH_H
#define GRAPH_H
/*
该图是无向图且是无权图
*/
#include<iostream>
#include"Edge.h"
using namespace std;
template<typename T>
class Graph{
private:
int vertexNum;//节点数目
int edgeNum;//边的数目
Edge<T> edge;
int *Mark;//记录顶点是否被访问过(0表示未被访问过,1表示已被访问过)
int **matrix;//用于实现图的邻接矩阵(全部初始化为0,0表示节点之间没有边,1表示节点之间有边)
int **WeightMartrix;//该矩阵为带权矩阵
int GType;//图的类型(0:无向图,1:有向图)
public:
Graph();
Graph(int ver);
//对Mark和matrix进行初始化
void Init();
~Graph();
//返回顶点vertex的第一条边
bool firstEdge(int vertex);
//返回与oneEdge有相同始点的下一条边
bool nextEdge(Edge<T>& oneEdge);
//设置边(该矩阵为带权矩阵)
void setEdge(int start, int end, T weight);
//设置边(该矩阵为不带权矩阵)
void setEdge(int start, int end);
void print(int n);
void Print();
};
template<typename T>
Graph<T>::Graph()
{
this->vertexNum = 0;
this->edgeNum = 0;
this->Mark = NULL;
this->matrix = NULL;
//默认为无向图
this->GType = 0;
}
template<typename T>
Graph<T>::Graph(int ver)
{
this->vertexNum = ver;
this->Mark = new int[this->vertexNum];
this->matrix = new int*[this->vertexNum];
this->WeightMartrix = new int*[this->vertexNum];
for (int i = 0; i < this->vertexNum; i++)
{
this->matrix[i] = new int[this->vertexNum];
this->WeightMartrix[i] = new int[this->vertexNum];
}
if (this->matrix == NULL || this->Mark == NULL||this->WeightMartrix==NULL)
{
cout << "空间未成功开辟" << endl;
exit(true);
}
this->Init();
}
template<typename T>
void Graph<T>::Init()
{
for (int i = 0; i < this->vertexNum; i++)
{
this->Mark[i] = 0;
for (int j = 0; j < this->vertexNum; j++)
{
this->matrix[i][j] = 0;
this->WeightMartrix[i][j] = 0;
}
}
}
template<typename T>
Graph<T>::~Graph()
{
for (int i = 0; i < this->vertexNum; i++)
{
delete this->matrix[i];
}
delete this->Mark;
this->matrix = NULL;
this->Mark = NULL;
}
template<typename T>
bool Graph<T>::firstEdge(int vertex)
{
//返回以vertex为起点的第一条边
edge.start = vertex;
int i = 0;
for (; i < this->vertexNum; i++)
{
if (this->matrix[vertex-1][i] == 1)
break;
}
if (i >= this->vertexNum)
{
cout << "该顶点没有与边链接" << endl;
return false;
}
else
{
edge.end = i + 1;
return true;
}
}
template<typename T>
bool Graph<T>::nextEdge(Edge<T>& oneEdge)
{
//返回与oneEdge有相同始点的下一条边
int i = oneEdge.end;
for (; i < this->vertexNum; i++)
{
if (this->matrix[oneEdge.start-1][i] != 0)
{
break;
}
}
if (i >= this->vertexNum)
{
cout << "没有找到与该边有相同始点的下一条边" << endl;
return false;
}
edge.start = oneEdge.start;
edge.end = i + 1;
return true;
}
template<typename T>
void Graph<T>::setEdge(int start, int end, T weight)
{
if (start >= this->vertexNum || end >= this->vertexNum)
{
cout << "start/end超出范围" << endl;
}
else if (this->WeightMartrix[start-1][end-1] != 0)
{
cout << "该边已存在" << endl;
cout << "是否需要重新设置(Y/N)" << endl;
char ch;
cin >> ch;
if (ch == 'Y')
{
this->WeightMartrix[start-1][end-1] = weight;
}
}
else
{
this->WeightMartrix[start - 1][end - 1] = weight;
}
}
template<typename T>
void Graph<T>::setEdge(int start, int end)
{
if (start > this->vertexNum || end > this->vertexNum)
{
cout << "start/end已经超出范围" << endl;
}
else if (this->matrix[start-1][end-1] != 0)
{
cout << "该边已存在" << endl;
}
else
{
this->matrix[start-1][end-1] = 1;
}
}
template<typename T>
void Graph<T>::print(int n)
{
if (firstEdge(n))
{
cout << edge.start << " " << edge.end << endl;
}
}
template<typename T>
void Graph<T>::Print()
{
for (int i = 0; i < this->vertexNum; i++)
{
for (int j = 0; j < this->vertexNum; j++)
{
if (this->WeightMartrix[i][j] != 0)
{
cout << "(" << i + 1 << "," << j + 1 << ") :"<< this->WeightMartrix[i][j];
}
}
cout << endl;
}
cout << endl;
}
#endif // !GRAPH_H
//主函数
#include"Graph.h"
int main(int argc, char argv[])
{
Graph<int>graph(5);
graph.setEdge(4, 5);
graph.setEdge(3, 2);
graph.setEdge(1, 1, 5);
graph.print(1);
graph.print(3);
graph.print(4);
graph.Print();
return 0;
}
图的定义,存储结构是邻接矩阵(无向图,包含带权图)
最新推荐文章于 2022-05-23 14:08:28 发布