Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <set>
  6. #include <vector>
  7. #include <stdio.h>
  8. #include <queue>
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14. class Graf{
  15. private:
  16. int** adjacencyMatrix;
  17. int vertexCount;
  18. int matrixsize;
  19. set <int> M;
  20.  
  21.  
  22. void addEdge(int start_node, int end_node) {
  23. if(start_node >= 0 && start_node < matrixsize && end_node > 0 && end_node < matrixsize) {
  24. adjacencyMatrix[start_node][end_node] = 1;
  25.  
  26. }
  27.  
  28. //prida orientovanu hranu zo start do end
  29. }
  30.  
  31. void removeEdge(int start_node, int end_node) {
  32. if(start_node >= 0 && start_node < matrixsize && end_node > 0 && end_node < matrixsize) {
  33. adjacencyMatrix[start_node][end_node] = 0;
  34.  
  35. }
  36. //odstrani orientovanu hranu zo start do end
  37. }
  38.  
  39.  
  40.  
  41. void initGraph(char* filename){
  42. std::ifstream file(filename);
  43. int numberedges = 0;
  44. int pom = 0;
  45. unsigned int x,y;
  46. set<int> all_vertices;
  47.  
  48. while (file >> x >> y)
  49. {
  50. all_vertices.insert(x);
  51. all_vertices.insert(y);
  52. numberedges++;
  53. if (pom < x)
  54. pom = x;
  55. if (pom < y)
  56. pom = y;
  57. }
  58.  
  59.  
  60. this->vertexCount = all_vertices.size();
  61. set<int>::iterator it = all_vertices.begin();
  62. for (int i=0; i<4;i++)
  63. {
  64. M.insert(*it);
  65. it++;
  66. }
  67. this->matrixsize = pom;
  68. adjacencyMatrix = new int*[matrixsize];
  69. for(int i = 0; i < matrixsize; i++) {
  70. adjacencyMatrix[i] = new int[matrixsize];
  71. for(int j = 0; j < matrixsize; j++)
  72. adjacencyMatrix[i][j] = 0;
  73. }
  74.  
  75. //zisti pocet uzlov/hran a inicializuje maticu
  76.  
  77. cout << "Pocet vrcholov : " << this->vertexCount << endl;
  78. cout << "Pocet hran: " << numberedges << endl;
  79. }
  80. public:
  81.  
  82. Graf(int vertexCount = -1){
  83. this->vertexCount = vertexCount;
  84. }
  85.  
  86. void loadFile(char* filename)
  87. {
  88. this->initGraph(filename);
  89.  
  90. std::ifstream file(filename);
  91. std::string line;
  92. unsigned int from, to;
  93.  
  94. while (!file.eof())
  95. {
  96. file >> from >> to;
  97. addEdge(from,to);
  98. }
  99.  
  100.  
  101. }
  102.  
  103. void print()
  104. {
  105. for (int i = 0; i<matrixsize;i++)
  106. for (int j = 0; j< matrixsize; j++)
  107. cout << adjacencyMatrix [i][j];
  108. }
  109.  
  110. bool cesta(int V1, int V2)
  111. {
  112.  
  113. set <int> prejdene;
  114. queue <int> q;
  115. q.push(V2);
  116.  
  117. while (!q.empty())
  118. {
  119. V2 = q.front();
  120. for (int i=0;i<this->matrixsize;i++)
  121. {
  122. if (adjacencyMatrix[i][V2]>0)
  123. if (prejdene.find(i)==prejdene.end())
  124. q.push(i);
  125. }
  126. prejdene.insert(V2);
  127. q.pop();
  128. }
  129. if (prejdene.find(V1)==prejdene.end())
  130. return false;
  131. else return true;
  132.  
  133. }
  134.  
  135. int dosiahnutelnost()
  136. {
  137. set <int> prejdene;
  138. queue <int> q;
  139. int pom;
  140. for (set<int>::iterator it = this->M.begin(); it != M.end();it++)
  141. {
  142. q.push(*it);
  143.  
  144. while (!q.empty())
  145. {
  146. pom = q.front();
  147. for (int i=0;i<this->matrixsize;i++)
  148. {
  149. if (adjacencyMatrix[pom][i]>0)
  150. if (prejdene.find(i)==prejdene.end())
  151. q.push(i);
  152. }
  153. prejdene.insert(pom);
  154. q.pop();
  155. }
  156.  
  157. }
  158. return prejdene.size();
  159.  
  160. }
  161. };
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. int main()
  169. {
  170. Graf graf(-1);
  171. graf.loadFile("Graf.txt");
  172.  
  173. cout << graf.cesta(0,56) << endl;
  174. cout << graf.cesta(2,755) << endl;
  175.  
  176. cout << graf.dosiahnutelnost() << endl;
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement