1. 图的表示:给定图数据文件,计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中。
源码:
#include <stdio.h>
#define OK 1
#define ERROR 0
#define MAX_VERTEX_NUM 20
typedef int VrType;
typedef int VertexType;
typedef struct AraCell{
VrType adj;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
}MGraph;
//输出邻接矩阵
void printGraph(MGraph G)
{
int i,j;
for(i=0;i<G.vexnum;i++)
printf("%2d",i+1);
printf("\n");
for(i=0;i<G.vexnum;i++){
printf(" %2d ",G.vexs[i]);
printf("%2d",i+1);
for(j=0;j<G.vexnum;j++)
printf("%2d",G.arcs[i][j]);
printf("\n");
}
}
//主文件
int main()
{
int n;
FILE *in, out;
in =fopen("e:\\tinyG.txt","r");
n=0;
while (true) {
r= fscanf(f,"%lf",&d);
if (r == 1) {
n++;
printf("%d==%g",n,d);
a[i]=d;
i++;
} else if (0==r) {
fgetc(f);
} else break;
}
int max = a[0];
for (i = 1; i < n; ++i)
{
if (a[i] > max)
max = a[i];
}
double b[14][14]={0};
int t=0,s=0;
for(int j=0;j<n;j++)
{
t=a[j];
j++;
s=a[j];
b[t][s]=1;
}
fclose(f);
out = fopen("E:\\tinyG_matrix.txt","wt");
for(t = 0;t < 14;t++)
{
for(s = 0;s < 14;s++)
{
fprintf(out,"%8.0lf",b[t][s]);
}
}
fclose(out);
getchar();
MGraph G;
VertexType u,u1,w1,w2;
scanf("%d,%d",&G.vexnum,&G.arcnum);
printGraph(G);
return 0;
}