Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. void Graph::IDS(int x, int required, int depth = 1)
  2. {
  3. if(x == required) return;
  4.  
  5. cout << "Iterated Deepening Search for " << required << ", starting from vertex " << x << " : " << endl;
  6.  
  7. IDS_util(x, required, depth);
  8.  
  9. cout << endl;
  10. }
  11.  
  12. void Graph::IDS_util(int x, int required, int depth)
  13. {
  14. stack s;
  15. bool *visited = new bool[n+1];
  16. int i, j, k;
  17.  
  18. for(i = 0; i <= n; i++)
  19. visited[i] = false;
  20.  
  21. cout << "Depth = " << depth << ": ";
  22.  
  23. visited[x] = true;
  24.  
  25. for (int c = 1; c <= n; c++){
  26. s.push(x);
  27.  
  28. if(isConnected(x, c) && !visited[c])
  29. {
  30. for (j = 0; j < depth; j++){
  31. k = s.pop();
  32.  
  33. if(k == required) return;
  34.  
  35. cout << "[" << k <<"] ";
  36.  
  37. for (i = n; i >= 0 ; --i)
  38. if (isConnected(k, i) && !visited[i]) {
  39. s.push(i);
  40. visited[i] = true;
  41. }
  42. }
  43. }
  44. }
  45.  
  46. if(depth == n) return;
  47.  
  48. cout << endl;
  49.  
  50. IDS_util(x, required, depth+1);
  51. }
  52.  
  53. 0,1,1,1,0,0,0,0,0
  54. 0,0,0,0,1,0,0,0,0
  55. 0,0,0,0,0,1,1,0,0
  56. 0,0,0,0,0,0,0,1,0
  57. 0,0,0,0,0,0,0,0,1
  58. 0,0,0,0,0,0,0,0,0
  59. 0,0,0,0,0,0,0,0,0
  60. 0,0,0,0,0,0,0,0,0
  61. 0,0,0,0,0,0,0,0,0
  62.  
  63. [1]
  64. / |
  65. [2] [3] [4]
  66. / /
  67. [5] [6] [7] [8]
  68. /
  69. [9]
  70.  
  71. Iterated Deepening Search for 7, starting from vertex 1 :
  72. Depth = 0:
  73. Depth = 1: [1]
  74. Depth = 2: [1] [2]
  75. Depth = 3: [1] [2] [5]
  76. Depth = 4: [1] [2] [5] [9]
  77. Depth = 5: [1] [2] [5] [9] [3]
  78. Depth = 6: [1] [2] [5] [9] [3] [6]
  79. Depth = 7: [1] [2] [5] [9] [3] [6]
  80.  
  81. void Graph::IDS(int x, int required)
  82. {
  83. if(x == required) return;
  84.  
  85. cout << "Iterated Deepening Search for " << required << ", starting from vertex " << x << " : " << endl;
  86.  
  87. for (int d = 0 ; d <= n ; d++)
  88. if (IDS_util(x, required, d))
  89. return;
  90.  
  91. cout << required << " was unable to be located via " << x << endl;
  92. }
  93.  
  94.  
  95.  
  96. bool Graph::IDS_util(int x, int required, int depth){
  97.  
  98. if(x == required) return true;
  99.  
  100. stack s, x_child;
  101. bool *visited = new bool[n+1];
  102. int i,k, d, sub_k;
  103.  
  104. for(i = 0; i <= n; i++) visited[i] = false;
  105.  
  106. visited[x] = true;
  107.  
  108. for (i = n; i >= 0 ; --i)
  109. if (isConnected(x, i))
  110. x_child.push(i);
  111.  
  112. cout << '[' << x << "] ";
  113.  
  114. while(!x_child.isEmpty()){
  115. k = x_child.pop();
  116. s.push(k);
  117.  
  118. for(d = 0; d < depth; d++){
  119. sub_k = s.pop();
  120. if(sub_k == required) return true;
  121.  
  122. cout << '[' << sub_k << "] ";
  123.  
  124. for (i = 0; i <= n; i++){
  125. if (isConnected(sub_k, i) && !visited[i]) {
  126. if (i == required){
  127. cout << "nn" << required << " is a child of " << sub_k << endl;
  128. return true;
  129. }
  130.  
  131. s.push(i);
  132. visited[i] = true;
  133. }
  134. }
  135. }
  136. }
  137. cout<<endl;
  138. delete [] visited;
  139.  
  140. return false;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement