Pajek的网络输入数据格式为*.net,而今天想下的一些数据都是*.gml格式的。
http://www-personal.umich.edu/~mejn/netdata/
Mark Newman在提供这些开源的数据的同时,提供了他写的一个简单GML分析器的C源码。我修改了一点,加了个主函数用来生成*.net文件。
如果*.gml文件提供更多信息,如节点的分类等,也可以修改这个简单的GML2Pajek来增加更多内容。
如果需要读取*.gml中更多信息,需要增加的地方:
1. 数据结构
2. 对应GML分析器中的函数
3. 将NETWORK输出到Pajek支持格式的函数(可能还要打开新文件,如*.vec)
我修改了Mark Newman提供的分析器的一部分。但是我认为Mark Newman放出这个程序的时候一定是有经过测试的,我不知道为什么我不修改就不能用了(不能转换他主页上提供的gml格式数据)。可能和一些我不知道的原因有关。
同时由于Newman提供的部分数据带有结点的value,我对每个*.gml都会生成对应的*.vec文件。如果*.gml不带有value,这个*.vec是多余的,我没有做判断。
下面是源代码,Newman提供的gml parser我做了一些修改。
gml2pajek.c
// Program to read a network stored in a GML file into a NETWORK struct
//
// Yi-Shi Lin 12 AUG 03
//
// A simple parser in C that will read the files into a data structure is needed
// It is provided by Mark Newman:
// http://www-personal.umich.edu/~mejn/netdata/readgml.zip
//
// If more infomation in *.gml is needed
// Please modify the data structure defined in network.h
// and the corresponding functions in readgml.c and gml2pajek.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "readgml.h"
#define FILENAMELEN 100
// Function to open *.gml and *.net
int open_file(FILE **gml_file, FILE **net_file, FILE **vec_file)
{
char filename[FILENAMELEN];
int len;
printf("\ninput *.gml\n");
scanf("%s", filename);
len=strlen(filename);
if (((*gml_file)=fopen(filename, "r"))==NULL)
{
printf("fail to open \"%s\"\n", filename);
return 0;
}
printf("%s has been opened...\n", filename);
filename[len-3]='n';
filename[len-2]='e';
filename[len-1]='t';
if (((*net_file)=fopen(filename, "w"))==NULL)
{
printf("fail to open \"%s\"\n", filename);
return 0;
}
filename[len-3]='v';
filename[len-2]='e';
filename[len-1]='c';
if (((*vec_file)=fopen(filename, "w"))==NULL)
{
printf("fail to open \"%s\"\n", filename);
return 0;
}
return 1;
}
// Function to close *.gml and *.net
void close_file(FILE *gml_file, FILE *net_file, FILE *vec_file)
{
fclose(gml_file);
fclose(net_file);
fclose(vec_file);
}
// Function to write the network into *.net
void write_network(NETWORK *network, FILE *net_file, FILE *vec_file)
{
int i,j;
fprintf(net_file, "*Vertices %d\n", network->nvertices);
for (i=0; i<network->nvertices; i++)
{
fprintf(net_file, "%d ", i+1);
if (network->vertex[i].label)
fprintf(net_file, "\"%s\"", network->vertex[i].label);
fprintf(net_file, "\n");
}
fprintf(net_file, "*Edges\n");
for (i=0; i<network->nvertices; i++)
{
for (j=0; j<network->vertex[i].degree; j++)
{
EDGE *tmp_edge=&(network->vertex[i].edge[j]);
fprintf(net_file, "%d %d %.lf\n", i+1, tmp_edge->t