subgraph.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // =====================================================================================
  2. //
  3. // Filename: subgraph.cpp
  4. //
  5. // Description: output subgraph
  6. //
  7. // Version: 1.0
  8. // Created: 2018年 01月 08日 星期一 16:09:22 CST
  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 <string>
  19. #include <vector>
  20. #include <algorithm>
  21. using namespace std;
  22. vector<vector<int>> adjacency_list;
  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. data[file_len - 1] = '\0';
  37. //skip comments
  38. char *pos = data;
  39. while (*pos == 'c') {
  40. while(*(pos++) != '\n');
  41. }
  42. //read vertex_count
  43. int vertex_count = 0, edge_count = 0;
  44. while (*pos < '0' || *pos > '9') {
  45. ++pos;
  46. }
  47. while (*pos != ' ') {
  48. vertex_count = vertex_count * 10 + *pos - '0';
  49. ++pos;
  50. }
  51. //read edge_count
  52. while (*pos < '0' || *pos > '9') {
  53. ++pos;
  54. }
  55. while (*pos >= '0' && *pos <= '9') {
  56. edge_count = edge_count * 10 + *pos - '0';
  57. ++pos;
  58. }
  59. //read adjacency_list
  60. adjacency_list.resize(vertex_count + 1);
  61. vector<int> vertex_degree(vertex_count + 1, 0);
  62. char *stash_pos = pos;
  63. int v1, v2;
  64. for (int i = 0; i < edge_count && *pos != '\0'; ++i) {
  65. v1 = v2 = 0;
  66. //read v1
  67. while (*pos < '0' || *pos > '9') {
  68. ++pos;
  69. }
  70. while (*pos != ' ') {
  71. v1 = v1 * 10 + *pos - '0';
  72. ++pos;
  73. }
  74. //read v2
  75. while (*pos < '0' || *pos > '9') {
  76. ++pos;
  77. }
  78. while (*pos >= '0' && *pos <= '9') {
  79. v2 = v2 * 10 + *pos - '0';
  80. ++pos;
  81. }
  82. vertex_degree[v1]++;
  83. vertex_degree[v2]++;
  84. }
  85. for (size_t v = 1; v < adjacency_list.size(); ++v) {
  86. adjacency_list[v].reserve(vertex_degree[v]);
  87. }
  88. pos = stash_pos;
  89. for (int i = 0; i < edge_count && *pos != '\0'; ++i) {
  90. v1 = v2 = 0;
  91. //read v1
  92. while (*pos < '0' || *pos > '9') {
  93. ++pos;
  94. }
  95. while (*pos != ' ') {
  96. v1 = v1 * 10 + *pos - '0';
  97. ++pos;
  98. }
  99. //read weight
  100. while (*pos < '0' || *pos > '9') {
  101. ++pos;
  102. }
  103. while (*pos >= '0' && *pos <= '9') {
  104. v2 = v2 * 10 + *pos - '0';
  105. ++pos;
  106. }
  107. adjacency_list[v1].push_back(v2);
  108. adjacency_list[v2].push_back(v1);
  109. }
  110. delete[] data;
  111. }
  112. int main(int argc, char const *argv[]) {
  113. if (argc != 2) {
  114. std::cout << "usage" << std::endl;
  115. return 1;
  116. }
  117. string input_filename =argv[1];
  118. fast_read(input_filename);
  119. vector<int> vertex;
  120. int v;
  121. vector<int> indicator(adjacency_list.size(), 0);
  122. std::cout << "input vertex:" << std::endl;
  123. while (cin >> v) {
  124. if (indicator[v] == 0) {
  125. vertex.push_back(v);
  126. indicator[v] = 1;
  127. }
  128. }
  129. std::sort(vertex.begin(), vertex.end());
  130. for (auto v : vertex) {
  131. std::cout << v << " : ";
  132. for (auto u : adjacency_list[v]) {
  133. if (indicator[u]) {
  134. cout << u << ' ';
  135. }
  136. }
  137. std::cout << std::endl;
  138. }
  139. return 0;
  140. }