Advertisement
wintest

dfs

Dec 12th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stack>
  4. #include<vector>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. #define MAXN 1000
  10.  
  11. int N, M;
  12. int G[MAXN][MAXN] = { 0 };
  13. int U[MAXN+1], S[MAXN], P[MAXN],T[MAXN],top, t;
  14. void empty_stack() { top = -1; }
  15. void push(int x) { S[++top] = x; }
  16. void pop() { top--; }
  17. int look() { return S[top]; }
  18. bool not_empty() { return top > -1; }
  19.  
  20.  
  21. void DFS(int r)
  22. {
  23.     for (int i = 1; i <= N; i++) U[i] = 0;
  24.     empty_stack();
  25.     push(r); U[r] = 1; P[r] = 0;
  26.     while (not_empty())
  27.     {
  28.         int t = look(); if (G[t][0] > 0)
  29.         {
  30.             int v = G[t][G[t][0]--];
  31.             if (!U[v]) { U[v] = 1; P[v] = t; push(v); }
  32.         }
  33.         else pop();
  34.     }
  35. }
  36. void DFS_R(int r)
  37. {
  38.     int y;
  39.     while (G[r][0] > 0)
  40.     {
  41.         y = G[r][G[r][0]--];
  42.         if (!U[y]) { U[y] = 1; P[y] = r; DFS_R(y); }
  43.     }
  44.     T[t--] = r;
  45.  
  46. }
  47. void first_call()
  48. {
  49.     int i; for (i = 1; i <= N; i++) U[i] = 0; t = N - 1;
  50.     for (i = 1; i <= N; i++) if (U[i] == 0)
  51.     {
  52.         U[i] = 1; P[i] = 0; DFS_R(i);
  53.     }
  54. }
  55.  
  56. void DFS_R_keepCounters(int r)
  57. {
  58.     int y;
  59.     while (G[0][r] > 0)
  60.     {
  61.         y = G[r][G[0][r]--];
  62.         if (!U[y]) { U[y] = 1; P[y] = r; DFS_R(y); }
  63.     }
  64. }
  65. void first_call_keepCounters() {
  66.     int I;
  67.     for (int i = 1; i <= N; i++) { U[i] = 0; G[0][i] = G[i][0]; }
  68.     for (int i = 1; i <= N; i++) if (U[i] == 0)
  69.     {
  70.         U[i] = 1; P[i] = 0; DFS_R(i);
  71.     }
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77.     int i, u, v;
  78.    
  79.         scanf_s("%d %d", &N, &M);
  80.  
  81.  
  82.         for (i = 1; i <= M; i++) {
  83.             scanf_s("%d %d", &u, &v);
  84.             G[u][++G[u][0]] = v;
  85.             G[v][++G[v][0]] = u;
  86.         }
  87.  
  88.         //cout << M <<" "<< N << endl;
  89.  
  90.         //for (int i = 1; i <= N; i++)
  91.         //{
  92.         //  for (int j = 0; j <= N; j++)
  93.         //  {
  94.         //      cout << G[i][j] << " ";
  95.         //  }
  96.         //  cout << endl;
  97.         //}
  98.  
  99.         cout << "Iterative DFS" << endl;
  100.         DFS(1);
  101.  
  102.         for (size_t i = 0; i < N; i++)
  103.         {
  104.             cout << S[i] << endl;
  105.         }
  106.         cout << "Now recursive:" << endl;
  107.  
  108.         first_call();
  109.         for (size_t i = 0; i < N; i++)
  110.         {
  111.             cout << S[i] << endl;
  112.         }
  113.  
  114.         cout << "Now keeping the counters:" << endl;
  115.  
  116.         first_call_keepCounters();
  117.         for (size_t i = 0; i < N; i++)
  118.         {
  119.             cout << S[i] << endl;
  120.         }
  121.  
  122.         cout << "Topologic:" << endl;
  123.  
  124.         first_call();
  125.         for (size_t i = 0; i < N; i++)
  126.         {
  127.             cout << T[i] << endl;
  128.         }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement