Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. GRAPH
  2.  
  3. //MAIN
  4. #include"Graph.h"
  5. template<class VertexType>
  6. void DFS(GraphType<VertexType>&graph, VertexType startVertex, VertexType endVertex );
  7. template<class VertexType>
  8. void BFS(GraphType<VertexType>&graph, VertexType startVertex, VertexType endVertex );
  9.  
  10. int main()
  11. {
  12.  
  13. GraphType<string> it(15);
  14.  
  15. cout << (it.IsEmpty()? "True" : "False") << endl;
  16.  
  17.  
  18.  
  19. string x,y;
  20. string a = "A";
  21. it.AddVertex(a);
  22. a = "B";
  23. it.AddVertex(a);
  24. a ="C";
  25. it.AddVertex(a);
  26. a = "D";
  27. it.AddVertex(a);
  28. a = "E";
  29. it.AddVertex(a);
  30. a = "F";
  31. it.AddVertex(a);
  32. a = "G";
  33. it.AddVertex(a);
  34. a = "H";
  35. it.AddVertex(a);
  36. a = "I";
  37. it.AddVertex(a);
  38. a = "J";
  39. it.AddVertex(a);
  40.  
  41.  
  42. cout << (it.IsEmpty()? "True" : "False") << endl;
  43. cout << (it.IsFull()? "True" : "False") << endl;
  44.  
  45.  
  46.  
  47. it.AddEdge("A","B",1);
  48. it.AddEdge("B","C",2);
  49. it.AddEdge("C","D",3);
  50. it.AddEdge("B","D",1);
  51. it.AddEdge("D","C",5);
  52. it.AddEdge("A","E",7);
  53. it.AddEdge("E","D",3);
  54. it.AddEdge("D","F",10);
  55. it.AddEdge("B","G",9);
  56. it.AddEdge("G","J",13);
  57. it.AddEdge("J","I",6);
  58. it.AddEdge("H","F",7);
  59. it.AddEdge("H","E",5);
  60.  
  61.  
  62. x = "G";
  63. y = "I";
  64. BFS(it,x,y);
  65.  
  66. x = "E";
  67. y = "F";
  68. BFS(it,x,y);
  69.  
  70. x = "A";
  71. y = "H";
  72. BFS(it,x,y);
  73.  
  74. x = "H";
  75. y = "B";
  76. BFS(it,x,y);
  77.  
  78. x = "B";
  79. y = "H";
  80. BFS(it,x,y);
  81.  
  82. x = "F";
  83. y = "C";
  84. BFS(it,x,y);
  85.  
  86. cout << endl;
  87.  
  88. it.AddEdge("F","H",3);
  89.  
  90.  
  91.  
  92. x = "A";
  93. y = "H";
  94. DFS(it,x,y);
  95.  
  96. x = "H";
  97. y = "B";
  98. DFS(it,x,y);
  99.  
  100. x = "B";
  101. y = "H";
  102. DFS(it,x,y);
  103.  
  104. x = "F";
  105. y = "C";
  106. DFS(it,x,y);
  107.  
  108.  
  109.  
  110. return 0;
  111. }
  112. template<class VertexType>
  113. void DFS(GraphType<VertexType>&graph, VertexType startVertex, VertexType endVertex )
  114. {
  115. stack<VertexType> stack;
  116. queue<VertexType> vertexQ;
  117. bool found = false;
  118.  
  119. VertexType vertex;
  120. VertexType item;
  121.  
  122. graph.ClearMarks();
  123.  
  124. stack.push(startVertex);
  125.  
  126. do {
  127. vertex = stack.top();
  128. stack.pop();
  129. if(vertex == endVertex){
  130. found = true;
  131. }
  132.  
  133. else if(!graph.IsMarked(vertex)) {
  134. graph.MarkVertex(vertex);
  135. graph.GetToVertices(vertex, vertexQ);
  136.  
  137. while(!vertexQ.empty()) {
  138. item = vertexQ.front();
  139. vertexQ.pop();
  140. if(!graph.IsMarked(item))
  141. stack.push(item);
  142. }
  143. }
  144.  
  145. }while(!stack.empty() && !found);
  146.  
  147. if(!found)
  148. cout << "0 " ;
  149. else
  150. cout << "1 " ;
  151. }
  152.  
  153.  
  154. template<class VertexType>
  155. void BFS(GraphType<VertexType>&graph, VertexType startVertex, VertexType endVertex )
  156. {
  157. queue<VertexType> queueQ;
  158. queue<VertexType> vertexQ;
  159. bool found = false;
  160.  
  161. VertexType vertex;
  162. VertexType item;
  163. graph.ClearMarks();
  164.  
  165. queueQ.push(startVertex);
  166.  
  167. do
  168. {
  169. vertex=queueQ.front();
  170. queueQ.pop();
  171.  
  172. if(vertex == endVertex){
  173. found = true;
  174. }
  175. else if(!graph.IsMarked(vertex)) {
  176. graph.MarkVertex(vertex);
  177. graph.GetToVertices(vertex, vertexQ);
  178.  
  179. while(!vertexQ.empty()) {
  180. item = vertexQ.front();
  181. vertexQ.pop();
  182. if(!graph.IsMarked(item))
  183. queueQ.push(item);
  184. }
  185. }
  186. }while(!queueQ.empty() && !found);
  187.  
  188. if(!found)
  189. cout << "0 " ;
  190. else
  191. cout << "1 " ;
  192.  
  193. }
  194.  
  195. //IMPLEMENTATION
  196. #ifndef GRAPH_CPP
  197. #define GRAPH_CPP
  198. #include"Graph.h"
  199.  
  200. template<class VertexType>
  201. GraphType<VertexType>::GraphType(int maxV)
  202. {
  203. numVertices = 0;
  204. maxVertices = maxV;
  205.  
  206. vertices = new VertexType[maxV];
  207.  
  208. edges = new int*[maxV];
  209. for(int i = 0; i < maxV; i++)
  210. edges[i] = new int[maxV];
  211.  
  212. marks = new bool[maxV];
  213. }
  214.  
  215.  
  216. template<class VertexType>
  217. GraphType<VertexType>::~GraphType()
  218. {
  219. delete [] vertices;
  220.  
  221. for(int i = 0; i < maxVertices; i++)
  222. delete [] edges[i];
  223. delete [] edges;
  224.  
  225. delete [] marks;
  226. }
  227.  
  228.  
  229. template<class VertexType>
  230. int GraphType<VertexType>:: IndexIs(VertexType vertex) const
  231. {
  232. for(int i=0;i<numVertices;i++)
  233. if(vertices[i]==vertex)
  234. return i;
  235. return -1;
  236. }
  237.  
  238.  
  239. template<class VertexType>
  240. bool GraphType<VertexType> :: IsMarked(VertexType vertex) const
  241. {
  242. int ind = IndexIs(vertex);
  243. return marks[ind];
  244. }
  245.  
  246.  
  247. template<class VertexType>
  248. void GraphType<VertexType> :: ClearMarks()
  249. {
  250. for(int i = 0; i < numVertices; i++)
  251. marks[i] = false;
  252. }
  253.  
  254.  
  255. template<class VertexType>
  256. void GraphType<VertexType> :: MarkVertex(VertexType vertex)
  257. {
  258. int ind = IndexIs(vertex);
  259. marks[ind] = true;
  260. }
  261.  
  262.  
  263. template<class VertexType>
  264. bool GraphType<VertexType> :: IsEmpty() const
  265. {
  266. return (numVertices==0);
  267. }
  268.  
  269.  
  270. template<class VertexType>
  271. bool GraphType<VertexType> :: IsFull() const
  272. {
  273. return (numVertices==maxVertices);
  274. }
  275.  
  276.  
  277. template<class VertexType>
  278. void GraphType<VertexType>::AddVertex(VertexType vertex)
  279. {
  280. vertices[numVertices] = vertex;
  281.  
  282. for(int index = 0; index < numVertices; index++) {
  283. edges[numVertices][index] = NULL_EDGE;
  284. edges[index][numVertices] = NULL_EDGE;
  285. }
  286.  
  287.  
  288. numVertices++;
  289. }
  290.  
  291.  
  292. template<class VertexType>
  293. void GraphType<VertexType>::AddEdge(VertexType fromVertex, VertexType toVertex, int weight)
  294. {
  295. int row;
  296. int col;
  297.  
  298. row = IndexIs(fromVertex);
  299. col = IndexIs(toVertex);
  300. edges[row][col] = weight;
  301. }
  302.  
  303.  
  304. template<class VertexType>
  305. int GraphType<VertexType>::WeightIs(VertexType fromVertex, VertexType toVertex)
  306. {
  307. int row;
  308. int col;
  309.  
  310. row = IndexIs(fromVertex);
  311. col = IndexIs(toVertex);
  312. return edges[row][col];
  313. }
  314.  
  315.  
  316. template<class VertexType>
  317. void GraphType<VertexType>::GetToVertices(VertexType vertex,queue<VertexType>& adjvertexQ)
  318. {
  319. int fromIndex;
  320. int toIndex;
  321.  
  322. fromIndex = IndexIs(vertex);
  323. for(toIndex = 0; toIndex < numVertices; toIndex++)
  324. if(edges[fromIndex][toIndex] != NULL_EDGE)
  325. adjvertexQ.push(vertices[toIndex]);
  326. }
  327. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement