Maxim_Leo

Untitled

May 10th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <algorithm>
  5. #include<vector>
  6. #include <windows.h>
  7.  
  8. #define _SCL_SECURE_NO_WARNINGS
  9. #include <boost/graph/graphviz.hpp>
  10. #include <boost/graph/adjacency_list.hpp>
  11. #include <boost/graph/iteration_macros.hpp>
  12. using namespace std;
  13.  
  14. vector<pair<string, string>> woodEdges;
  15. map<string, vector<string>> AdjacencyList;
  16. vector<pair<string, int>> steps;
  17. vector<vector<string>> cycles;
  18.  
  19.  
  20. void saveCycle(const string& start, const string& end) {
  21.  
  22. string tmp = end;
  23. // vector<vector<string>> cycles;
  24. cycles.push_back({});
  25.  
  26. while (tmp != start) {
  27. // {{}, {}, {} <- добавление temp в последний массив}
  28. cycles[cycles.size() - 1].push_back(tmp);
  29. // среди woodEdges найти pair<first, second>
  30. // tmp = woodEdges.find(edge => edge.first === temp).second;
  31. for (const auto& edge: woodEdges) {
  32. if (edge.second == tmp) {
  33. tmp = edge.first;
  34. break;
  35. }
  36. }
  37. }
  38.  
  39. // {{}, {}, {} <- добавление temp в последний массив}
  40. cycles[cycles.size() - 1].push_back(tmp);
  41. // {{}, {}, {} <- развернуть последний массив}
  42. std::reverse(cycles[cycles.size() - 1].begin(), cycles[cycles.size() - 1].end());
  43. // {{}, {}, {} <- вывод на экран последнего массива}
  44.  
  45. for (const auto& x: cycles[cycles.size() - 1]) { //Выводим цикл
  46. cout << x << " ";
  47. }
  48. cout << "Cycle saved" << endl<<endl;
  49. }
  50.  
  51. void FSC(const string& node, int depth) {
  52. steps.push_back(make_pair(node, depth));
  53. depth++;
  54. for (const auto& friendNode: AdjacencyList.at(node)) { // adjencyList.at(node): {"", "", ""} - вершины смежные с node
  55. cout << "Node|Friend " << node << " " << friendNode << endl<<endl;
  56. bool isFriendNodeInSteps = false; // if (!steps.find(step => step.node === friend)) {
  57. for (const auto& step: steps) {
  58. if (step.first == friendNode) {
  59. isFriendNodeInSteps = true;
  60. }
  61. }
  62.  
  63. if (!isFriendNodeInSteps) {
  64. cout << "Push to woodEdges " << node << " " << friendNode << endl<<endl;
  65. woodEdges.push_back(make_pair(node, friendNode));
  66. FSC(friendNode, depth);
  67. } else {
  68. for (const auto& x: woodEdges) { // else if (woodEdges.find(edge => edge.data.source === node && edge.data.target !== friend))
  69. if (x.first == node && x.second != friendNode) {
  70. cout << "Cycle found reverse edge of the original edge " << x.first << " " << x.second << endl<<endl;
  71. saveCycle(node, friendNode);
  72. }
  73. }
  74.  
  75. }
  76. }
  77. }
  78.  
  79.  
  80.  
  81. void FundamentalSystemofCycles() {
  82. cout << "FundamentalCutsBasis() run" << endl<<endl;
  83. int depth = 0;
  84. FSC(AdjacencyList.begin()->first, depth);
  85. ofstream fout("Cycles.txt");
  86.  
  87. // print cycles
  88. cout << "Cycles: " << endl<<endl;
  89. for (const auto& cycle: cycles) {
  90. fout << "graph{ ";
  91. for (const auto& x: cycle) {
  92. cout << x << " ";
  93. fout << x;
  94. if (x != cycle[cycle.size() - 1]) fout << "--";
  95. else fout <<"--"<<cycle[0];
  96. }
  97. cout << endl<<endl;
  98. fout << endl << "}" << endl<<endl;
  99. }
  100. fout.close();
  101. system("dot Cycles.txt -Tpng -OCycles.png");
  102. // print woodEdges
  103. cout << "WoodEdges" << endl<<endl;
  104. for (const auto& x: woodEdges) {
  105. cout << x.first << " " << x.second << endl<<endl;
  106. }
  107. cout << "Cyclomatic number of the graph G: " << cycles.size() << endl << endl;
  108. }
  109.  
  110. vector<pair<string, string>> createEdgesFromCycle(vector<string> cycle) { //делим цикл на ребра
  111. vector<pair<string, string>> edges;
  112. for (int i = 0; i < cycle.size(); i++) {
  113. edges.push_back(make_pair(cycle[i], cycle[(i + 1) % cycle.size()]));
  114. }
  115. return edges;
  116. }
  117.  
  118. void FundamentalCutsSystem() {
  119. cout << "FundamentalCutsSystem() run" << endl << endl;
  120. vector<vector<pair<string, string>>> edgedCycles;
  121.  
  122. for (const auto& cycle: cycles) {
  123. edgedCycles.push_back(createEdgesFromCycle(cycle)); //записываем в edgedCycles массив с ребрами цикла
  124. }
  125. vector<vector<pair<string, string>>> cuts;
  126.  
  127. for (const auto& edge: woodEdges) {
  128. cuts.push_back({edge});
  129. for (const auto& cycle: edgedCycles) {
  130. for (const auto& cycleEdge: cycle) {
  131. if (cycleEdge.first == edge.first && cycleEdge.second == edge.second) {
  132. cuts[cuts.size() - 1].push_back(cycle[cycle.size() - 1]);
  133. }
  134. }
  135. }
  136. }
  137. ofstream fout("Cuts.txt");
  138.  
  139. cout << "Cuts: " << endl<<endl;
  140. for (const auto& cut: cuts) { //вывод разрезов
  141. fout << "graph{ "<<endl;
  142. for(const auto& edge: cut) {
  143. cout << edge.first << " " << edge.second << endl;
  144. fout << edge.first << "--" << edge.second << endl;
  145. }
  146. cout << endl;
  147. fout <<endl<< "} "<<endl;
  148. }
  149. system("dot Cuts.txt -Tpng -OCuts.png");
  150. cout << "Number of cuts: " << cuts.size();
  151. }
  152.  
  153. int main() {
  154. // IEdje[] = vector<pair<string, string>>
  155. // map<string, vector<string>> adjencyList;
  156. setlocale(LC_ALL, "Russian");
  157. SetConsoleCP(1251);
  158. SetConsoleOutputCP(1251);
  159. AdjacencyList["1"] = {"2", "3", "4", "5"};
  160. AdjacencyList["2"] = {"1", "3", "4", "5"};
  161. AdjacencyList["3"] = {"1", "2", "4"};
  162. AdjacencyList["4"] = {"1", "2", "3"};
  163. AdjacencyList["5"] = {"1", "2"};
  164. ifstream in("input1.txt");
  165. in.close();
  166. system("dot input1.txt -Tpng -oInputGraph.png");
  167. FundamentalSystemofCycles();
  168.  
  169. FundamentalCutsSystem();
  170.  
  171. };
Advertisement
Add Comment
Please, Sign In to add comment