Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. /*
  2. Fie G(X,U)/*
  3. Fie G(X,U) graf neorientat reprezentat prin matrice de adiacenta
  4. X multimea nodurilor , |X|=n , i.e. sujnt in total n noduri
  5. U multimea arcelor , multime de perechi neordonate din X (i,j) cu i!=j
  6.  
  7. Fie x1 nod de start .
  8. Se cere sa se efectueze o parcurgere a grafului pornind din nodul x1
  9. navingand pe muchiile acestuia
  10.  
  11. exemplu ( peters graph )
  12.  
  13.  
  14. * 1
  15. / \
  16. 2 *--*5
  17. | |
  18. 3 *--*4
  19.  
  20. fie x1=1 nod de start
  21.  
  22. Strategii de parcurgere
  23.  
  24. [I] DFS ( deep first search )
  25.  
  26. Consta in a vizita continuu prim adiacent nevizitat inca al curentului
  27. ( daca acesta exista )
  28. pentru x1=1
  29.  
  30. * 1
  31. / \
  32. 2 *--*5
  33. | |
  34. 3 *--*4
  35.  
  36. DFS(1)=1,2,3,4,5
  37.  
  38. ATT : strategia DFS este de tip BKTR !~~
  39.  
  40. adica daca pentru nod curent nu exista ceea ce caut ( "prim adiacent nevizitat inca" )
  41. ma intorc la nod anterior pentru a incerca explorare printr-un alt nod
  42.  
  43. Adica pentru o variatie :
  44.  
  45.  
  46.  
  47. * 1
  48. / \
  49. 2 *--*5
  50. | |
  51. 3 *--*4
  52. /
  53. 6*
  54.  
  55. DFS(1)=1,2,3,4,5,6
  56.  
  57. Deci DFS fiind de tip BKTR exista abordari iterative ( in cicluri de tip while
  58. " atata timp cat mai sunt succesori " i.e. "atata timp cat mai sunt tentative
  59. de explorare din nod curent" ) sau recursive ( autoapel pentru
  60. toti adiacentii nevizitati inca i.e. ciclul iterativ de tip while se transforma
  61. in autoapel
  62.  
  63. Cu scopul unor optimziari exista formulari "degenerate" de BKTR
  64. pentru a rula DFS
  65.  
  66.  
  67.  
  68. */
  69.  
  70.  
  71. #include<fstream>
  72. #include<iostream>
  73. using namespace std;
  74.  
  75. int n; // n=|X|
  76. int G[100][100]; // G(X,U)
  77. int S[100]; // solutia ( lista nodurilor vizitate )
  78. int V[100]; // vector caracteristic al vizitatilor
  79. int vf;
  80. int top;
  81.  
  82.  
  83. int read_data()
  84. { fstream f;
  85. f.open("input.dat",ios::in);
  86. f>>n;
  87. for(int i=1;i<=n;i++)
  88. for(int j=1;j<=n;j++) f>>G[i][j];
  89. f>>vf;
  90. /*
  91. * 1
  92. / \
  93. 2 *--*5
  94. | |
  95. 3 *--*4
  96.  
  97. vf=1
  98.  
  99. input.dat :
  100.  
  101. 5
  102. 0 1 0 0 1
  103. 1 0 1 0 1
  104. 0 1 0 1 0
  105. 0 0 1 0 1
  106. 1 1 0 1 0
  107. 1
  108. */
  109.  
  110. }
  111. /*
  112. int DFS_IT(int vf)
  113. {
  114. top=1;
  115. S[top]=vf
  116. cout<<vf;
  117. V[vf]=1;
  118. int gasit=1;
  119. while (gasit) {
  120. gasit=0;
  121. for(int i=1;i<=n;i++)
  122. { if () { gasit=1;
  123. break;
  124. }
  125. }
  126. if (gasit==1) { top++;
  127. S[top]=i;
  128. V[i]=1;
  129. cout<<i<<" ";
  130. }
  131. else top--;
  132. }
  133. }
  134. */
  135.  
  136. int succ(int S[100],int top)
  137. {
  138. if (S[top]<n) {
  139. S[top]++;
  140. V[top] = 1;
  141. return 1;
  142. }
  143. return 0;
  144. }
  145.  
  146. int valid(int S[100],int top)
  147. {
  148. if (top==1) return 1;
  149. for(int i=1;i<top;i++)
  150. if (S[i]==S[top]) {return 0;}
  151. return 1;
  152. }
  153.  
  154. int tipar(int S[100],int top)
  155. {
  156. cout<<endl;
  157. for(int i=1;i<=top;i++)
  158. cout<<S[i]<<" ";
  159. return 1;
  160. }
  161.  
  162. int solutie(int S[100],int top)
  163. {
  164. if (top==n) return 1;
  165. if (G[S[vf][top]==1 && V[top]==0)
  166. return 0;
  167. }
  168.  
  169. int init(int S[100],int top)
  170. {
  171. S[top]=top;
  172. cout << vf;
  173. return 1;
  174. }
  175.  
  176. int tipar()
  177. {
  178.  
  179. }
  180.  
  181. int bktr()
  182. {
  183. top = 1;
  184. V[vf] = 1;
  185. init(S,top);
  186. while (top>=1)
  187. {
  188. int am;
  189. int este;
  190. do{
  191. am=succ(S,top);
  192. este=valid(S,top);
  193. } while (!((am&&este)||(!am)));
  194. if (am) {
  195. if (solutie(S,top)) { tipar(S,top);}
  196. else {
  197. top++;
  198. init(S,top);
  199. }
  200. }
  201. else {top--;}
  202. }
  203. return 1;}
  204.  
  205. int main()
  206. {
  207. read_data();
  208. bktr();
  209. //DFS_IT(1); // 1 2 3 4 5
  210. /*
  211. Tema :
  212. Sa se modifice DFS_IT "degenerat" in forma explicita BKTR standard
  213.  
  214. 1) init
  215. 2) tipar
  216. 3) valid
  217. 4) succesor
  218. 5) solutie
  219.  
  220. while (vf>=1) {
  221. int am;
  222. int este;
  223. do {
  224. am=succesor(ST,vf);
  225. este=valid(ST,vf);
  226. } while(!((am&&este)||(!am)));
  227. if (am) { if (solutie(ST,vf)) tipar(ST,vf);
  228. else { vf++;
  229. init(ST,vf);}
  230. }
  231. else vf--;
  232. }
  233.  
  234. */
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement