test_dimacs.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // =====================================================================================
  2. //
  3. // Filename: test_dimacs.cpp
  4. //
  5. // Description: test if the instance contain loop or multi edge
  6. //
  7. // Version: 1.0
  8. // Created: 2017年 12月 28日 星期四 06:27:25 -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 <fstream>
  18. #include <vector>
  19. #include <algorithm>
  20. using namespace std;
  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. int vertex_count = 0, edge_count = 0;
  43. while (*pos < '0' || *pos > '9') {
  44. ++pos;
  45. }
  46. while (*pos != ' ') {
  47. vertex_count = vertex_count * 10 + *pos - '0';
  48. ++pos;
  49. }
  50. //read edge_count
  51. while (*pos < '0' || *pos > '9') {
  52. ++pos;
  53. }
  54. while (*pos >= '0' && *pos <= '9') {
  55. edge_count = edge_count * 10 + *pos - '0';
  56. ++pos;
  57. }
  58. //read vertex_weight
  59. vertex_weight.resize(vertex_count + 1);
  60. int v,w;
  61. for(vector<vector<int>>::size_type i=1; i<vertex_weight.size(); ++i){
  62. v = w = 0;
  63. //read vertex
  64. while (*pos < '0' || *pos > '9') {
  65. ++pos;
  66. }
  67. while (*pos != ' ') {
  68. v = v * 10 + *pos - '0';
  69. ++pos;
  70. }
  71. //read weight
  72. while (*pos < '0' || *pos > '9') {
  73. ++pos;
  74. }
  75. while (*pos >= '0' && *pos <= '9') {
  76. w = w * 10 + *pos - '0';
  77. ++pos;
  78. }
  79. vertex_weight[v]=w;
  80. }
  81. //read adjacency_list
  82. adjacency_list.resize(vertex_count + 1);
  83. vector<int> vertex_degree(vertex_count + 1, 0);
  84. char *stash_pos = pos;
  85. int v1, v2;
  86. for (int i = 0; i < edge_count; ++i) {
  87. v1 = v2 = 0;
  88. //read v1
  89. while (*pos < '0' || *pos > '9') {
  90. ++pos;
  91. }
  92. while (*pos != ' ') {
  93. v1 = v1 * 10 + *pos - '0';
  94. ++pos;
  95. }
  96. //read v2
  97. while (*pos < '0' || *pos > '9') {
  98. ++pos;
  99. }
  100. while (*pos >= '0' && *pos <= '9') {
  101. v2 = v2 * 10 + *pos - '0';
  102. ++pos;
  103. }
  104. vertex_degree[v1]++;
  105. vertex_degree[v2]++;
  106. }
  107. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  108. adjacency_list[v].reserve(vertex_degree[v]);
  109. }
  110. pos = stash_pos;
  111. for (int i = 0; i < edge_count; ++i) {
  112. v1 = v2 = 0;
  113. //read v1
  114. while (*pos < '0' || *pos > '9') {
  115. ++pos;
  116. }
  117. while (*pos != ' ') {
  118. v1 = v1 * 10 + *pos - '0';
  119. ++pos;
  120. }
  121. //read weight
  122. while (*pos < '0' || *pos > '9') {
  123. ++pos;
  124. }
  125. while (*pos >= '0' && *pos <= '9') {
  126. v2 = v2 * 10 + *pos - '0';
  127. ++pos;
  128. }
  129. adjacency_list[v1].push_back(v2);
  130. adjacency_list[v2].push_back(v1);
  131. }
  132. delete[] data;
  133. }/*}}}*/
  134. void test() {
  135. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  136. std::sort(adjacency_list[v].begin(), adjacency_list[v].end());
  137. for (size_t i = 0; i < adjacency_list[v].size(); ++i) {
  138. if (adjacency_list[v][i] == (int)v) {
  139. std::cout << "loop" << std::endl;
  140. abort();
  141. }
  142. if (i > 0 && adjacency_list[v][i] == adjacency_list[v][i - 1]) {
  143. std::cout << "multi edge" << std::endl;
  144. abort();
  145. }
  146. }
  147. }
  148. }
  149. int main(int argc, char const *argv[]) {
  150. if (argc != 2) {
  151. std::cout << "usage" << std::endl;
  152. return 1;
  153. }
  154. fast_read(argv[1]);
  155. test();
  156. return 0;
  157. }