图的邻接矩阵表示
这里仅实现了UDG(无向图)的输入,有向图,有向网,无向网自己照葫芦画瓢。
// Graph_in_adjacent_matrix.h
#ifndef GRAPH_IN_ADJACENT_MATRIX_H_INCLUDED
#define GRAPH_IN_ADJACENT_MATRIX_H_INCLUDED
#include <DScourse.h>
#include <stdio.h>
#include<limits.h>
//最大数据
#define MAX_ADJ_LIMIT UINT_MAX //极限大
#define MAX_GRAPH_SIZE 40 //图的最大顶点数
#define MAX_INFO_LEN 255 //边/弧 的info的最大长度
typedef enum {false=0,true=1} Bool;
typedef unsigned int AdjType ; //weight的数据类型
typedef char * InfoType;
typedef unsigned char VElemType;//顶点的数据类型
typedef enum {DG=1,DN=2,UDG=3,UDN=4} GraphKind;//四种图的类型
typedef struct ArcCell{
AdjType weight;
InfoType *info;
}ArcCell,ArcMatrix[MAX_GRAPH_SIZE][MAX_GRAPH_SIZE];//矩阵的定义
typedef struct{
GraphKind kind;
ArcMatrix matrix; //二维数组,存放弧的矩阵
VElemType ver[MAX_GRAPH_SIZE];//一维数组,存放顶点
int verSize,arcSize;//verSize: 顶点数量,arcSize弧的数量
}MGraph; //图的定义
//下面为四种不同类型图的创建
Status CreateGraph(MGraph * M);
Status CreateDG(MGraph *);
Status CreateDN(MGraph *);
Status CreateUDG(MGraph*);
Status CreateUDN(MGraph*);
Status Locate(MGraph*,VElemType,int *);//locate 查找指定数据在顶点表中的位置
Status InputInfo(InfoType *); //输入info
Status PrintGraph(MGraph *); //打印图的结构
VElemType InputVer(); //获取顶点数据的输入
#endif // GRAPH_IN_ADJACENT_MATRIX_H_INCLUDED
//graph_in_adjacent_matrix.c
//以上函数的具体实现代码
#include<stdio.h>
#include<stdlib.h>
#include"graph_in_adjacent_matrix.h"
//判断数据类型并选择合适的函数
Status CreateGraph(MGraph * M)
{
printf("Please input graph kind , DG=1,DN=2,UDG=3,UDN=4 :");
GraphKind kind;
scanf("%d",&kind);
switch(kind)
{
case DG:
return CreateDG(M);
case DN:
return CreateDN(M);
case UDG:
return CreateUDG(M);
case UDN:
return CreateUDN(M);
}
}
Status CreateUDG(MGraph * M)
{
int i,j,k;
VElemType tail,head;
AdjType weight;
int vt,vh;
Bool infoOrNot;
printf("please input verSize arcSize infoOrNot:");
scanf("%d %d %d",&M->verSize,&M->arcSize,&infoOrNot);
//输入顶点的数量,边的数量,是否输入备注
for(i=0; i<MAX_GRAPH_SIZE; i++) M->ver[i]=0; //初始化顶点数组
printf("start input ver:\t");
for(i=0; i<M->verSize; i++) M->ver[i]=InputVer();//循环输入各个顶点的数据
printf("start input adj:\n");
for(i=0; i<MAX_GRAPH_SIZE; i++)
for(j=0; j<MAX_GRAPH_SIZE; j++)
M->matrix[i][j].info=NULL,M->matrix[i][j].weight=0;//初始化表示边的二维数组
for(k=0; k<M->arcSize; k++)
{
tail=InputVer();
head=InputVer();
//scanf("%c%c",&tail,&head);
Locate(M,tail,&vt);
Locate(M,head,&vh);
M->matrix[vt][vh].weight=1;
if(infoOrNot) InputInfo(M->matrix[vt][vh].info);
M->matrix[vh][vt]=M->matrix[vt][vh];
}//输入各个边的tail和head
}
Status CreateDG(MGraph * M)
{
return OK;
}
Status CreateDN(MGraph * M)
{
return OK;
}
Status CreateUDN(MGraph * M)
{
return OK;
}
Status Locate(MGraph * M,VElemType ver,int * pos)
{
int i;
for(i=0; i<M->verSize; i++)
{
if(M->ver[i]==ver)
{
*pos=i;
return OK;
}
}
return ERROR;
}
VElemType InputVer(){
VElemType c;
do{
c=getchar();
}while(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')));//防止输入不想要的字符,比如回车符
return c;
}
Status InputInfo(InfoType * info)
{
printf("please input info\n");
(*info)=(char*)malloc(sizeof(char)*MAX_INFO_LEN);
scanf("%s",*info);
return OK;
}
Status PrintGraph(MGraph * M)
{
printf("\n");
printf("GraphKind=%d\n",M->kind);
int i,j;
printf("ver:\t");
for(i=0; i<M->verSize; i++)printf("%-6c",M->ver[i]);
printf("\n\t");
for(i=0; i<M->verSize; i++)printf("%-6d",i);
for(i=0; i<M->verSize; i++){
printf("\n%-3d\t",i);
for(j=0; j<M->verSize; j++)
{
printf("%-6d",M->matrix[i][j]);
}
}
printf("\n");
}