写出向存储结构为邻接矩阵的无向图G中插入一条边(x,y)的算法。算法的头函数为:void AddEdgetoGraph(Grap
2024-11-05数据结构(02331)
写出向存储结构为邻接矩阵的无向图G中插入一条边(x,y)的算法。算法的头函数为:
void AddEdgetoGraph(Graph*G,VertexType X,VertexType y>,无向图G的存储结构
为:
#define MaxVertex num
typedef char VertexType;
typedef int EdgeType;
typedef struct graph {
int n,e;//图的实际顶点数和边数
EdgeType edge [MaxVertex][MaxVertex];//邻接矩阵
VertexType vertex[MaxVertex];//顶点表
}Graph;
【正确答案】:【答案】
void AddEdgetoGraph(Graph * G,VertexType x,VertexType y)
{ i=-l;j=-1;
for (k=0;kn;k++)//查找x,y的编号
{if(G->vertex[k]==x)i=k;
if(G->vertex[k]==y)j=k;}
if (i==-1|| j==-1)Error(“结点不存在”);
else {//插入边(x,y)
G->edge[i][j]=1;G->edge[j][i]=l;G->e++;}
解析:无向图中一条边(x,y)的加入会导致邻接矩阵中A[i][j]和A[j][i]的值置1。
void AddEdgetoGraph(Graph*G,VertexType X,VertexType y>,无向图G的存储结构
为:
#define MaxVertex num
typedef char VertexType;
typedef int EdgeType;
typedef struct graph {
int n,e;//图的实际顶点数和边数
EdgeType edge [MaxVertex][MaxVertex];//邻接矩阵
VertexType vertex[MaxVertex];//顶点表
}Graph;
【正确答案】:【答案】
void AddEdgetoGraph(Graph * G,VertexType x,VertexType y)
{ i=-l;j=-1;
for (k=0;k
{if(G->vertex[k]==x)i=k;
if(G->vertex[k]==y)j=k;}
if (i==-1|| j==-1)Error(“结点不存在”);
else {//插入边(x,y)
G->edge[i][j]=1;G->edge[j][i]=l;G->e++;}
解析:无向图中一条边(x,y)的加入会导致邻接矩阵中A[i][j]和A[j][i]的值置1。
