您的位置:首页 > 编程语言 > C语言/C++

C++邻接矩阵实现有向图、无向图

2017-05-03 19:49 323 查看
/*********************邻接矩阵实现无向图******************/
class MatrixUDG
{
private:
char mVexs[MAX];//顶点集合
int mVexBum;//顶点数
int mEdgNum;//边数
int mMatrix[MAX][MAX];//邻接矩阵

char readChar();//读取一个字符
int getPosition(char ch);//返回ch在矩阵中中的位置
public:
MatrixUDG();//创建图(自己输入数据)
MatrixUDG(const char *vexs, int vlen,char edges[][2], int elen);//创建图(用已提供的数据)
~MatrixUDG();
void print();//打印矩阵队列图
};
//读取一个字符
char MatrixUDG::readChar()
{
char ch;
cout << "input a ch: ";
cin >> ch;
return ch;
}
//返回ch在矩阵中的位置
int MatrixUDG::getPosition(char ch)
{
for (int i = 0; i < this->mVexBum; i++)
{
if (this->mVexs[i] == ch)
return i;
}
return -1;
}
//创建图(自己输入数据)
MatrixUDG::MatrixUDG()
{
char c1, c2;
int p1, p2;
cout << "构造函数" << endl;
cout << "input vertex number: ";
cin >> mVexBum;
cout << "input edge number: ";
cin >> mEdgNum;
if (this->mVexBum < 1 || this->mEdgNum<1 || (this->mEdgNum>(this->mVexBum*(this->mVexBum - 1))))
{
cout << "input error" << endl;
return;
}
//初始化顶点
for (int i = 0; i < this->mVexBum; i++)
this->mVexs[i] = readChar();
//初始化边:邻接矩阵赋值
for (int i = 0; i < this->mEdgNum; i++)
{
cout << "edge(" << i << ")" << endl;
c1 = readChar();
c2 = readChar();

p1 = getPosition(c1);
p2 = getPosition(c2);
if (p1 == -1 || p2 == -1)
{
cout << "input error: invalid edge!" << endl;
return;
}
this->mMatrix[p1][p2] = 1;
this->mMatrix[p2][p1] = 1;
}
for (int i = 0; i < this->mVexBum; i++)
{
for (int j = 0; j < this->mVexBum; j++)
{
if (this->mMatrix[i][j] != 1)
this->mMatrix[i][j] = 0;
}
}
}
//创建图用已提供的数据
MatrixUDG::MatrixUDG(const char *vexs, int vlen, char edges[][2], int elen)
{
cout << "构造函数" << endl;
int p1, p2;
this->mVexBum = vlen;
this->mEdgNum = elen;
//初始化顶点
for (int i = 0; i < this->mVexBum; i++)
this->mVexs[i] = vexs[i];
//初始化边
for (int i = 0; i < this->mEdgNum; i++)
{
p1 = getPosition(edges[i][0]);
p2 = getPosition(edges[i][1]);

this->mMatrix[p1][p2] = 1;
this->mMatrix[p2][p1] = 1;
}
}
//析构函数
MatrixUDG::~MatrixUDG()
{
cout << "析构函数" << endl;
}
//打印邻接矩阵
void MatrixUDG::print()
{
cout << "打印矩阵队列图" << endl;
int i, j;
for (i = 0; i < this->mVexBum; i++)
{
for (j = 0; j < this->mVexBum; j++)
cout << this->mMatrix[i][j] << " ";
cout << endl;
}
}
//测试程序
int main()
{
MatrixUDG Grap;
Grap.print();
return 0;
}
/*********************邻接矩阵实现有向图******************/
//有向图类
template<class Type>
class DirectedGraph
{
private:
Type vexs[MAX];//顶点表
int arc[MAX][MAX];//邻接矩阵
int numVertexes;//图中当前顶点数
int numEdges;//图中当前边数

int getPosition(Type el);//返回el在邻接矩阵中的位置
Type readType();
public:
DirectedGraph();//创建图
~DirectedGraph();//析构函数
void print();//打印图的邻接矩阵
};
//返会el在连接矩阵中的位置
template<class Type>
int DirectedGraph<Type>::getPosition(Type el)
{
for (int i = 0; i < this->numVertexes; i++)
{
if (this->vexs[i] == el)
return i;
}
return -1;
}
//从外界输入数据
template<class Type>
Type DirectedGraph<Type>::readType()
{
Type data;
cout << "input  a data: ";
cin >> data;
return data;
}
//创建图(自己输入数据)
template<class Type>
DirectedGraph<Type>::DirectedGraph()
{
Type c1, c2;
int p1, p2;
cout << "input numVertexes: ";
cin >> this->numVertexes;
cout << "input numEdges: ";
cin >> this->numEdges;
if (this->numVertexes < 1 || this->numEdges<1 || (this->numEdges> this->numVertexes*(this->numVertexes - 1)))
{
cout << "input error: invalid input!" << endl;
return;
}
//初始化顶点
for (int i = 0; i < this->numVertexes; i++)
this->vexs[i] = readType();
//初始化边
for (int i = 0; i < this->numEdges; i++)
{
cout << "edge(" << i << ")" << endl;
cout << "input arcHead: ";
c1 = readType();
cout << "input arcTail: ";
c2 = readType();
p1 = getPosition(c1);
p2 = getPosition(c2);
if (p1 == -1 || p2 == -1)
{
cout << "input invalid" << endl;
return;
}
this->arc[p1][p2] = 1;
}
for (int i = 0; i < this->numVertexes; i++)
{
for (int j = 0; j < this->numVertexes; j++)
{
if (this->arc[i][j] != 1)
this->arc[i][j] = 0;
}
}
}
//析构函数
template<class Type>
DirectedGraph<Type>::~DirectedGraph()
{
cout << "析构函数" << endl;
}
//打印邻接矩阵
template<class Type>
void DirectedGraph<Type>::print()
{
for (int i = 0; i < this->numVertexes; i++)
{
for (int j = 0; j < this->numVertexes; j++)
cout << this->arc[i][j] << " ";
cout << endl;
}
}
//测试程序
int main()
{
DirectedGraph<char> graph;
graph.print();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言