Advertisement
Xom9ik

Alg_Lab_9 (lll semester)

Nov 20th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. //поиск в глубину
  8. void DFS(int* graph[], int st, int size, bool visited[])
  9. {
  10.     cout << st + 1 << " ";
  11.     visited[st] = true;
  12.     for (int r = 0; r <= size; r++)
  13.         if ((graph[st][r] != 0) && (!visited[r]))
  14.             DFS(graph, r, size, visited);
  15. }
  16. //поиск в ширину
  17. void BFS(int* graph[], bool *visited, int unit, int size)
  18. {
  19.     int *queue = new int[size];
  20.     int count, head;
  21.     for (int i = 0; i<size; i++) queue[i] = 0;
  22.     count = 0; head = 0;
  23.     queue[count++] = unit;
  24.     visited[unit] = true;
  25.     while (head<count)
  26.     {
  27.         unit = queue[head++];
  28.         cout << unit + 1 << " ";
  29.         for (int i = 0; i<size; i++)
  30.             if (graph[unit][i] && !visited[i])
  31.             {
  32.                 queue[count++] = i;
  33.                 visited[i] = true;
  34.             }
  35.     }
  36.     delete[]queue;
  37. }
  38.  
  39. int main()
  40. {
  41.     setlocale(LC_ALL, "Rus");
  42.     srand(time(NULL));
  43.     int size, option, start;
  44.     cout << "Enter the size of the matrix: ";
  45.     cin >> size;
  46.     int **graph = new int*[size];
  47.     for (int i = 0; i < size; i++)
  48.         graph[i] = new int[size];
  49.     cout << "1 - Random" << endl;
  50.     cout << "2 - Manual" << endl;
  51.     cout << "Your choice: ";
  52.     cin >> option;
  53.     switch (option)
  54.     {
  55.     case 1:
  56.         for (int i = 0; i < size; i++)
  57.             for (int j = 0; j < size; j++)
  58.                 graph[i][j] = 0 + rand() % 2;
  59.         break;
  60.     case 2:
  61.         for (int i = 0; i < size; i++)
  62.             for (int j = 0; j < size; j++)
  63.             {
  64.                 cout << "[" << i << "][" << j << "->";
  65.                 cin >> graph[i][j];
  66.             }
  67.         break;
  68.     }
  69.     cout << "The adjacency matrix of a graph: " << endl;
  70.     bool *visitedDFS = new bool[size];
  71.     bool *visitedBFS = new bool[size];
  72.     for (int i = 0; i < size; i++)
  73.     {
  74.         visitedDFS[i] = false;
  75.         visitedBFS[i] = false;
  76.         for (int j = 0; j < size; j++)
  77.             cout << " " << graph[i][j];
  78.         cout << endl;
  79.     }
  80.     cout << "Enter start top: ";
  81.     cin >> start;
  82.     //в глубину
  83.     cout << "Search in depth" << endl;
  84.     cout << "Bypass order: ";
  85.     DFS(graph, start - 1, size, visitedDFS);
  86.  
  87.     //в ширину
  88.     cout << "\nSearch in width" << endl;
  89.     cout << "Bypass order: ";
  90.     BFS(graph, visitedBFS, start - 1, size);
  91.  
  92.     delete[]visitedDFS;
  93.     delete[]visitedBFS;
  94.     delete[]graph;
  95.  
  96.     cout << endl;
  97.     system("pause");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement