dimacs2txt.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // =====================================================================================
  2. //
  3. // Filename: dimcas2txt.cpp
  4. //
  5. // Description: converter
  6. //
  7. // Version: 1.0
  8. // Created: 2017年 12月 26日 星期二 07:53:19 -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. #include <algorithm>
  20. using namespace std;
  21. vector<vector<int>> adjacency_list;
  22. vector<int> vertex_weight;
  23. void fast_build(string file_name) {/*{{{*/
  24. ifstream in_file(file_name);
  25. if (!in_file) {
  26. cout << "in_file error" << endl;
  27. exit(1);
  28. }
  29. in_file.seekg (0, in_file.end);
  30. size_t file_len = in_file.tellg();
  31. in_file.seekg (0, in_file.beg);
  32. char *data = new char [file_len];
  33. in_file.read(data, file_len);
  34. in_file.close();
  35. //skip comments
  36. char *pos = data;
  37. while (*pos == '%') {
  38. while(*(pos++) != '\n');
  39. }
  40. //read vertex_count
  41. long vertex_count = 0, edge_count = 0;
  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. long v,w;
  60. for(vector<vector<long>>::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<long> vertex_degree(vertex_count + 1, 0);
  83. char *stash_pos = pos;
  84. long v1, v2;
  85. for (long 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 weight
  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 (long 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 file_name) {
  134. ofstream out_file(file_name);
  135. if (!out_file) {
  136. std::cout << "out_file error" << std::endl;
  137. abort();
  138. }
  139. out_file << adjacency_list.size() - 1 << endl;
  140. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  141. out_file << " " << vertex_weight[v];
  142. }
  143. out_file << std::endl;
  144. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  145. std::sort(adjacency_list[v].begin(), adjacency_list[v].end());
  146. }
  147. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  148. int cur_i = 0;
  149. for (size_t u = 1; u < adjacency_list.size(); ++u) {
  150. if (cur_i == (int)adjacency_list[v].size()) {
  151. out_file << ' ' << 0;
  152. continue;
  153. }
  154. if ((int)u == adjacency_list[v][cur_i]) {
  155. out_file << ' ' << 1;
  156. cur_i++;
  157. }
  158. else {
  159. out_file << ' ' << 0;
  160. }
  161. }
  162. out_file << std::endl;
  163. }
  164. out_file.close();
  165. }
  166. int main(int argc, char const *argv[]) {
  167. if (argc != 2) {
  168. std::cout << "usage: " << std::endl;
  169. return 1;
  170. }
  171. string in_filename = argv[1];
  172. fast_build(in_filename);
  173. string out_filename (in_filename.begin(), in_filename.end() - 4);
  174. out_filename += "txt";
  175. convert(out_filename);
  176. return 0;
  177. }