graphml2dimacs.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/python3.5
  2. import sys
  3. import re
  4. import xml.etree.ElementTree as ET
  5. ET.register_namespace('', "http://graphml.graphdrawing.org/xmlns")
  6. def get_namespace(element):
  7. m = re.match('\{.*\}', element.tag)
  8. return m.group(0) if m else ''
  9. adjacency_list = [[]]
  10. node_map = {}
  11. node_weight = [0]
  12. nodex_index = 1
  13. tree = ET.parse(sys.argv[1])
  14. namespace = get_namespace(tree.getroot())
  15. for elem in tree.iterfind(namespace + 'key[@attr.name="weight"]') :
  16. weight_id = elem.attrib["id"]
  17. root = tree.find(namespace + "graph")
  18. for elem in root:
  19. # get node
  20. if elem.tag == namespace + 'node':
  21. # print (elem.attrib['id'], " weight: ")
  22. node_map[elem.attrib['id']] = nodex_index
  23. while len(adjacency_list) < nodex_index:
  24. adjacency_list.append([])
  25. nodex_index += 1
  26. # get weight
  27. for sub_elem in elem:
  28. # print (sub_elem.attrib['key'])
  29. if sub_elem.attrib['key'] == weight_id:
  30. # print (sub_elem.text)
  31. node_weight.append(int(sub_elem.text))
  32. # get edge
  33. elif elem.tag == namespace + 'edge':
  34. source_index = node_map[elem.attrib['source']]
  35. target_index = node_map[elem.attrib['target']]
  36. adjacency_list[source_index].append(target_index);
  37. adjacency_list[target_index].append(source_index);
  38. # for i in range(0, len(node_weight)):
  39. # print ("%s(%s) " % (i, node_weight[i]))
  40. # before mapp
  41. # nodex_index = 0
  42. # for node_list in adjacency_list:
  43. # if (nodex_index == 0) :
  44. # nodex_index = 1
  45. # continue;
  46. # print (nodex_index , end=' : ')
  47. # for v in node_list:
  48. # print (v, end=' ')
  49. # print ()
  50. # nodex_index += 1
  51. node_map.clear()
  52. new_node_index = 1
  53. nodex_index = 0
  54. for node_list in adjacency_list:
  55. if (nodex_index == 0):
  56. nodex_index = 1
  57. continue
  58. if (len(node_list) != 0):
  59. node_map[nodex_index] = new_node_index
  60. new_node_index += 1
  61. nodex_index += 1
  62. # new node_weight
  63. new_node_weight = []
  64. node_index = 0
  65. for i in range(1, len(adjacency_list) - 1):
  66. if (i in node_map):
  67. if (len(new_node_weight) - 1 < node_map[i]):
  68. new_node_weight.extend([0]*(node_map[i] + 1 - len(new_node_weight)))
  69. new_node_weight[node_map[i]] = node_weight[i]
  70. # new graph
  71. node_count = new_node_index - 1
  72. edge_count = 0
  73. for node_list in adjacency_list:
  74. edge_count += len(node_list)
  75. edge_count = (int)(edge_count / 2)
  76. print ("p edge %s %s" % (node_count, edge_count))
  77. for i in range(1, len(new_node_weight)):
  78. print ("v %s %s" % (i, new_node_weight[i]))
  79. nodex_index = 0
  80. for node_list in adjacency_list:
  81. if (nodex_index == 0):
  82. nodex_index = 1
  83. continue
  84. if (len(node_list) == 0):
  85. nodex_index += 1
  86. continue
  87. for v in node_list:
  88. if (node_map[nodex_index] < node_map[v]):
  89. print ("e %s %s" % (node_map[nodex_index], node_map[v]))
  90. nodex_index += 1
  91. # for key in node_map:
  92. # print ("%s -> %s" %(key, node_map[key]), end = ', ')