/*用邻接矩阵实现图*/
#include
#include
#define WItem int
typedef struct graph *Graph;
struct graph
{
WItem NoEdge; /*无边标记*/
int n; /*顶点数*/
int e; /*边数*/
WItem **a; /*邻接矩阵*/
}AWDgraph;
Graph Graphinit(int n,WItem noEdge) /*创建图*/
{
int i,j;
Graph G=(struct graph *)malloc(sizeof (*G));
G->n=n;
G->e=0;
G->NoEdge=noEdge;
a=(WItem**)malloc(sizeof(WItem)*n*n);
for(i=0;i
{
for (j=0;j
G->a[i][j]=G->NoEdge;
}
return G;
}
int GraphEdges(Graph(G)) /*输出边数*/
{return G->e;}
int GraphVertices(Graph(G)) /*输出顶点数*/
{return G->n;}
int GraphExist(int i,int j,Graph G) /*判断边是否存在*/
{
if(i<1||j<1||i>G->n||G->a[i][j]==G->NoEdge) return 0;
return 1;
}
void GraphAdd(int i,int j,WItem w,Graph G) /*加入一条边*/
{
if(i<1||j<1||i>G->n||G->n||i==j||G->a[i][j]!=G->NoEdge)
printf("Bad input");
G->a[i][j]=w;
G->e++;
}
void GraphDelete(int i,int j,Graph G) /*删除一条边*/
{
if(i<1||j<1||i>G->n||j>G->n||G->a[i][j]==G->NoEdge)
printf("Bad input");
G->a[i][j]=G->NoEdge;
G->e--;
}
int OutDegree(int i,Graph G) /*计算出度*/
{
int j,sum=0;
if(i<1||i>G->n) printf("Bad input");
for(j=1;j<=G->n;j++)
if(G->a[i][j]!=G->NoEdge) sum++;
return sum;
}
int InDegree(int i,Graph G) /*计算入度*/
{
int j,sum=0;
if(i<1||i>G->n) printf("Bad input");
for(j=1;j<=G->n;j++)
if(G->a[i][j]!=G->NoEdge) sum++;
return sum;
}
/*输出表*/
void GraphOut(Graph G)
{
int i,j;
for(i=1;i<=G->n;i++)
{
for(j=1;j<=G->n;j++)
{printf("%d",G->a[i][j]);
printf("\n");}
}
}
void main() /*测试该图类型数据结构算法*/
{
int p,q,n,e,i,j,w,noEdge;
Graph G;
noEdge=0;
printf("几个结点\n");
scanf("%d",&n);
Graph Graphinit(int n,WItem noEdge);
printf("加入几条边\n");
scanf("%d",&e);
for(p=0;p
printf("边的权值为多少?");
scanf("%d",&w);
printf("在哪加边?");
scanf("%d,%d",&i,&j);
void GraphAdd(int i,int j, int w,Graph G);
}
for(i=1;i<=G->n;i++)
{
for(j=1;j<=G->n;j++)
printf("%d",G->a[i][j]);
}
}