Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <list>
  6.  
  7. using namespace std;
  8.  
  9. class Vertex {
  10. public:
  11.     int nr;
  12.     int dist;
  13.     Vertex* next;
  14.     Vertex(int a, int b, Vertex* c) {
  15.         nr = a;
  16.         dist = b;
  17.         next = c;
  18.     }
  19. };
  20.  
  21. class Graph {
  22. public:
  23.     Vertex** NL;
  24.     int size;
  25.     int data;
  26.     void AddToNL(Vertex*& H, int nr, int data) {
  27.         Vertex* t = new Vertex(nr, data, H);
  28.         H = t;
  29.     };
  30.     Graph() {
  31.         fstream file;
  32.         file.open("graf.txt", ios::in);
  33.         size;
  34.         data;
  35.         file >> size;
  36.  
  37.         //LISTA SASIEDZTWA
  38.         NL = new Vertex * [size];
  39.         for (int i = 0; i < size; i++) {
  40.             NL[i] = NULL;
  41.         }
  42.         for (int i = 0; i < size; i++) {
  43.             for (int j = 0; j < size; j++) {
  44.                 file >> data;
  45.                 if (data > 0) {
  46.                     AddToNL(NL[i], j, data);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     //PRZESZUKIWANIE W SZERZ
  53.     void BFS(int s) {
  54.         cout << "Przeszukiwanie wszerz" << endl;
  55.         char* colors = new char[size];
  56.         for (int i = 0; i < size; i++) {
  57.             colors[i] = 'w';
  58.         }
  59.         list<int> queue;
  60.         colors[s] = 'g';
  61.         queue.push_back(s);
  62.         while (!queue.empty()) {
  63.             int u = queue.front();
  64.             queue.pop_front();
  65.             Vertex* p = NL[u];
  66.             while (p) {
  67.                 if (colors[p->nr] == 'w') {
  68.                     colors[p->nr] = 'g';
  69.                     cout << "Odwiedzonio wierzcholek " << p->nr << endl;
  70.                     queue.push_back(p->nr);
  71.                 }
  72.                 p = p->next;
  73.             }
  74.             colors[u] = 'b';
  75.         }
  76.     }
  77.  
  78.     //PRZESZUKIWANIE W GLAB
  79.     char* colors = new char[size];
  80.     void DFS() {
  81.         cout << "Przeszukiwanie w glab" << endl;
  82.         for (int i = 0; i < size; i++) {
  83.             colors[i] = 'w';
  84.         }
  85.         for (int i = 0; i < size; i++) {
  86.             if (colors[i] == 'w')
  87.                 visit(i);
  88.         }
  89.     }
  90.     void visit(int s) {
  91.         cout << "Odwiedzono wierzcholek " << s << endl;
  92.         colors[s] = 'g';
  93.         Vertex* p = NL[s];
  94.         while (p) {
  95.             if (colors[p->nr] == 'w') {
  96.                 visit(p->nr);
  97.             }
  98.             p = p->next;
  99.         }
  100.         colors[s] = 'b';
  101.     }
  102. };
  103.  
  104. int main() {
  105.     Graph graph;
  106.     graph.BFS(0);
  107.     cout << endl;
  108.     graph.DFS();
  109.     system("PAUSE");
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement