//图的定义
template<class T>
struct MatrixGraph {
int edge_num; //边数
int vertex_num; //顶点数
int EdgeMatrix[101][101]; //邻接矩阵
T vertex_[101]; //顶点值(要加下划线,不然重名)
};
生成图的代码:
template<class T>
MatrixGraph<T> CreatUndigraph(int vertex[]) {//顶点值,vertex[0]储存了数组长度
MatrixGraph<T> undigraph;
undigraph.edge_num = 0;//初始化边数
undigraph.vertex_num = vertex[0];//初始化顶点数
int i, j;
//初始化顶点值
for (int i = 0; i <= undigraph.vertex_num; i++) {
undigraph.vertex_[i] = vertex[i];
}
//初始化矩阵
for (int i = 1; i <= undigraph.vertex_num; i++) {
for (int j = i; j <= undigraph.vertex_num; j++) {
undigraph.EdgeMatrix[i][j] = 0;
undigraph.EdgeMatrix[j][i] = 0;
}
}
//修改边矩阵
while (1) {
cout << "输入存在边的顶点1(输入0结束):";
cin >> i;
cout << "输入存在边的顶点2(输入0结束):";
cin >> j;
if (i == 0 || j == 0) break;//修改边结束
undigraph.EdgeMatrix[i][j] = 1;
undigraph.EdgeMatrix[j][i] = 1;//对称
undigraph.edge_num++;//存在边,边数+1
}
cout << "创建图成功!" << endl;
return undigraph;
}
//图的定义
template<class T>
struct MatrixGraph {
int edge_num; //边数
int vertex_num; //顶点数
int EdgeMatrix[101][101]; //邻接矩阵
T vertex_[101]; //顶点值(要加下划线,不然重名)
};