Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. struct Vertex{
  5. int val;
  6. Vertex *next;
  7. };
  8.  
  9. struct Graph{
  10. Vertex **E;
  11. bool *visited;
  12. int size;
  13. Graph(){};
  14. Graph(Vertex **E, bool *visited, int size){
  15. this -> E = E;
  16. this -> visited = visited;
  17. this -> size = size;
  18. }
  19. };
  20.  
  21.  
  22. Graph *initializeUndirectedGraph();
  23. void reinitializeGraph(Graph *G);
  24. void printList(Graph *G);
  25. void BFSlist(Graph *G, int s);
  26.  
  27. void DFSlist_UTIL(Graph *G, int s);
  28. void DFSlist(Graph *G, int s);
  29.  
  30. int main() {
  31. Graph *G = initializeUndirectedGraph();
  32. std::cout << "Give me the starting vertex for BFS: " << std::endl;
  33. int vertex;
  34. std::cin >> vertex;
  35. BFSlist(G,vertex);
  36. return 0;
  37. }
  38.  
  39. void BFSlist(Graph *G, int s)
  40. {
  41. int *distance = new int[G -> size], *predecessor = new int[G -> size];
  42. for(int i = 0; i < G -> size; i++){
  43. G -> visited[i] = false;
  44. distance[i] = INT32_MAX;
  45. predecessor[i] = -1;
  46. }
  47. std::queue<int> q;
  48. q.push(s);
  49. distance[s] = 0;
  50. predecessor[s] = -1;
  51.  
  52. int current;
  53. std::cout << "BFS list - starting point at: " << s << std::endl;
  54. while(!q.empty()){
  55. current = q.front();
  56. q.pop();
  57. std::cout << current << " ";
  58. G -> visited[current] = true;
  59. for(Vertex *tmp = G -> E[current]; tmp; tmp = tmp -> next){
  60. if(!G -> visited[tmp -> val]){
  61. distance[tmp -> val] = distance[current] + 1;
  62. predecessor[tmp -> val] = current;
  63. q.push(tmp -> val);
  64. }
  65. }
  66. }
  67. std::cout << std::endl;
  68. for(int i = 0; i < G -> size; i++)
  69. std::cout << "Index: " << i << ", distance: " << distance[i] << ", predecessor: " << predecessor[i] << std::endl;
  70. }
  71.  
  72. void DFSlist(Graph *G, int s)
  73. {
  74. for(int i = 0; i < G -> size; i++)
  75. G -> visited[i] = false;
  76. std::cout << "DFS list - starting point at: " << s << std::endl;
  77. for(int i = 0; i < G -> size; i++)
  78. if(!G -> visited[i])
  79. DFSlist_UTIL(G,i);
  80. std::cout << std::endl;
  81.  
  82. }
  83.  
  84. void DFSlist_UTIL(Graph *G, int s)
  85. {
  86. G -> visited[s] = true;
  87. std::cout << s << " ";
  88. for(Vertex *tmp = G -> E[s]; tmp; tmp = tmp -> next)
  89. if(!G -> visited[tmp -> val])
  90. DFSlist_UTIL(G,tmp -> val);
  91. }
  92.  
  93. void reinitializeGraph(Graph *G)
  94. {
  95. bool *B = new bool[G -> size];
  96. for(int i = 0; i < G -> size; i++){
  97. B[i] = false;
  98. }
  99. G -> visited = B;
  100. }
  101.  
  102. Graph *initializeDirectedGraph()
  103. {
  104. int vertices, edges;
  105. std::cout << "Give me the number of vertices and edges in your graph: " << std::endl;
  106. std::cin >> vertices >> edges;
  107. Vertex **A = new Vertex *[vertices];
  108. bool *B = new bool[vertices];
  109.  
  110. for(int i = 0; i < vertices; i++) {
  111. A[i] = nullptr;
  112. B[i] = false;
  113. }
  114.  
  115. std::cout << "Give me starting and ending vertex for every edge: " << std::endl;
  116. int v1, v2;
  117. for(int i = 0; i < edges; i++) {
  118. std::cin >> v1 >> v2;
  119. Vertex *vertex = new Vertex;
  120. vertex -> val = v2;
  121. vertex -> next = A[v1];
  122. A[v1] = vertex;
  123. }
  124.  
  125. Graph *F = new Graph;
  126. F -> E = A;
  127. F -> visited = B;
  128. F -> size = vertices;
  129.  
  130. return F;
  131. }
  132.  
  133. Graph *initializeUndirectedGraph()
  134. {
  135. int vertices, edges;
  136. std::cout << "Give me the number of vertices and edges in your graph: " << std::endl;
  137. std::cin >> vertices >> edges;
  138. Vertex **A = new Vertex *[vertices];
  139. bool *B = new bool[vertices];
  140.  
  141. for(int i = 0; i < vertices; i++) {
  142. A[i] = nullptr;
  143. B[i] = false;
  144. }
  145.  
  146. std::cout << "Give me starting and ending vertex for every edge: " << std::endl;
  147. int v1, v2;
  148. for(int i = 0; i < edges; i++) {
  149. std::cin >> v1 >> v2;
  150.  
  151. Vertex *vertex_1 = new Vertex;
  152. vertex_1 -> val = v2;
  153. vertex_1 -> next = A[v1];
  154. A[v1] = vertex_1;
  155.  
  156. Vertex *vertex_2 = new Vertex;
  157. vertex_2 -> val = v1;
  158. vertex_2 -> next = A[v2];
  159. A[v2] = vertex_2;
  160. }
  161.  
  162. Graph *F = new Graph;
  163. F -> E = A;
  164. F -> visited = B;
  165. F -> size = vertices;
  166.  
  167. return F;
  168. }
  169.  
  170.  
  171. void printList(Graph *G)
  172. {
  173. std::cout << "Adjacency list: " << std::endl;
  174. Vertex *tmp;
  175. for(int i = 0; i < G -> size; i++){
  176. tmp = G -> E[i];
  177. std::cout << i <<": ";
  178.  
  179. while(tmp){
  180. std::cout << tmp -> val << " ";
  181. tmp = tmp -> next;
  182. }
  183. std::cout << std::endl;
  184. }
  185. std::cout << std::endl;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement