创建无向图代码更改
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=0; //初始化邻接矩阵
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 Max_du(MGraph *G)
{int num,i,j; int max=0;
for(i=0;i<G->vexnum;i++)
{num=0;
for(j=0;j<G->vexnum;j++) if(G->arcs[i][j].adj) num++;
if(max<num) max=num;
}
printf("The max degree is:%d\n",max);
}
完整代码
# 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=0; //初始化邻接矩阵
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 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");
}
}
void Max_du(MGraph *G)
{int num,i,j; int max=0;
for(i=0;i<G->vexnum;i++)
{num=0;
for(j=0;j<G->vexnum;j++) if(G->arcs[i][j].adj) num++;
if(max<num) max=num;
}
printf("The max degree is:%d\n",max);
}
int main(){
MGraph G;
if(CreatUDN(G)){
print_Great(G);
Max_du(&G);
print_Great(G);
}
}
代码运行截图
邻接表的创建与打印
int CreateDG(ALGraph &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 the number of IncInfo (0 for none) : ");
scanf("%d",&IncInfo); //弧的信息没有,就输入0
for(i=0;i<G.vexnum;++i) //初始化图
{ G.vertices[i].data=i;
G.vertices[i].firstarc=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);
i=v1; j=v2;
i--; j--;
ArcNode *p;
p=(ArcNode*)malloc(sizeof(ArcNode)); //申请一个ArcNode类型的结点
if(!p)
{ printf("Overflow!");
return (0);
}
p->adjvex=j; //给 p->adjvex赋值
p->nextarc=G.vertices[i].firstarc;
p->info=(int *)malloc(sizeof(int));
G.vertices[i].firstarc=p;
if(IncInfo)
{ printf("Please input the info :");
scanf("%s",(p->info)); //input information
}
}
return (OK);
}
//6、补充完整输出图的邻接矩阵函数。
void print_Great(ALGraph &G)
{ int i;
ArcNode *p;
printf("The Graph is :\n");
for(i=0;i<G.vexnum;i++)
{
printf("%d",i+1);
p=G.vertices[i].firstarc;//给p赋值为依附于第i+1个顶点的弧的指针
while(p)
{
printf("->%d",(p->adjvex)+1);
p=p->nextarc;//让p指向链表只下一个结点
}
printf("\n");
}
}
邻接表法实现有向图中顶点的最大出度值
void Max_du(ALGraph *G)
{ArcNode *p;
p = (ArcNode*)malloc(sizeof(ArcNode));
int num,i,j; int max=0;
for(i=0;i<G->vexnum;i++)
{num=0;
p=G->vertices[i].firstarc;
while(p) { p=p->nextarc; num++; }
if (max<num) max=num;
}
printf("The max degree is:%d\n",max);
}
完整代码
# 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 CreateDG(ALGraph &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 the number of IncInfo (0 for none) : ");
scanf("%d",&IncInfo); //弧的信息没有,就输入0
for(i=0;i<G.vexnum;++i) //初始化图
{ G.vertices[i].data=i;
G.vertices[i].firstarc=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);
i=v1; j=v2;
i--; j--;
ArcNode *p;
p=(ArcNode*)malloc(sizeof(ArcNode)); //申请一个ArcNode类型的结点
if(!p)
{ printf("Overflow!");
return (0);
}
p->adjvex=j; //给 p->adjvex赋值
p->nextarc=G.vertices[i].firstarc;
p->info=(int *)malloc(sizeof(int));
G.vertices[i].firstarc=p;
if(IncInfo)
{ printf("Please input the info :");
scanf("%s",(p->info)); //input information
}
}
return (OK);
}
//6、补充完整输出图的邻接矩阵函数。
void print_Great(ALGraph &G)
{ int i;
ArcNode *p;
printf("The Graph is :\n");
for(i=0;i<G.vexnum;i++)
{
printf("%d",i+1);
p=G.vertices[i].firstarc;//给p赋值为依附于第i+1个顶点的弧的指针
while(p)
{
printf("->%d",(p->adjvex)+1);
p=p->nextarc;//让p指向链表只下一个结点
}
printf("\n");
}
}
void Max_du(ALGraph *G)
{ArcNode *p;
p = (ArcNode*)malloc(sizeof(ArcNode));
int num,i,j; int max=0;
for(i=0;i<G->vexnum;i++)
{num=0;
p=G->vertices[i].firstarc;
while(p) { p=p->nextarc; num++; }
if (max<num) max=num;
}
printf("The max degree is:%d\n",max);
}
int main(){
ALGraph G;
if(CreateDG(G)){
print_Great(G);
Max_du(&G);
print_Great(G);
}
}
运行结果截图