Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. /****************************************************************
  2. BFS - adjacency matrix
  3. ****************************************************************/
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <ctime>
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12. /****************************************************************
  13. Class Queue represent a Queue data structure which is First In
  14. First Out [FIFO] structured. It has operations like Enqueue which
  15. adds an element at the rear side and Dequeue which removes the
  16. element from front.
  17. *****************************************************************/
  18.  
  19. struct node {
  20. int info;
  21. node *next;
  22. };
  23.  
  24. class Queue {
  25. private:
  26. node *front;
  27. node *rear;
  28. public:
  29. Queue();
  30. ~Queue();
  31. bool isEmpty();
  32. void enqueue(int);
  33. int dequeue();
  34. void display();
  35.  
  36. };
  37.  
  38. void Queue::display(){
  39. node *p = new node;
  40. p = front;
  41. if(front == NULL){
  42. cout<<"\nNothing to Display\n";
  43. }else{
  44. while(p!=NULL){
  45. cout<<endl<<p->info;
  46. p = p->next;
  47. }
  48. }
  49. }
  50.  
  51. Queue::Queue() {
  52. front = NULL;
  53. rear = NULL;
  54. }
  55.  
  56. Queue::~Queue() {
  57. delete front;
  58. }
  59.  
  60. void Queue::enqueue(int data) {
  61. node *temp = new node();
  62. temp->info = data;
  63. temp->next = NULL;
  64. if(front == NULL){
  65. front = temp;
  66. }else{
  67. rear->next = temp;
  68. }
  69. rear = temp;
  70. }
  71.  
  72. int Queue::dequeue() {
  73. node *temp = new node();
  74. int value;
  75. if(front == NULL){
  76. cout<<"\nQueue is Emtpty\n";
  77. }else{
  78. temp = front;
  79. value = temp->info;
  80. front = front->next;
  81. delete temp;
  82. }
  83. return value;
  84. }
  85.  
  86. bool Queue::isEmpty() {
  87. return (front == NULL);
  88. }
  89.  
  90.  
  91. /************************************************************
  92. Class Graph represents a Graph [V,E] having vertices V and
  93. edges E.
  94. ************************************************************/
  95. class Graph {
  96. private:
  97. int n; /// n is the number of vertices in the graph
  98. int **A; /// A stores the edges between two vertices
  99. public:
  100. Graph(int size = 2);
  101. ~Graph();
  102. bool isConnected(int, int);
  103. void addEdge(int u, int v);
  104. void BFS(int );
  105. void afis(){
  106. for (int i = 0; i < n; ++i)
  107. {
  108. cout<<endl;
  109. for (int j = 0; j < n; ++j)
  110. cout<<A[i][j]<<" ";
  111. }
  112. }
  113. };
  114.  
  115. Graph::Graph(int size) {
  116. int i, j;
  117. if (size < 2) n = 2;
  118. else n = size;
  119. A = new int*[n];
  120. for (i = 0; i < n; ++i)
  121. A[i] = new int[n];
  122. for (i = 0; i < n; ++i)
  123. for (j = 0; j < n; ++j)
  124. A[i][j] = 0;
  125. }
  126.  
  127. Graph::~Graph() {
  128. for (int i = 0; i < n; ++i)
  129. delete [] A[i];
  130. delete [] A;
  131. }
  132.  
  133.  
  134. /******************************************************
  135. Checks if two given vertices are connected by an edge
  136. @param u vertex
  137. @param v vertex
  138. @return true if connected false if not connected
  139. ******************************************************/
  140. bool Graph::isConnected(int u, int v) {
  141. return (A[u-1][v-1] == 1);
  142. }
  143.  
  144. /*****************************************************
  145. adds an edge E to the graph G.
  146. @param u vertex
  147. @param v vertex
  148. *****************************************************/
  149. void Graph::addEdge(int u, int v) {
  150. A[u-1][v-1] = A[v-1][u-1] = 1;
  151. }
  152.  
  153. /*****************************************************
  154. performs Breadth First Search
  155. @param s initial vertex
  156. *****************************************************/
  157. void Graph::BFS(int s) {
  158. Queue Q;
  159.  
  160. /** Keeps track of explored vertices */
  161. bool *explored = new bool[n+1];
  162.  
  163. /** Initailized all vertices as unexplored */
  164. for (int i = 1; i <= n; ++i)
  165. explored[i] = false;
  166.  
  167. /** Push initial vertex to the queue */
  168. Q.enqueue(s);
  169. explored[s] = true; /** mark it as explored */
  170. cout << "Breadth first Search starting from vertex ";
  171. cout << s << " : " << endl;
  172.  
  173. /** Unless the queue is empty */
  174. while (!Q.isEmpty()) {
  175. /** Pop the vertex from the queue */
  176. int v = Q.dequeue();
  177.  
  178. /** display the explored vertices */
  179. cout << v << " ";
  180.  
  181. /** From the explored vertex v try to explore all the
  182. connected vertices */
  183. for (int w = 1; w <= n; ++w)
  184.  
  185. /** Explores the vertex w if it is connected to v
  186. and and if it is unexplored */
  187. if (isConnected(v, w) && !explored[w]) {
  188. /** adds the vertex w to the queue */
  189. Q.enqueue(w);
  190. /** marks the vertex w as visited */
  191. explored[w] = true;
  192. }
  193. }
  194. cout << endl;
  195. delete [] explored;
  196. }
  197.  
  198. int main() {
  199. /** Creates a graph with 12 vertices */
  200. Graph g(12);
  201.  
  202. /** Adds edges to the graph */
  203. g.addEdge(1, 2); g.addEdge(1, 3);
  204. g.addEdge(2, 4); g.addEdge(3, 4);
  205. g.addEdge(3, 6); g.addEdge(4 ,7);
  206. g.addEdge(5, 6); g.addEdge(5, 7);
  207. clock_t t1;
  208. t1 = clock();
  209.  
  210. /** Explores all vertices findable from vertex 1 */
  211. g.BFS(1);
  212. float diff = (double)(clock() - t1)/CLOCKS_PER_SEC;
  213. cout <<endl<< "The time taken for Breadth first search: "<< diff << endl;
  214.  
  215. g.afis();
  216. system("pause");
  217. return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement