邻接矩阵的创建(有向,无向,网,图)

邻接矩阵的创建(有向,无向,网,图),第1张

邻接矩阵的创建(有向,无向,网,图)
#include
#include
#include //控制cout的输出格式的函数所需要的头文件
#define vertex_type char
#define weight_type int 
#define VERMAX  20
using namespace std;
enum gragh_type{DG,UDG,DN,UDN};//D-directed  UD-undirected  G-gragh  N-net
struct Mgragh
{
	vertex_type vertex[VERMAX];
	weight_type arcs[VERMAX][VERMAX];
	gragh_type kind;
	int ver_num;
	int arc_num;
};
class Gragh
{
private:
	Mgragh gragh;
public:
	Gragh(){}
	~Gragh(){}
	void DG();
	void UDG();
	void DN();
	void UDN();
	void show_gragh();
	int loc_vex(Mgragh G,vertex_type v);
};


int main()
{
	Gragh my_gragh;
	my_gragh.DN();
	my_gragh.show_gragh();

	return 0;
}

void Gragh::DG()//create an directed gragh
{
	cout << "输入顶点数:";
	cin >> gragh.ver_num;
	cout << "输入弧数:";
	cin >> gragh.arc_num;
	cout << "输入顶点名称(空格隔开):";
	for (int i = 0; i < gragh.ver_num; i++)
	{
		cin >> gragh.vertex[i];
	}
	for (int i = 0; i < gragh.ver_num; i++)
	{
		for (int k = 0; k < gragh.ver_num; k++)
		{
			gragh.arcs[i][k] = 0;
		}
	}
	for (int i = 0; i < gragh.arc_num; i++)
	{
		vertex_type x, y;
		cout << "输入弧头,弧尾(空格隔开):";
		cin >> x >> y;
		int m = loc_vex(gragh,x);
		int n = loc_vex(gragh,y);
		if (m == -1 || n == -1)
		{
			cout << "未定位到弧头或弧尾";
			exit(1);
		}
		gragh.arcs[m][n] = 1;
	}
}
void Gragh::UDG()
{
	cout << "输入顶点数:";
	cin >> gragh.ver_num;
	cout << "输入边数:";
	cin >> gragh.arc_num;
	cout << "输入顶点名称(空格隔开):";
	for (int i = 0; i < gragh.ver_num; i++)
	{
		cin >> gragh.vertex[i];
	}
	for (int i = 0; i < gragh.ver_num; i++)
	{
		for (int k = 0; k < gragh.ver_num; k++)
		{
			gragh.arcs[i][k] = 0;
		}
	}
	for (int i = 0; i < gragh.arc_num; i++)
	{
		vertex_type x, y;
		cout << "输入相连顶点(空格隔开):";
		cin >> x >> y;
		int m = loc_vex(gragh, x);
		int n = loc_vex(gragh, y);
		if (m == -1 || n == -1)
		{
			cout << "未定位到顶点";
			exit(1);
		}
		gragh.arcs[m][n] = gragh.arcs[n][m] = 1;//无向图邻接矩阵沿主对角线对称
	}
}

void Gragh::UDN()
{
	cout << "输入顶点数:";
	cin >> gragh.ver_num;
	cout << "输入边数:";
	cin >> gragh.arc_num;
	cout << "输入顶点名称(空格隔开):";
	for (int i = 0; i < gragh.ver_num; i++)
	{
		cin >> gragh.vertex[i];
	}
	for (int i = 0; i < gragh.ver_num; i++)
	{
		for (int k = 0; k < gragh.ver_num; k++)
		{
			gragh.arcs[i][k] = 0;
		}
	}
	for (int i = 0; i < gragh.arc_num; i++)
	{
		vertex_type x, y;
		weight_type w;
		cout << "输入相连两顶点以及权值(空格隔开):";
		cin >> x >> y >> w;
		int m = loc_vex(gragh, x);
		int n = loc_vex(gragh, y);
		if (m == -1 || n == -1)
		{
			cout << "未定位到顶点";
			exit(1);
		}
		gragh.arcs[m][n] = gragh.arcs[n][m] = w;
	}
}

void Gragh::DN()
{
	cout << "输入顶点数:";
	cin >> gragh.ver_num;
	cout << "输入弧数:";
	cin >> gragh.arc_num;
	cout << "输入顶点名称(空格隔开):";
	for (int i = 0; i < gragh.ver_num; i++)
	{
		cin >> gragh.vertex[i];
	}
	for (int i = 0; i < gragh.ver_num; i++)
	{
		for (int k = 0; k < gragh.ver_num; k++)
		{
			gragh.arcs[i][k] = 0;
		}
	}
	for (int i = 0; i < gragh.arc_num; i++)
	{
		vertex_type x, y;
		weight_type w;
		cout << "输入弧头,弧尾以及权值(空格隔开):";
		cin >> x >> y >> w;
		int m = loc_vex(gragh, x);
		int n = loc_vex(gragh, y);
		if (m == -1 || n == -1)
		{
			cout << "未定位到弧头或弧尾";
			exit(1);
		}
		gragh.arcs[m][n] = w;
	}
}
int Gragh::loc_vex(Mgragh G,vertex_type v)
{
	for (int i = 0; i < G.ver_num; i++)
	{
		if (G.vertex[i] == v)
			return i;
	}
	return -1; //找不到顶点,返回-1
}

void Gragh::show_gragh()
{
	cout < 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存