图的存储结构-邻接矩阵

1.图的邻接矩阵存储方式是用两个数组来表示图。一个一维数组存储图中的顶点信息,一个一维数组存储图中的边或者弧的信息

2.邻接矩阵的实现

#include<iostream>


#define MAXVEX 100 /* 最大顶点数,应由用户定义 */
#define INFINITY 65535

using namespace std;
typedef struct
{
	int arc[MAXVEX][MAXVEX];/* 邻接矩阵,可看作边表 */
	int numNodes, numEdges; /* 图中当前的顶点数和边数  */
}MGraph;

void CreateMGraph(MGraph *G){
	cout<<"输入顶点数和边数"<<endl;
	int numN,numE;
	cin>>numN>>numE;
	G->numNodes=numN;
	G->numEdges=numE;
	for(int i=0; i<numE; i++){
		for(int j=0;j<numE;j++){
			if(i==j)
				G->arc[i][j]=0;
			else{
				G->arc[i][j]=INFINITY;
			}
		}
	}
	for(int i=0; i<numE; i++){
		cout<<"输入边i,j,w"<<endl;
		int top,end,weight;
		cin>>top>>end>>weight;
		G->arc[top][end]=weight;
	}
}

void print(MGraph *G){
	for(int i=0; i<G->numEdges; i++){
		for(int j=0;j<G->numEdges;j++){
			if(G->arc[i][j]==65535)
				cout<<"#"<<" ";
			else
			 cout<<G->arc[i][j]<<" ";
		}
		cout<<endl;
		}
	}


int main(){
	MGraph G;    
	CreateMGraph(&G);
	print(&G);	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值