Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. /*
  2. ====================================================
  3. ################## Code Author ##################
  4. ====================================================
  5. Md. Tahmid Hasan
  6. #tahmidhasan3003
  7. ====================================================
  8. Code Title: Finding Block in a Graph.cpp
  9. ====================================================
  10. ############## Algorithm Reference ##############
  11. ====================================================
  12. Book Title: Introduction to Graph Theory (2nd ed.)
  13. Author: Douglas B. West
  14. ISBN: 978-81-7758-741-8
  15. ====================================================
  16. 4.1.23.* Algorithm. (Computing the blocks of a graph)
  17. Input: A connected graph G. (The blocks of a graph are the blocks of its components, which can be found by depth-first search, so we may assume that G is connected.)
  18. Idea: Build a depth-first search tree T of G, discarding portions of T as blocks are identified. Maintain one vertex called ACTIVE.
  19. Initialization: Pick a root x ∈ V(H); make x ACTIVE; set T = {x}.
  20. Iteration: Let v denote the current active vertex.
  21. 1) If v has an unexplored incident edge vw, then
  22. 1A) If w ∉ V(T), then add vw to T, mark vw explored, make w ACTIVE.
  23. 1B) If W ∈ V(T), then w is an ancestor of v; mark vw explored.
  24. 2) If v has no more unexplored incident edges, then
  25. 2A) If v ≠ x, and w is the parent of v, make w ACTIVE. If no vertex in the current subtree T' rooted at v has an explored edge to an ancestor above w, then V(T') ∪ {w} is the vertex set of a block, record this information and delete V(T') from T.
  26. 2B) If v = x, terminate.
  27. ====================================================
  28. ################ Important Notice ################
  29. ====================================================
  30. Input Details:-
  31. 1st line: total number of vertex (V)
  32. 2nd line: total number of edges (E)
  33. Next E lines: vertex of edges (u v)
  34. Last line: start vertex (root)
  35. (Here is given a sample)
  36. ====================================================
  37. filename: input.txt
  38. (Put this .txt file in the same directory of source code. Content is given below.)
  39. ====================================================
  40. 11
  41. 13
  42. 0 1
  43. 0 3
  44. 0 4
  45. 0 8
  46. 0 10
  47. 1 2
  48. 2 3
  49. 4 5
  50. 4 7
  51. 4 10
  52. 5 6
  53. 6 7
  54. 9 10
  55. 10
  56. ====================================================
  57. ################## Coding Start ##################
  58. ====================================================
  59. */
  60.  
  61. #include<bits/stdc++.h>
  62.  
  63. using namespace std;
  64.  
  65. bool findConnection(int *adjMatrix, int *parentArray, int totalV, int node, int parent) //search connection to ancestor
  66. {
  67. int ancestor = parentArray[parent];
  68. while(ancestor != -1)
  69. {
  70. if(adjMatrix[ancestor*totalV+node] == 2) //2 means explored edge
  71. return true;
  72. ancestor = parentArray[ancestor];
  73. }
  74. return false;
  75. }
  76.  
  77. bool findBlock(int *adjMatrix, int *parentArray, int totalV, int root, int parent, stack<int>* blockPoints)
  78. {
  79. if(findConnection(adjMatrix, parentArray, totalV, root, parent)) //find connection to ancestor
  80. return true;
  81.  
  82. for(int i=0; i<totalV; i++) //find vertex in tree of root
  83. if(parentArray[i] == root) //child found of root
  84. if(findBlock(adjMatrix, parentArray, totalV, i, parent, blockPoints)) //call for child i
  85. return true;
  86.  
  87. (*blockPoints).push(root); //consider as block point
  88. parentArray[root] = -1; //parent -1 means not parent means delete from tree
  89. return false;
  90. }
  91.  
  92. int main()
  93. {
  94. freopen("input.txt","r",stdin);
  95. int totalV, totalE, totalBlocks=0, rootX, activeV, associateW, tempU, tempV, *adjMatrix, *parentArray, *cutVertexArray;
  96. bool hasUnexploredE, isElement;
  97. stack <int> blockPoints;
  98.  
  99. cin>>totalV>>totalE; //total no of vertex and edges
  100.  
  101. adjMatrix = (int*)malloc(sizeof(int)*totalV*totalV); //creating adjacent matrix
  102. parentArray = (int*)malloc(sizeof(int)*totalV); //creating parent container
  103. cutVertexArray = (int*)malloc(sizeof(int)*totalV); //creating cut vertex container
  104.  
  105. memset(adjMatrix, 0, sizeof(int)*totalV*totalV); //set all value to 0
  106. memset(parentArray, -1, sizeof(int)*totalV); //set all value to -1
  107. memset(cutVertexArray, 0, sizeof(int)*totalV); //set all value to 0
  108.  
  109. for(int i=0; i<totalE; i++)
  110. {
  111. cin>>tempU>>tempV; //input space separated u,v vertex of an edge
  112. adjMatrix[tempU*totalV+tempV] = adjMatrix[tempV*totalV+tempU] = 1;
  113. }
  114.  
  115. cin>>rootX; //root vertex
  116. activeV = rootX; //set active
  117. parentArray[rootX] = -1; //no parent
  118.  
  119. while(1)
  120. {
  121. hasUnexploredE = false;
  122. for(int i=0; i<totalV; i++) //unexplored edges checking
  123. {
  124. if(*(adjMatrix+activeV*totalV+i) == 1)
  125. {
  126. hasUnexploredE = true;
  127. associateW = i;
  128. break;
  129. }
  130. }
  131. if(hasUnexploredE) //condition 1
  132. {
  133. isElement = false;
  134.  
  135. if((associateW == rootX) || (parentArray[associateW] != -1))
  136. isElement = true;
  137.  
  138. if(!isElement) //Condition 1A
  139. {
  140. parentArray[associateW] = activeV; //set a parent means added to tree
  141. adjMatrix[activeV*totalV+associateW] = adjMatrix[associateW*totalV+activeV] = 2; //mark explored
  142. activeV = associateW; //change active vertex
  143. }
  144. else //condition 1B
  145. adjMatrix[activeV*totalV+associateW] = adjMatrix[associateW*totalV+activeV] = 2; //mark explored
  146. }
  147. else //condition 2
  148. {
  149. if(activeV != rootX) //condition 2A
  150. {
  151. associateW = parentArray[activeV]; //pick parent of activeV
  152. if(!findBlock(adjMatrix, parentArray, totalV, activeV, associateW, &blockPoints)) //no vertex has explored edge to ancestor
  153. {
  154. totalBlocks++;
  155. cutVertexArray[associateW] = 1;
  156. cout<<"Block "<<totalBlocks<<": "<<associateW<<" "; //show block points
  157. while(!blockPoints.empty())
  158. {
  159. cout<<blockPoints.top()<<" ";
  160. blockPoints.pop();
  161. }
  162. cout<<endl;
  163. }
  164. activeV = associateW; //make parent active
  165. }
  166. else //condition 2B
  167. break;
  168. }
  169. }
  170. cout<<endl<<"Total Blocks: "<<totalBlocks<<endl; //total blocks
  171. cout<<"Articulation Points: "; //cut vertex
  172. for(int i=0; i<totalV; i++)
  173. if(cutVertexArray[i])
  174. cout<<i<<" ";
  175. cout<<endl;
  176.  
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement