设计一个算法,删除无向图的邻接矩阵中给定顶点。

图的结构定义

# define INFINITY 1000
# define MAX_VERTEX_NUM 20 //最大值
# define OK 1
#define MAX_VERTEX_NUM 20// 定义常量MAX_VERTEX_NUM为20
#include<stdio.h>
#include<stdlib.h> 
#include <iostream>
#define MaxQueueSizea 100;
typedef enum{DG,DN,UDG,UDN} GraphKind;// {有向图,有向网,无向图,无向网}
typedef int EType;//定义 EType 类型为 int
typedef int InfoType;
typedef int VertexType;//定义 VertexType 类型为 int
typedef struct ArcCell
{ 
	EType adj; //EType 是顶点关系类型。对无权图,用 1 或 0 表示相邻否;
//带权图,则为权值类型
 	InfoType *info; //该弧相关信息的指针
	}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{ 	VertexType vexs[MAX_VERTEX_NUM];//顶点向量
 	AdjMatrix arcs; //邻接矩阵
 	int vexnum,arcnum; //图的当前顶点数和弧数
 	GraphKind kind; //图的种类标志
}MGraph;
	typedef int VertexType;
	typedef int InfoType;
	typedef struct ArcNode //定义图的存储结构
		{ 	int adjvex; //该弧所指向的顶点的位置
 			struct ArcNode *nextarc; //指向下一条弧的指针
 			InfoType *info; //该弧相关信息的指针
		}ArcNode;
	typedef struct VNode
		{ 	VertexType data; //顶点信息
 			ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
			}VNode,AdjList[MAX_VERTEX_NUM];
	typedef struct
	{ 		AdjList vertices;
 			int vexnum,arcnum; //图的当前顶点数和弧数
 			int kind; //图的种类标志
	}ALGraph;

无向图的创建

int CreatUDN(MGraph &G)
{ int IncInfo,i,j,k,v1,v2,w;
 printf("Please input the number of G.vexnum (eg,G.vexnum=4):");
 scanf("%d",&G.vexnum);//输入顶点数 G.vexnum
 printf("Please input the number of G.arcnum (eg,G.arcnum=4): ");
 scanf("%d",&G.arcnum);//输入边数 G.arcnum
 printf("Please input IncInfo (0 for none) : ");
 scanf("%d",&IncInfo);//输入弧相关信息
 for(i=0;i<G.vexnum;++i)
 for(j=0;j<G.vexnum;++j)
 	{ G.arcs[i][j].adj=INFINITY; //初始化邻接矩阵
 	G.arcs[i][j].info=NULL;
 	}
 	printf("Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...\n");
 for(k=0;k<G.arcnum;k++ ) //构造邻接矩阵
{ 	printf("Please input the %dth arc's v1 and v2:",k+1);//输入一条边
 	scanf("%d,%d",&v1,&v2);
 	printf("Please input the arc's weight :");
 	//canf("%d",&w); //输入边的权值
 	i=v1; j=v2;
 	i--; j--;
 	G.arcs[i][j].adj=G.arcs[j][i].adj=1;
 	if(IncInfo)
 	{printf("Please input the %dth arc's Info :",k+1);
	//scanf("%s",G.arcs[i][j].info);
 	}
 } 
 return (OK);
} 

在图 G中删除顶点I/

void Delvi(MGraph *G,int i)/*在图 G中删除顶点I*/
{int num,j,k;
if(i<1||i>G->vexnum)
        {printf("error");  exit(0);}
else
{num=0;
 for(j=1;j<=G->vexnum;j++)
  {if(G->arcs[i][j].adj) num++;   if(G->arcs[j][i].adj) num++; }
 G->arcnum-=num;
 for(j=i+1;j<=G->vexnum;j++)
  for(k=1;k<=G->vexnum;k++)
        G->arcs[j-1][k]=G->arcs[j][k];
 for(j=i+1;j<=G->vexnum;j++)
  for(k=1;k<=G->vexnum-1;k++)
       G->arcs[k][j-1]=G->arcs[k][j];
 G->vexnum--;
 }
}

总代码

# define INFINITY 1000
# define MAX_VERTEX_NUM 20 //最大值
# define OK 1
#define MAX_VERTEX_NUM 20// 定义常量MAX_VERTEX_NUM为20
#include<stdio.h>
#include<stdlib.h> 
#include <iostream>
#define MaxQueueSizea 100;
typedef enum{DG,DN,UDG,UDN} GraphKind;// {有向图,有向网,无向图,无向网}
typedef int EType;//定义 EType 类型为 int
typedef int InfoType;
typedef int VertexType;//定义 VertexType 类型为 int
typedef struct ArcCell
{ 
	EType adj; //EType 是顶点关系类型。对无权图,用 1 或 0 表示相邻否;
//带权图,则为权值类型
 	InfoType *info; //该弧相关信息的指针
	}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{ 	VertexType vexs[MAX_VERTEX_NUM];//顶点向量
 	AdjMatrix arcs; //邻接矩阵
 	int vexnum,arcnum; //图的当前顶点数和弧数
 	GraphKind kind; //图的种类标志
}MGraph;
	typedef int VertexType;
	typedef int InfoType;
	typedef struct ArcNode //定义图的存储结构
		{ 	int adjvex; //该弧所指向的顶点的位置
 			struct ArcNode *nextarc; //指向下一条弧的指针
 			InfoType *info; //该弧相关信息的指针
		}ArcNode;
	typedef struct VNode
		{ 	VertexType data; //顶点信息
 			ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
			}VNode,AdjList[MAX_VERTEX_NUM];
	typedef struct
	{ 		AdjList vertices;
 			int vexnum,arcnum; //图的当前顶点数和弧数
 			int kind; //图的种类标志
	}ALGraph;
//3、下面的函数是采用数组(邻接矩阵)表示法,构造无向网,请填空。
int CreatUDN(MGraph &G)
{ int IncInfo,i,j,k,v1,v2,w;
 printf("Please input the number of G.vexnum (eg,G.vexnum=4):");
 scanf("%d",&G.vexnum);//输入顶点数 G.vexnum
 printf("Please input the number of G.arcnum (eg,G.arcnum=4): ");
 scanf("%d",&G.arcnum);//输入边数 G.arcnum
 printf("Please input IncInfo (0 for none) : ");
 scanf("%d",&IncInfo);//输入弧相关信息
 for(i=0;i<G.vexnum;++i)
 for(j=0;j<G.vexnum;++j)
 	{ G.arcs[i][j].adj=INFINITY; //初始化邻接矩阵
 	G.arcs[i][j].info=NULL;
 	}
 	printf("Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...\n");
 for(k=0;k<G.arcnum;k++ ) //构造邻接矩阵
{ 	printf("Please input the %dth arc's v1 and v2:",k+1);//输入一条边
 	scanf("%d,%d",&v1,&v2);
 	printf("Please input the arc's weight :");
 	//canf("%d",&w); //输入边的权值
 	i=v1; j=v2;
 	i--; j--;
 	G.arcs[i][j].adj=G.arcs[j][i].adj=1;
 	if(IncInfo)
 	{printf("Please input the %dth arc's Info :",k+1);
	//scanf("%s",G.arcs[i][j].info);
 	}
 } 
 return (OK);
} 
void Delvi(MGraph *G,int i)/*在图 G中删除顶点I*/
{int num,j,k;
if(i<1||i>G->vexnum)
        {printf("error");  exit(0);}
else
{num=0;
 for(j=1;j<=G->vexnum;j++)
  {if(G->arcs[i][j].adj) num++;   if(G->arcs[j][i].adj) num++; }
 G->arcnum-=num;
 for(j=i+1;j<=G->vexnum;j++)
  for(k=1;k<=G->vexnum;k++)
        G->arcs[j-1][k]=G->arcs[j][k];
 for(j=i+1;j<=G->vexnum;j++)
  for(k=1;k<=G->vexnum-1;k++)
       G->arcs[k][j-1]=G->arcs[k][j];
 G->vexnum--;
 }
}
void print_Great(MGraph &G) 
{
	int i,j;
 	printf("The Graph is :\n");
 	for(i=0;i<G.vexnum ;i++)
	{
	for(j=0;j< G.vexnum;j++)
 	if(G.arcs[i][j].adj == INFINITY) printf("0");
	else printf("%d", G.arcs[i][j].adj); 
	printf("\n");
	} 
}
int main(){
	MGraph G;
	if(CreatUDN(G)){
		print_Great(G);
		Delvi(&G,2);
		print_Great(G);
	}
} 

代码运行图片
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

烈焰星辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值