fork(5) download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class vertex;
  5. class Graph;
  6.  
  7. void BFS(Graph,int);
  8.  
  9.  
  10.  
  11. struct Edge{
  12. int E_num;
  13. vertex*first;
  14. vertex*second;
  15. Edge*enext;
  16. };
  17.  
  18. struct node{
  19. Edge*data; // node of edgelist inside a vertex
  20. node*next; // Here we can also store vertex numbers to get adjacent vertices quickly.
  21. };
  22.  
  23. class vertex{
  24. public:
  25. int V_num;
  26. node*E_in_front;
  27. node*E_in_rear;
  28. node*E_out_front;
  29. node*E_out_rear;
  30. vertex*vnext;
  31. int color;
  32. int label;
  33. int parent;
  34.  
  35.  
  36. vertex(){
  37. E_in_front = E_in_rear = NULL;
  38. E_out_front = E_out_rear = NULL;
  39. color = 0; // initially white.
  40. label = -1;
  41. parent = -1;
  42. cout<<"Prompt: vertex created"<<endl;
  43. }
  44.  
  45. void insert_node_vin(Edge*x)
  46. {
  47. node*temp = new node;
  48. temp->data = x;
  49. temp->next = NULL;
  50. if(E_in_front == NULL)
  51. {
  52. E_in_front = E_in_rear = temp;
  53. cout<<"pointer to Edge in vin inserted"<<endl;
  54. return;
  55. }
  56. E_in_rear->next = temp;
  57. E_in_rear = temp;
  58. cout<<"pointer to Edge in vin inserted"<<endl;
  59. }
  60.  
  61. void insert_node_vout(Edge*x)
  62. {
  63. node*temp = new node;
  64. temp->data = x;
  65. temp->next = NULL;
  66. if(E_out_front == NULL)
  67. {
  68. E_out_front = E_out_rear = temp;
  69. cout<<"pointer to Edge in vout inserted"<<endl;
  70. return;
  71. }
  72. E_out_rear->next = temp;
  73. E_out_rear = temp;
  74. cout<<"pointer to Edge in vin inserted"<<endl;
  75. }
  76.  
  77.  
  78.  
  79. };
  80.  
  81.  
  82.  
  83. class Graph{
  84.  
  85. public:
  86.  
  87. Edge*E_front;
  88. Edge*E_rear;
  89. vertex*V_front;
  90. vertex*V_rear;
  91.  
  92.  
  93. Graph(){
  94. E_front = E_rear = NULL;
  95. V_front = V_rear = NULL;
  96. cout<<"Graph is initialised"<<endl;
  97. }
  98.  
  99. void insert_edge(int v1, int v2, int num) // num = edge number input by user.
  100. {
  101. if(ispresent(v1))
  102. {
  103. if(ispresent(v2))
  104. { // just create an edge with starting vertex v1 and ending v2.
  105. Edge*temp = new Edge;
  106. temp->E_num = num;
  107. vertex*temp1 = V_front;
  108. vertex*temp2 = V_front;
  109. while(temp1 != NULL)
  110. {
  111. if(temp1->V_num == v1)
  112. break;
  113. temp1 = temp1->vnext;
  114. }
  115. while(temp2 != NULL)
  116. {
  117. if(temp2->V_num == v2)
  118. break;
  119. temp2 = temp2->vnext;
  120. }
  121. E_rear->enext = temp;
  122. temp->enext = NULL;
  123. E_rear = temp;
  124. temp1->insert_node_vout(temp);
  125. temp2->insert_node_vin(temp);
  126.  
  127. temp->first = temp1;
  128. temp->second = temp2;
  129. }
  130. else
  131. {
  132. Edge*temp = new Edge;
  133. temp->E_num = num;
  134. vertex*temp1 = V_front;
  135. while(temp1 != NULL)
  136. {
  137. if(temp1->V_num == v1)
  138. break;
  139. temp1 = temp1->vnext;
  140. }
  141. E_rear->enext = temp;
  142. temp->enext = NULL;
  143. E_rear = temp;
  144. vertex*temp2 = new vertex;
  145. temp2->V_num = v2;
  146. V_rear->vnext = temp2;
  147. temp2->vnext = NULL;
  148. V_rear = temp2;
  149.  
  150. temp1->insert_node_vout(temp);
  151. temp2->insert_node_vin(temp);
  152.  
  153. temp->first = temp1;
  154. temp->second = temp2;
  155. }
  156. }
  157. else // if v1 is not present.
  158. {
  159. if(ispresent(v2))
  160. {
  161. Edge*temp = new Edge;
  162. temp->E_num = num;
  163. vertex*temp2 = V_front;
  164. while(temp2 != NULL)
  165. {
  166. if(temp2->V_num == v2)
  167. break;
  168. temp2 = temp2->vnext;
  169. }
  170. E_rear->enext = temp;
  171. temp->enext = NULL;
  172. E_rear = temp;
  173. vertex*temp1 = new vertex;
  174. temp1->V_num = v1;
  175. V_rear->vnext = temp1;
  176. temp1->vnext = NULL;
  177. V_rear = temp1;
  178.  
  179. temp1->insert_node_vout(temp);
  180. temp2->insert_node_vin(temp);
  181.  
  182. temp->first = temp1;
  183. temp->second = temp2;
  184. }
  185. else // both v1 and v2 are not present. It means either there are no elements in graph or only v1 & v2 are not present.
  186. {
  187. Edge*temp = new Edge;
  188. temp->E_num = num;
  189. if(E_front == NULL)
  190. {
  191. E_front = E_rear = temp;
  192. temp->enext = NULL;
  193. }
  194. else
  195. {
  196. E_rear->enext = temp;
  197. temp->enext = NULL;
  198. E_rear = temp;
  199. }
  200. vertex*temp1 = new vertex;
  201. temp1->V_num = v1;
  202. vertex*temp2 = new vertex;
  203. temp2->V_num = v2;
  204. if(V_front == NULL)
  205. {
  206. V_front = V_rear = temp1;
  207. temp1->vnext = temp2;
  208. temp2->vnext = NULL;
  209. V_rear = temp2;
  210. }
  211. else
  212. {
  213. V_rear->vnext = temp1;
  214. temp1->vnext = temp2;
  215. temp2->vnext = NULL;
  216. V_rear = temp2;
  217. }
  218.  
  219. temp1->insert_node_vout(temp);
  220. temp2->insert_node_vin(temp);
  221.  
  222. temp->first = temp1;
  223. temp->second = temp2;
  224. cout<<"both "<<v1<<" and "<<v2<<" inserted in graph along with their edge number "<<num<<endl;
  225. }
  226. }
  227. } // End void insert_edge(v1,v2,num).
  228.  
  229. bool ispresent(int v)
  230. {
  231. if(V_front == NULL)
  232. return false;
  233.  
  234. vertex*temp = V_front;
  235. while(temp != NULL)
  236. {
  237. if(temp->V_num == v)
  238. return true;
  239. temp = temp->vnext;
  240. }
  241. return false;
  242. }
  243.  
  244. int label_vertex(int v)
  245. {
  246. vertex*temp = V_front;
  247. while(temp != NULL)
  248. {
  249. if(temp->V_num == v)
  250. return temp->label;
  251. temp = temp->vnext;
  252. }
  253.  
  254. return 0;
  255. }
  256. }; // END OF CLASS GRAPH.
  257.  
  258. int main()
  259. {
  260. Graph G;
  261. int v1=-1,v2,edgenum;
  262. cin>>v1>>v2>>edgenum;
  263. while(v1 != -1)
  264. {
  265. G.insert_edge(v1,v2,edgenum);
  266. cin>>v1;
  267. if(v1==-1)
  268. break;
  269. cin>>v2>>edgenum;
  270. }
  271. /* Now we have to define three arrays:- color, label, parent. For that either of steps can be taken:
  272.   i) use a data member size in class graph and increment with each vertex addition
  273.   ii)use three more data member inside vertex class ,namely color, label, parent.
  274.   We will be using ii) case as it is more complex to implement.
  275.   */
  276. cout<<"Enter the vertices between whom shortest distance is to be find out: ";
  277. cin>>v1>>v2;
  278. /* Here we will label all the vertex to practice.Otherwise there is no need of this because start BFS from v1 and when we get v2 print its
  279.   label & also no need of taking "label" as data member because we have to just ++ a variable initialise to 0 whenever we pop-out of
  280.   queue and no need of labelling all intermediate vertices.But we do it here.
  281.   */
  282.  
  283. BFS(G,v1); // as discussed above this would change to BFS(G,v1,v2) and just return shortest distance directly.
  284.  
  285. int shortest_path = G.label_vertex(v2) - G.label_vertex(v1);
  286.  
  287. cout<<"Shortest path between "<<v1<<" and "<<v2<<" is : "<<shortest_path<<endl;
  288. // WE CAN ALSO STATE THE PATH TAKEN.
  289. //system("pause");
  290. return 0;
  291. }
  292.  
  293. struct queuenode{
  294. vertex*vpointer;
  295. queuenode*qnext;
  296. };
  297.  
  298. class queue{
  299. public:
  300. queuenode*front;
  301. queuenode*rear;
  302. int length;
  303.  
  304. queue(){ front = NULL;
  305. rear = NULL;
  306. length = 0;
  307. }
  308.  
  309. void enqueue(vertex*x)
  310. {
  311. queuenode*newnode = new queuenode;
  312. newnode->vpointer = x;
  313. newnode->qnext = NULL;
  314. if(length==0)
  315. {
  316. front = rear = newnode;
  317. length++;
  318. cout<<"Enqueued "<<x->V_num<<endl;
  319. return;
  320. }
  321.  
  322. rear->qnext = newnode;
  323. length++;
  324. rear = newnode;
  325. cout<<"Enqueued "<<x->V_num<<endl;
  326. }
  327.  
  328. void dequeue(){
  329. if(front == NULL)
  330. return;
  331.  
  332. queuenode*temp = front;
  333. front = front->qnext;
  334. cout<<"Dequeued "<<temp->vpointer->V_num<<endl;
  335. delete temp;
  336. length--;
  337.  
  338. }
  339. };
  340.  
  341.  
  342. void BFS(Graph G, int v)
  343. {
  344. vertex*temp = G.V_front; // Here we have taken "." instead of "->" in G.V_front.This is because "->" is taken only when object
  345. while(temp->V_num != v) // is of pointer type, like Graph*G.Thanks!!
  346. temp = temp->vnext;
  347.  
  348. queue q;
  349. q.enqueue(temp);
  350. temp->color = 1; // gray- visited once and push in queue.
  351. temp->label = 0; // level of that element.
  352. while(q.length != 0) // queue is not empty.
  353. {
  354. queuenode*temp1 = q.front;
  355. node*temp2 = temp1->vpointer->E_out_front;
  356. while(temp2 != NULL)
  357. { if(temp2->data->second->color == 0)
  358. {
  359. q.enqueue(temp2->data->second);
  360. temp2->data->second->label = temp1->vpointer->label + 1;
  361. temp2->data->second->color = 1;
  362. temp2->data->second->parent = temp1->vpointer->V_num;
  363. }
  364. temp2 = temp2->next;
  365.  
  366. }
  367. temp1->vpointer->color = 2;
  368. q.dequeue();
  369.  
  370. }
  371.  
  372. }
  373.  
  374.  
  375.  
Runtime error #stdin #stdout 0.01s 2692KB
stdin
Standard input is empty
stdout
Graph is initialised