用邻接矩阵表示一个无向图

实现的操作有:

  1. 增加一个结点
  2. 删除一个结点
  3. 增加一条边
  4. 删除一条边
    邻接矩阵的存储表示:
typedef struct{ 
	VerTexType vexs[MVNum];            		//顶点表 
	ArcType arcs[MVNum][MVNum];      		//邻接矩阵 
	int vexnum,arcnum;                		//图的当前点数和边数 
}AMGraph;

具体实现代码:

#include <iostream>
using namespace std;

#define MaxInt 32767                    	//表示极大值,即∞
#define MVNum 100                       	//最大顶点数
#define OK 1	
 						
typedef char VerTexType;              		//假设顶点的数据类型为字符型 
typedef int ArcType;                  		//假设边的权值类型为整型 

//- - - - -图的邻接矩阵存储表示- - - - -
typedef struct{ 
	VerTexType vexs[MVNum];            		//顶点表 
	ArcType arcs[MVNum][MVNum];      		//邻接矩阵 
	int vexnum,arcnum;                		//图的当前点数和边数 
}AMGraph;

int LocateVex(AMGraph G , VerTexType v){
	//确定点v在G中的位置
	for(int i = 0; i < G.vexnum; ++i)
		if(G.vexs[i] == v)
			return i;
   return -1;
}

int CreateUDN(AMGraph &G){ 
    //采用邻接矩阵表示法,创建无向网G 
	int i , j , k;
	VerTexType v1 , v2;
	ArcType w;
	cout <<"请输入总顶点数,总边数,以空格隔开:";
    cin >> G.vexnum >> G.arcnum;							//输入总顶点数,总边数
	cout << endl;
	cout << "输入点的名称,如a" << endl;
    for(i = 0; i < G.vexnum; ++i){   
		cout << "请输入第" << (i+1) << "个点的名称:";
		cin >> G.vexs[i];                        			//依次输入点的信息 
	}
	cout << endl;
	
    for(i = 0; i < G.vexnum; ++i)                			//初始化邻接矩阵,边的权值均置为极大值MaxInt 
		for(j = 0; j < G.vexnum; ++j)   
			G.arcs[i][j] = MaxInt;  
	cout << "输入边依附的顶点及权值,如 a b 5" << endl;
	for(k = 0; k < G.arcnum;++k){							//构造邻接矩阵 
		cout << "请输入第" << (k + 1) << "条边依附的顶点及权值:";
		cin >> v1 >> v2 >> w;								//输入一条边依附的顶点及权值
		i = LocateVex(G, v1);  j = LocateVex(G, v2);		//确定v1和v2在G中的位置,即顶点数组的下标 
		G.arcs[i][j] = w;									//边<v1, v2>的权值置为w 
		G.arcs[j][i] = G.arcs[i][j];						//置<v1, v2>的对称边<v2, v1>的权值为w 
	}
	return OK; 
}

void showmatrix(AMGraph G)
{
	int i,j;	
	for(i = 0 ; i < G.vexnum ; ++i)
	{
		for(j = 0; j < G.vexnum; ++j)
		{
			if(i!=j)	
			{
				if(G.arcs[i][j] != MaxInt)
					cout << G.arcs[i][j] << "\t";
				else
					cout << "∞" << "\t";
			}
			else 
				cout<<0<<"\t";
		} 
    	cout<<endl;
	}
	cout <<endl;	
}

void insertvex(AMGraph &G, VerTexType v)//插入一个新结点
{
	G.vexnum++;
	G.vexs[G.vexnum-1] =v;
}

void deletevex(AMGraph &G, VerTexType v)//删除一个结点
{
	int k=LocateVex(G,v);
	for(int i=0;i<G.vexnum;i++)
	{
		for(int j=0;j<G.vexnum;j++)
		{
			if(i>k && j>k){
				G.arcs[i-1][j-1]=G.arcs[i][j];
			}
			else if(i>k && j!=k){
				G.arcs[i-1][j]=G.arcs[i][j];
			}else if(j>k && i!=k){
				G.arcs[i][j-1]=G.arcs[i][j];
			}
		}
	}
	for(int i=k;i<G.vexnum-1;i++)
	{
		G.vexs[i]=G.vexs[i+1];	
	}
	G.vexnum--;
}

void insertarc(AMGraph &G,VerTexType v1,VerTexType v2,ArcType w)
{//添加一条边
	int k1,k2;
	k1=LocateVex(G,v1);
	k2=LocateVex(G,v2);
	G.arcs[k1][k2]=w;
	G.arcs[k2][k1]=w;
	G.arcnum++;
}

void deletearc(AMGraph &G,VerTexType v1,VerTexType v2)
{//删除一条边
	int k1,k2;
	k1=LocateVex(G,v1);
	k2=LocateVex(G,v2);
	G.arcs[k1][k2]=MaxInt;
	G.arcs[k2][k1]=MaxInt;
	G.arcnum--;
}
 
int main(){
	cout << "************采用邻接矩阵表示法创建无向网**************" << endl << endl;
	AMGraph G;
	VerTexType v,v1,v2;
    ArcType w;
    cout<<"1.创建一个无向网"<<endl;
	cout<<"2.输出邻接矩阵"<<endl;
	cout<<"3.添加结点"<<endl;
	cout<<"4.删除结点"<<endl;
	cout<<"5.添加一条边"<<endl;
	cout<<"6.删除一条边"<<endl;
	cout<<"0.退出"<<endl;
	int choose=-1;
while(choose!=0)
	{
		cout<<"请选择要执行的操作:"<<endl;
		cin>>choose;
		switch(choose)
		{
			case 1:
				 CreateUDN(G);
			     break;
			case 2:
				 showmatrix(G);
				 break;
			case 3:
				 cout<<"请输入要添加的结点:"<<endl;
				 cin>>v;
			     insertvex(G,v);
				 break;
			case 4:
			     cout<<"请选择要删除的结点:"<<endl;
				 cin>>v;
				 deletevex(G,v);
				 break;
			case 5:
				 cout<<"请输入要添加的边的两个顶点和权值,用空格隔开:"<<endl;
				 cin>>v1>>v2>>w;
				 insertarc(G,v1,v2,w);
				 break;
			case 6:
				 cout<<"请输入要删除的边的两个顶点,用空格隔开:"<<endl;
				 cin>>v1>>v2;
			     deletearc(G,v1,v2);
				 break;
		}
	}
 return 0;
}
	
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飒要学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值