【复杂网络系列】图模型语言(graph model language)gml格式文件生成代码

python代码


edges格式如下:

edges={}

edges[node1]={}

edges[node2]={}

edges[node1][node2]=1


def gen_gml_file(edges,filename):

    """

    生成gml格式文件

    """
    fp_out=open(filename,'w')
    fp_out.write("graph [\n")
    for node in edges.keys():
        fp_out.write("\tnode [\n")
        fp_out.write("\t\tid "+str(node)+"\n")
        # insert other node attributes here
        fp_out.write("\t]\n")


    for node1 in edges.keys():
        for node2 in edges[node1].keys():
            fp_out.write("\tedge [\n")
            fp_out.write("\t\tsource "+str(node1)+"\n")
            fp_out.write("\t\ttarget "+str(node2)+"\n")
            # insert other edge attributes here
            fp_out.write("\t]\n")


    fp_out.write("]\t")


可以用gelphi软件打开gml格式的文件。


对zachary‘s karate club网络图生成的gml格式文件,如下所示

graph [

node [
id 24
]
node [
id 25
]
node [
id 26
]
node [
id 27
]
node [
id 20
]
node [
id 21
]
node [
id 22
]
node [
id 23
]
node [
id 28
]
node [
id 29
]
node [
id 1
]
node [
id 3
]
node [
id 2
]
node [
id 5
]
node [
id 4
]
node [
id 7
]
node [
id 6
]
node [
id 9
]
node [
id 8
]
node [
id 11
]
node [
id 10
]
node [
id 13
]
node [
id 12
]
node [
id 15
]
node [
id 14
]
node [
id 17
]
node [
id 16
]
node [
id 33
]
node [
id 18
]
node [
id 31
]
node [
id 30
]
node [
id 34
]
node [
id 19
]
node [
id 32
]
edge [
source 24
target 33
]
edge [
source 24
target 26
]
edge [
source 24
target 30
]
edge [
source 24
target 28
]
edge [
source 24
target 34
]
edge [
source 25
target 32
]
edge [
source 25
target 26
]
edge [
source 25
target 28
]
edge [
source 26
target 24
]
edge [
source 26
target 25
]
edge [
source 26
target 32
]
edge [
source 27
target 30
]
edge [
source 27
target 34
]
edge [
source 20
target 1
]
edge [
source 20
target 2
]
edge [
source 20
target 34
]
edge [
source 21
target 33
]
edge [
source 21
target 34
]
edge [
source 22
target 1
]
edge [
source 22
target 2
]
edge [
source 23
target 33
]
edge [
source 23
target 34
]
edge [
source 28
target 24
]
edge [
source 28
target 25
]
edge [
source 28
target 3
]
edge [
source 28
target 34
]
edge [
source 29
target 32
]
edge [
source 29
target 3
]
edge [
source 29
target 34
]
edge [
source 1
target 11
]
edge [
source 1
target 13
]
edge [
source 1
target 12
]
edge [
source 1
target 20
]
edge [
source 1
target 14
]
edge [
source 1
target 22
]
edge [
source 1
target 18
]
edge [
source 1
target 3
]
edge [
source 1
target 2
]
edge [
source 1
target 5
]
edge [
source 1
target 4
]
edge [
source 1
target 7
]
edge [
source 1
target 6
]
edge [
source 1
target 9
]
edge [
source 1
target 8
]
edge [
source 1
target 32
]
edge [
source 3
target 10
]
edge [
source 3
target 14
]
edge [
source 3
target 33
]
edge [
source 3
target 28
]
edge [
source 3
target 29
]
edge [
source 3
target 1
]
edge [
source 3
target 2
]
edge [
source 3
target 4
]
edge [
source 3
target 9
]
edge [
source 3
target 8
]
edge [
source 2
target 20
]
edge [
source 2
target 14
]
edge [
source 2
target 22
]
edge [
source 2
target 18
]
edge [
source 2
target 31
]
edge [
source 2
target 1
]
edge [
source 2
target 3
]
edge [
source 2
target 4
]
edge [
source 2
target 8
]
edge [
source 5
target 1
]
edge [
source 5
target 11
]
edge [
source 5
target 7
]
edge [
source 4
target 13
]
edge [
source 4
target 14
]
edge [
source 4
target 1
]
edge [
source 4
target 3
]
edge [
source 4
target 2
]
edge [
source 4
target 8
]
edge [
source 7
target 1
]
edge [
source 7
target 5
]
edge [
source 7
target 17
]
edge [
source 7
target 6
]
edge [
source 6
target 1
]
edge [
source 6
target 11
]
edge [
source 6
target 17
]
edge [
source 6
target 7
]
edge [
source 9
target 1
]
edge [
source 9
target 33
]
edge [
source 9
target 3
]
edge [
source 9
target 31
]
edge [
source 9
target 34
]
edge [
source 8
target 1
]
edge [
source 8
target 3
]
edge [
source 8
target 2
]
edge [
source 8
target 4
]
edge [
source 11
target 1
]
edge [
source 11
target 5
]
edge [
source 11
target 6
]
edge [
source 10
target 3
]
edge [
source 10
target 34
]
edge [
source 13
target 1
]
edge [
source 13
target 4
]
edge [
source 12
target 1
]
edge [
source 15
target 33
]
edge [
source 15
target 34
]
edge [
source 14
target 1
]
edge [
source 14
target 3
]
edge [
source 14
target 2
]
edge [
source 14
target 4
]
edge [
source 14
target 34
]
edge [
source 17
target 7
]
edge [
source 17
target 6
]
edge [
source 16
target 33
]
edge [
source 16
target 34
]
edge [
source 33
target 24
]
edge [
source 33
target 30
]
edge [
source 33
target 15
]
edge [
source 33
target 21
]
edge [
source 33
target 16
]
edge [
source 33
target 19
]
edge [
source 33
target 32
]
edge [
source 33
target 31
]
edge [
source 33
target 23
]
edge [
source 33
target 34
]
edge [
source 33
target 3
]
edge [
source 33
target 9
]
edge [
source 18
target 1
]
edge [
source 18
target 2
]
edge [
source 31
target 9
]
edge [
source 31
target 33
]
edge [
source 31
target 2
]
edge [
source 31
target 34
]
edge [
source 30
target 24
]
edge [
source 30
target 33
]
edge [
source 30
target 27
]
edge [
source 30
target 34
]
edge [
source 34
target 24
]
edge [
source 34
target 10
]
edge [
source 34
target 27
]
edge [
source 34
target 15
]
edge [
source 34
target 14
]
edge [
source 34
target 16
]
edge [
source 34
target 19
]
edge [
source 34
target 32
]
edge [
source 34
target 31
]
edge [
source 34
target 23
]
edge [
source 34
target 28
]
edge [
source 34
target 29
]
edge [
source 34
target 33
]
edge [
source 34
target 30
]
edge [
source 34
target 9
]
edge [
source 34
target 20
]
edge [
source 34
target 21
]
edge [
source 19
target 33
]
edge [
source 19
target 34
]
edge [
source 32
target 25
]
edge [
source 32
target 26
]
edge [
source 32
target 33
]
edge [
source 32
target 29
]
edge [
source 32
target 34
]
edge [
source 32
target 1
]
]

你可能感兴趣的:(复杂网络)