allia

BFS

Dec 16th, 2020 (edited)
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include<iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. class Graph
  6. {
  7.   private:
  8.   bool orient;
  9.   int n, **arr, **vse_rebra, shet_reber, comp, *path_comp, *Lev;
  10.  
  11.   public:
  12.   Graph (int **matrix, int x, int shet)
  13.   {
  14.     n = x;
  15.     arr = matrix;
  16.     shet_reber = shet;
  17.   }
  18.   void spisok ();
  19.   void get_spisok_reber();
  20.   int BFS(int v, int u);
  21.   int poisk_path(int v, int u);
  22.   void path(int v, int u);
  23. };
  24.  
  25. void Graph::spisok ()
  26.   {
  27.     int shet = 0;
  28.  
  29.      vse_rebra = new int*[shet_reber];
  30.  
  31.      for ( int i = 0; i < shet_reber; i++)
  32.        vse_rebra[i] = new int[2];
  33.  
  34.     for (int i =0; i < n; i++)
  35.      for (int j = i; j < n; j++)  
  36.      if (arr[i][j] == 1)
  37.      {
  38.        vse_rebra[shet][0] = i+1;
  39.        vse_rebra[shet][1] = j+1;
  40.        shet++;
  41.      }
  42.   }
  43.  
  44. void Graph::path(int v, int u)
  45. {
  46.   int razmer = Lev[u];
  47.   path_comp = new int[razmer];
  48.  
  49.   path_comp[razmer - 1] = u+1;
  50.  
  51.   for (int i = razmer - 2; i >= 0; i--)
  52.     for (int j = 0; j < n; j++)
  53.       if (Lev[j] == i+1 && poisk_path(j, path_comp[i+1]-1) > 0)
  54.         path_comp[i] = j+1;
  55.  
  56.   for ( int i = 0; i < razmer; i++)
  57.    cout << path_comp[i] << " ";
  58. }
  59.  
  60. int Graph::poisk_path(int v, int u)
  61. {
  62.   int shet, x, *path = new int[n], dlina = 0;
  63.  
  64.   queue<int> Que;
  65.  
  66.   for (shet = 0; shet < n; shet++)
  67.     {
  68.       path[shet] = 0;
  69.     }
  70.  
  71.   path[v] = 1;
  72.   Que.push(v);
  73.  
  74.   while (!Que.empty())
  75.    {
  76.      shet = Que.front();
  77.      Que.pop();
  78.     for (x = 0; x < n; x++)
  79.       if (arr[shet][x] != 0 && path[x] == 0)
  80.       {
  81.         path[x] = path[shet] + 1;
  82.         dlina++;
  83.         Que.push(x);
  84.       }
  85.    }
  86.  
  87.  if (path[u] == 0)
  88.    dlina = -1;
  89.     else if (u == v)
  90.      dlina = 0;
  91.  
  92.   return dlina;
  93. }
  94.  
  95. int Graph::BFS(int v, int u)
  96. {
  97.   int shet, x, dlina = 0;
  98.   Lev = new int[n];
  99.  
  100.   queue<int> Que;
  101.  
  102.   for (shet = 0; shet < n; shet++)
  103.     {
  104.       Lev[shet] = 0;
  105.     }
  106.  
  107.   Lev[v] = 1;
  108.   Que.push(v);
  109.  
  110.   while (!Que.empty())
  111.    {
  112.      shet = Que.front();
  113.      Que.pop();
  114.     for (x = 0; x < n; x++)
  115.       if (arr[shet][x] != 0 && Lev[x] == 0)
  116.       {
  117.         Lev[x] = Lev[shet] + 1;
  118.         dlina++;
  119.         Que.push(x);
  120.       }
  121.    }
  122.  
  123.  if (Lev[u] == 0)
  124.    dlina = -1;
  125.     else if (u == v)
  126.      dlina = 0;
  127.      else dlina = Lev[u]-1;
  128.  
  129.   return dlina;
  130. }
  131.  
  132. int main()
  133. {
  134.  int n, shet_reber = 0, v, u;
  135.  cin >> n;
  136.  
  137. int **arr = new int*[n];
  138.  
  139. for ( int i = 0; i < n; i++)
  140.      arr[i] = new int[n];
  141.    
  142. for (int i =0; i < n; i++)
  143.   for (int j =0; j < n; j++)  
  144.      {
  145.        cin >> arr[i][j];
  146.         if (arr[i][j] == 1)
  147.          shet_reber++;
  148.      }
  149.  
  150.  shet_reber /= 2;
  151.  
  152.  Graph object(arr, n, shet_reber);
  153.  
  154.  cin >> v >> u;
  155.  
  156.  int dlina = object.BFS(v-1, u-1);
  157.  
  158.  cout << dlina << endl;
  159.  
  160.  if (dlina > 0)
  161.   object.path(v-1, u-1);
  162. }
Add Comment
Please, Sign In to add comment