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;
}