Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.72 KB | None | 0 0
  1. // GraphTask.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include "pch.h"
  5. #define _CRT_SECURE_NO_DEPRECATE
  6. #include <fstream>
  7. #include <vector>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <set>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. struct ThreeVertices
  16. {
  17. int first, second, third;
  18. ThreeVertices(int x, int y, int z)
  19. {
  20. first = x;
  21. second = y;
  22. third = z;
  23. }
  24. };
  25.  
  26. class BipartiteGraph
  27. {
  28. private:
  29. int verticesCount;
  30. int secondSetStart;
  31. vector<set<int>> edges;
  32.  
  33. BipartiteGraph(vector<set<int>> _edges, int _verticesCount, int _secondSetStart)
  34. {
  35. edges = _edges;
  36. verticesCount = _verticesCount;
  37. secondSetStart = _secondSetStart;
  38. }
  39.  
  40. public:
  41. BipartiteGraph(int _verticesCount, int _secondSetStart)
  42. {
  43. edges = vector<set<int>>(_verticesCount);
  44. verticesCount = _verticesCount;
  45. secondSetStart = _secondSetStart;
  46. }
  47.  
  48. int GetVerticesSize()
  49. {
  50. return verticesCount;
  51. }
  52.  
  53. int GetTheSecondSetStart()
  54. {
  55. return secondSetStart;
  56. }
  57.  
  58. int GetEdgeCountForOneVertex(int vertexNumber)
  59. {
  60. return edges[vertexNumber].size();
  61. }
  62.  
  63. vector <set<int>> GetEges()
  64. {
  65. return edges;
  66. }
  67.  
  68. BipartiteGraph Copy()
  69. {
  70. return BipartiteGraph(edges, verticesCount, secondSetStart);
  71. }
  72.  
  73. bool ContainsEdge(const int firstVertex, int secondVertex)
  74. {
  75. return edges[firstVertex].find(secondVertex) != edges[firstVertex].end();;
  76. }
  77.  
  78. void AddEdge(int from, int to)
  79. {
  80. // add edge from 'from' to 'to'
  81. edges[from].insert(to);
  82. edges[to].insert(from);
  83. }
  84.  
  85. void RemoveEdge(int from, int to)
  86. {
  87. // remove edge from 'from' to 'to'
  88. /*auto itemTo = find(edges[from].begin(), edges[from].end(), to);
  89. if (itemTo != edges[from].end())
  90. {
  91. edges[from].erase(itemTo);
  92. }
  93. auto itemFor = find(edges[from].begin(), edges[from].end(), to);
  94. if (itemFor != edges[from].end())
  95. {
  96. edges[from].erase(itemFor);
  97. }*/
  98.  
  99. edges[from].erase(to);
  100. edges[to].erase(from);
  101. }
  102.  
  103. void Print()
  104. {
  105. cout << "Vertices " << verticesCount << " " << "Second Start " << secondSetStart << endl;
  106. for (int i = 0; i < secondSetStart; ++i)
  107. {
  108. std::set<int>::iterator it;
  109. for (it = edges[i].begin(); it != edges[i].end(); ++it)
  110. {
  111. cout << "From " << i << " " << "To " << *it << endl;
  112. }
  113.  
  114. }
  115. }
  116.  
  117. int GetSecondSetStart()
  118. {
  119. return secondSetStart;
  120. }
  121.  
  122. int GetVertexDegree(int vertexIndex)
  123. {
  124. return edges[vertexIndex].size();
  125. }
  126.  
  127. int GetEdgeSetSize()
  128. {
  129. return edges.size();
  130. }
  131.  
  132. void FindIncreasingVertices(vector<ThreeVertices>& increasingVertices)
  133. {
  134. for (int i = 0; i < edges.size(); ++i)
  135. {
  136. std::set<int>::iterator it;
  137. for (it = edges[i].begin(); it != edges[i].end(); ++it)
  138. {
  139. if (i > *it)
  140. {
  141. continue;
  142. }
  143. int firstSetVertex = i;
  144. int secondSetVertex = *it;
  145.  
  146. for (int j = 0; j < secondSetStart; ++j)
  147. {
  148. if (firstSetVertex == j)
  149. {
  150. continue;
  151. }
  152.  
  153. if (!ContainsEdge(secondSetVertex, j))
  154. {
  155. if (GetVertexDegree(firstSetVertex) <= GetVertexDegree(j))
  156. {
  157. increasingVertices.push_back(ThreeVertices(firstSetVertex, secondSetVertex, j));
  158. }
  159. }
  160. }
  161.  
  162. for (int e = secondSetStart; e < verticesCount; ++e)
  163. {
  164. if (e == secondSetVertex)
  165. {
  166. continue;
  167. }
  168.  
  169. if (!ContainsEdge(e, firstSetVertex))
  170. {
  171. if (GetVertexDegree(secondSetVertex) <= GetVertexDegree(e))
  172. {
  173. increasingVertices.push_back(ThreeVertices(secondSetVertex, firstSetVertex, e));
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180.  
  181. };
  182.  
  183. void ApplyRotation(BipartiteGraph & graph, ThreeVertices vertices)
  184. {
  185. // удалить одно ребро и добавить другое
  186. graph.RemoveEdge(vertices.first, vertices.second);
  187. graph.AddEdge(vertices.second, vertices.third);
  188. }
  189.  
  190. void RollbackRotation(BipartiteGraph & graph, ThreeVertices vertices)
  191. {
  192. // удалить одно ребро и добавить другое
  193. graph.RemoveEdge(vertices.second, vertices.third);
  194. graph.AddEdge(vertices.first, vertices.second);
  195. }
  196.  
  197.  
  198. void GenerateAllGraphs(BipartiteGraph& graph, vector<BipartiteGraph> & resultGraphs, int level)
  199. {
  200. vector<ThreeVertices> increasingVertices;
  201. graph.FindIncreasingVertices(increasingVertices);
  202.  
  203. if (increasingVertices.empty())
  204. {
  205. auto graphCopy = graph.Copy();
  206. resultGraphs.push_back(graphCopy);
  207. return;
  208. }
  209.  
  210. if (level == 0) {
  211. printf("Found increasing triples: %d\n", static_cast<int>(increasingVertices.size()));
  212. }
  213.  
  214. for (int x = 0; x < increasingVertices.size(); ++x)
  215. {
  216. if (level == 0) {
  217. printf("%d\n", x);
  218. }
  219. ApplyRotation(graph, increasingVertices[x]);
  220. GenerateAllGraphs(graph, resultGraphs, level + 1);
  221. RollbackRotation(graph, increasingVertices[x]);
  222. }
  223. }
  224.  
  225. bool AreGraphsEqual(BipartiteGraph & firstGraph, BipartiteGraph & secondGraph)
  226. {
  227. int edgeSetSize = firstGraph.GetEdgeSetSize();
  228.  
  229. vector<int> firstGraphDegrees(edgeSetSize);
  230. vector<int> secondGraphDegrees(edgeSetSize);
  231.  
  232. for (int j = 0; j < edgeSetSize; ++j)
  233. {
  234. firstGraphDegrees[j] = firstGraph.GetVertexDegree(j);
  235. secondGraphDegrees[j] = secondGraph.GetVertexDegree(j);
  236. }
  237. sort(firstGraphDegrees.begin(), firstGraphDegrees.end());
  238. sort(secondGraphDegrees.begin(), secondGraphDegrees.end());
  239.  
  240. return equal(firstGraphDegrees.begin(), firstGraphDegrees.end(), secondGraphDegrees.begin());
  241. }
  242.  
  243. void SelectUniqueGraphs(vector<BipartiteGraph> & allGraphs, vector<BipartiteGraph> & uniqueGraphs)
  244. {
  245. for (int x = 0; x < allGraphs.size(); ++x)
  246. {
  247. bool flag = true;
  248. for (int y = x+1; y < allGraphs.size(); ++y)
  249. {
  250. if (AreGraphsEqual(allGraphs[x], allGraphs[y]))
  251. {
  252. flag = false;
  253. break;
  254. }
  255.  
  256. }
  257. if (flag)
  258. {
  259. uniqueGraphs.push_back(allGraphs[x]);
  260. }
  261. }
  262. }
  263.  
  264. BipartiteGraph ReadGraph(string fileName)
  265. {
  266. ifstream myfile;
  267. myfile.open(fileName);
  268.  
  269. int verticesCount, edgesCount;
  270. myfile >> verticesCount >> edgesCount;
  271.  
  272. vector<set<int>> edgesPaths(edgesCount);
  273. int secondSetStart;
  274. myfile >> secondSetStart;
  275.  
  276. BipartiteGraph readGraph = BipartiteGraph(verticesCount, secondSetStart);
  277.  
  278. for (int i = 0; i < edgesCount; ++i)
  279. {
  280. int to, from;
  281. myfile >> from;
  282. myfile >> to;
  283.  
  284. readGraph.AddEdge(from, to);
  285. }
  286.  
  287. return readGraph;
  288. }
  289.  
  290. void PrintResult(vector<BipartiteGraph> & resultGraphs)
  291. {
  292. for (int i = 0; i < resultGraphs.size(); ++i)
  293. {
  294. resultGraphs[i].Print();
  295. }
  296. }
  297.  
  298. BipartiteGraph GenerateGraph(double edgeProbability, int verticesCount)
  299. {
  300. int secondSetStart = verticesCount / 2;
  301.  
  302. BipartiteGraph generatedGraph = BipartiteGraph(verticesCount, secondSetStart);
  303.  
  304. for (int i = 0; i < secondSetStart; ++i)
  305. {
  306. for (int j = secondSetStart; j < verticesCount; ++j)
  307. {
  308. double randomNumb = (double)rand() / RAND_MAX;
  309. if (randomNumb <= edgeProbability)
  310. {
  311. generatedGraph.AddEdge(i, j);
  312. }
  313. }
  314.  
  315. }
  316. return generatedGraph;
  317. }
  318.  
  319. void WriteGraphToFile(BipartiteGraph graph) {
  320.  
  321. FILE * fp = fopen("graph_description", "w");
  322.  
  323. int verticesCount = graph.GetVerticesSize();
  324.  
  325. fprintf(fp, "%d\n", verticesCount);
  326.  
  327. for (int i = 0; i < verticesCount; ++i)
  328. {
  329. int edgeCount = graph.GetEdgeCountForOneVertex(i);
  330. fprintf(fp, "%d ", edgeCount); //для i вершины записываем кол-во вершин, с кот она соединена ребром
  331.  
  332. vector <set<int>> edges = graph.GetEges();
  333. std::set<int>::iterator it;
  334.  
  335. for (it = edges[i].begin(); it != edges[i].end(); ++it)
  336. {
  337. fprintf(fp, "%d ", *it); // запись самих вершин
  338. }
  339.  
  340. fprintf(fp, "\n");
  341. }
  342.  
  343. int secondSetStart = graph.GetTheSecondSetStart();
  344. fprintf(fp, "%d", secondSetStart);
  345.  
  346. fclose(fp);
  347. }
  348.  
  349. int main()
  350. {
  351. const int MAX_VERTICES_COUNT = 20 * 1000;
  352.  
  353. double edgeProbability;
  354. printf("Enter edge probability: \n");
  355. scanf("%lf", &edgeProbability);
  356.  
  357. int verticesCount;
  358. printf("Enter vertices count: \n");
  359. scanf("%d", &verticesCount);
  360.  
  361. if (verticesCount < 1 || verticesCount > MAX_VERTICES_COUNT) {
  362. printf("Vertices count must be in interval [1; %d]\n", MAX_VERTICES_COUNT);
  363. return 0;
  364. }
  365.  
  366. if (edgeProbability < 0 || edgeProbability > 1) {
  367. printf("Edge probability must be in interval [0; 1]");
  368. return 0;
  369. }
  370.  
  371. BipartiteGraph generatedGraph = GenerateGraph(edgeProbability, verticesCount);
  372. WriteGraphToFile(generatedGraph);
  373.  
  374.  
  375. //string fileName = "in.txt";
  376. //auto generatedGraph = ReadGraph(fileName);
  377.  
  378.  
  379. vector<BipartiteGraph> resultGraphs;
  380. GenerateAllGraphs(generatedGraph, resultGraphs, 0);
  381.  
  382. vector<BipartiteGraph> uniqueGraphs;
  383. SelectUniqueGraphs(resultGraphs, uniqueGraphs);
  384.  
  385. PrintResult(uniqueGraphs);
  386. cout << uniqueGraphs.size();
  387.  
  388.  
  389.  
  390. return 0;
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement