dimacs2metis.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // =====================================================================================
  2. //
  3. // Filename: dimacs2metis.cpp
  4. //
  5. // Description: convert dimacs graph to metis format
  6. //
  7. // Version: 1.0
  8. // Created: 2017年 12月 28日 星期四 07:03:07 -00
  9. // Revision: none
  10. // Compiler: g++
  11. //
  12. // Author: Jinkun Lin, jkunlin@gmail.com
  13. // Organization: School of EECS, Peking University
  14. //
  15. // =====================================================================================
  16. #include <iostream>
  17. #include <vector>
  18. #include <fstream>
  19. using namespace std;
  20. int vertex_count = 0, edge_count = 0;
  21. vector<vector<int>> adjacency_list;
  22. vector<int> vertex_weight;
  23. // read graph file into adjacency_list
  24. void fast_read(string file_name) {/*{{{*/
  25. ifstream in_file(file_name);
  26. if (!in_file) {
  27. cout << "in_file error" << endl;
  28. exit(1);
  29. }
  30. in_file.seekg (0, in_file.end);
  31. size_t file_len = in_file.tellg();
  32. in_file.seekg (0, in_file.beg);
  33. char *data = new char [file_len];
  34. in_file.read(data, file_len);
  35. in_file.close();
  36. //skip comments
  37. char *pos = data;
  38. while (*pos == '%') {
  39. while(*(pos++) != '\n');
  40. }
  41. //read vertex_count
  42. while (*pos < '0' || *pos > '9') {
  43. ++pos;
  44. }
  45. while (*pos != ' ') {
  46. vertex_count = vertex_count * 10 + *pos - '0';
  47. ++pos;
  48. }
  49. //read edge_count
  50. while (*pos < '0' || *pos > '9') {
  51. ++pos;
  52. }
  53. while (*pos >= '0' && *pos <= '9') {
  54. edge_count = edge_count * 10 + *pos - '0';
  55. ++pos;
  56. }
  57. //read vertex_weight
  58. vertex_weight.resize(vertex_count + 1);
  59. int v,w;
  60. for(vector<vector<int>>::size_type i=1; i<vertex_weight.size(); ++i){
  61. v = w = 0;
  62. //read vertex
  63. while (*pos < '0' || *pos > '9') {
  64. ++pos;
  65. }
  66. while (*pos != ' ') {
  67. v = v * 10 + *pos - '0';
  68. ++pos;
  69. }
  70. //read weight
  71. while (*pos < '0' || *pos > '9') {
  72. ++pos;
  73. }
  74. while (*pos >= '0' && *pos <= '9') {
  75. w = w * 10 + *pos - '0';
  76. ++pos;
  77. }
  78. vertex_weight[v]=w;
  79. }
  80. //read adjacency_list
  81. adjacency_list.resize(vertex_count + 1);
  82. vector<int> vertex_degree(vertex_count + 1, 0);
  83. char *stash_pos = pos;
  84. int v1, v2;
  85. for (int i = 0; i < edge_count; ++i) {
  86. v1 = v2 = 0;
  87. //read v1
  88. while (*pos < '0' || *pos > '9') {
  89. ++pos;
  90. }
  91. while (*pos != ' ') {
  92. v1 = v1 * 10 + *pos - '0';
  93. ++pos;
  94. }
  95. //read v2
  96. while (*pos < '0' || *pos > '9') {
  97. ++pos;
  98. }
  99. while (*pos >= '0' && *pos <= '9') {
  100. v2 = v2 * 10 + *pos - '0';
  101. ++pos;
  102. }
  103. vertex_degree[v1]++;
  104. vertex_degree[v2]++;
  105. }
  106. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  107. adjacency_list[v].reserve(vertex_degree[v]);
  108. }
  109. pos = stash_pos;
  110. for (int i = 0; i < edge_count; ++i) {
  111. v1 = v2 = 0;
  112. //read v1
  113. while (*pos < '0' || *pos > '9') {
  114. ++pos;
  115. }
  116. while (*pos != ' ') {
  117. v1 = v1 * 10 + *pos - '0';
  118. ++pos;
  119. }
  120. //read weight
  121. while (*pos < '0' || *pos > '9') {
  122. ++pos;
  123. }
  124. while (*pos >= '0' && *pos <= '9') {
  125. v2 = v2 * 10 + *pos - '0';
  126. ++pos;
  127. }
  128. adjacency_list[v1].push_back(v2);
  129. adjacency_list[v2].push_back(v1);
  130. }
  131. delete[] data;
  132. }/*}}}*/
  133. void convert(string graph_name) {
  134. ofstream out_graph(graph_name);
  135. if (!out_graph) {
  136. std::cout << "outfile error" << std::endl;
  137. abort();
  138. }
  139. out_graph << vertex_count << ' ' << edge_count << endl;
  140. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  141. out_graph << adjacency_list[v][0];
  142. for (size_t i = 1; i < adjacency_list[v].size(); ++i) {
  143. int u = adjacency_list[v][i];
  144. out_graph << ' ' << u;
  145. }
  146. out_graph << std::endl;
  147. }
  148. ofstream out_weight(graph_name + ".weights");
  149. if (!out_weight) {
  150. std::cout << "weights file error" << std::endl;
  151. abort();
  152. }
  153. // offset = -1
  154. for (int v = 0; v < vertex_count; ++v) {
  155. out_weight << v << ' ' << vertex_weight[v + 1] << std::endl;
  156. }
  157. out_graph.close();
  158. out_weight.close();
  159. }
  160. int main(int argc, char const *argv[]) {
  161. if (argc != 2) {
  162. std::cout << "usage" << std::endl;
  163. return 1;
  164. }
  165. string in_filename = argv[1];
  166. fast_read(in_filename);
  167. string graph_name;
  168. size_t begin_index = 0;
  169. if ((begin_index = in_filename.find_last_of('/')) != string::npos) {
  170. graph_name = in_filename.substr(begin_index + 1, in_filename.size() - 5 - begin_index - 1);
  171. }
  172. else {
  173. graph_name = in_filename.substr(0, in_filename.size() -5);
  174. }
  175. graph_name += ".graph";
  176. convert(graph_name);
  177. return 0;
  178. }