Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <time.h>
  4. #include "Profiler.h"
  5. //#define TEST
  6.  
  7. using namespace std;
  8.  
  9. Profiler profile;
  10. int BFS_Count;
  11.  
  12. typedef struct _QNODE {
  13.     int key;
  14.     struct _QNODE* next;
  15. }QNODE;
  16.  
  17. int *gParentVect;
  18.  
  19. //LIFO
  20. class Queue {
  21. private:
  22.     QNODE *Head;
  23.     QNODE *Tail;
  24.     int Count;
  25.  
  26.     QNODE* newNode(int key)
  27.     {
  28.         QNODE *node = new QNODE;
  29.         node->key = key;
  30.         node->next = NULL;
  31.         return node;
  32.     }
  33.  
  34. public:
  35.  
  36.     Queue()
  37.     {
  38.         Head = Tail = NULL;
  39.         Count = 0;
  40.     }
  41.  
  42.     ~Queue()
  43.     {
  44.         QNODE *temp = NULL;
  45.         while (NULL != Head)
  46.         {
  47.             temp = Head;
  48.             Head = Head->next;
  49.             free(temp);
  50.         }
  51.  
  52.         free(Head);
  53.         free(Tail);
  54.         Count = 0;
  55.     }
  56.  
  57.     void enqueue(int Key)
  58.     {
  59.         QNODE *node = newNode(Key);
  60.  
  61.         if (NULL == Head)
  62.         {
  63.             BFS_Count += 2;
  64.             Head = Tail = node;
  65.         }
  66.         else
  67.         {
  68.             BFS_Count += 2;
  69.             Tail->next = node;
  70.             Tail = Tail->next;
  71.         }
  72.  
  73.         BFS_Count++;
  74.         Count++;
  75.     }
  76.  
  77.     bool isEmpty() { return 0 == Count; }
  78.  
  79.     int dequeue()
  80.     {
  81.         if (isEmpty())
  82.         {
  83.             return -1;
  84.         }
  85.  
  86.         Count--;
  87.         QNODE* temp = Head;
  88.         int retKey = temp->key;
  89.         Head = Head->next;
  90.  
  91.         free(temp);
  92.  
  93.         return retKey;
  94.     }
  95. };
  96.  
  97. class Graph {
  98. private:
  99.     vector<int> *Adjacence;
  100.     int Count;
  101.  
  102. public:
  103.  
  104.     Graph(int nrVertices)
  105.     {
  106.         Count = nrVertices;
  107.         Adjacence = new vector<int>[Count];
  108.     }
  109.  
  110.     void addEdge(int A, int B)
  111.     {
  112.         Adjacence[A].push_back(B);
  113.         Adjacence[B].push_back(A);
  114.     }
  115.  
  116.     void genGraph(int nrEdges)
  117.     {
  118.         bool **connections = new bool*[Count];
  119.         for (int i = 0; i < Count; i++)
  120.         {
  121.             connections[i] = (bool*)calloc(Count, sizeof(bool));
  122.         }
  123.  
  124.         int VertexA, VertexB;
  125.         while (NULL != nrEdges)
  126.         {
  127.             VertexA = rand() % Count;
  128.             do {
  129.                 VertexB = rand() % Count;
  130.             } while (VertexA == VertexB);
  131.  
  132.             if (true == connections[VertexA][VertexB])
  133.             {
  134.                 continue;
  135.             }
  136.  
  137.             connections[VertexA][VertexB] = connections[VertexB][VertexA] = true;
  138.             addEdge(VertexA, VertexB);
  139.             nrEdges--;
  140.         }
  141.  
  142.         for (int i = 0; i < Count; i++)
  143.         {
  144.             free(connections[i]);
  145.         }
  146.         delete connections;
  147.     }
  148.  
  149.     void clear()
  150.     {
  151.         delete[] Adjacence;
  152.         Adjacence = new vector<int>[Count];
  153.     }
  154.  
  155.     void print()
  156.     {
  157.         cout << "Node count: " << Count << '\n';
  158.         for (int i = 0; i < Count; i++)
  159.         {
  160.             cout << "Node " << i << ": ";
  161.             for (int j = 0; j < Adjacence[i].size(); j++)
  162.             {
  163.                 cout << Adjacence[i][j] << " ";
  164.             }
  165.             cout << '\n';
  166.         }
  167.     }
  168.  
  169.     void BFS()
  170.     {
  171.         bool *visited = (bool*)calloc(Count, sizeof(bool));
  172.  
  173.         //mapping all connected components
  174.         for (int i = 0; i < Count; i++)
  175.         {
  176.             if (false == visited[i])
  177.             {
  178.                 Queue *Q = new Queue();
  179.                 Q->enqueue(i);
  180.                 visited[i] = true;
  181.  
  182. #ifdef TEST
  183.                 gParentVect[i] = -1;
  184. #endif
  185.  
  186.                 while (false == Q->isEmpty())
  187.                 {
  188.                     BFS_Count += 2;
  189.                     int key = Q->dequeue();
  190.  
  191. #ifdef TEST
  192.                     cout << key << '\n';
  193. #endif
  194.  
  195.                     for (int i = 0; i < Adjacence[key].size(); i++)
  196.                     {
  197.                         BFS_Count++;
  198.                         if (false == visited[Adjacence[key][i]])
  199.                         {
  200.                             BFS_Count++;
  201.                             Q->enqueue(Adjacence[key][i]);
  202.                             visited[Adjacence[key][i]] = true;
  203.  
  204. #ifdef TEST
  205.                             gParentVect[Adjacence[key][i]] = key;
  206.  
  207. #endif
  208.                         }
  209.  
  210.                     }
  211.                 }
  212.  
  213.             }
  214.         }
  215.  
  216.         delete visited;
  217.     }
  218. };
  219.  
  220. void prettyPrint(int *vect, int index, int tab, int n)
  221. {
  222.     for (int i = 0; i < tab; i++)
  223.     {
  224.         cout << '\t';
  225.     }
  226.     cout << index << '\n';
  227.  
  228.     for (int i = 0; i < n; i++)
  229.     {
  230.         if (vect[i] == index)
  231.         {
  232.             prettyPrint(vect, i, tab + 1, n);
  233.         }
  234.     }
  235. }
  236.  
  237. void prettyPrint(int *vect, int n)
  238. {
  239.     int root = 0;
  240.     while (vect[root] != -1)
  241.     {
  242.         root = vect[root] - 1;
  243.     }
  244.     prettyPrint(vect, root, 0, n);
  245. }
  246.  
  247. int main()
  248. {
  249.     srand(time(NULL));
  250.     //int n = 10, m = 15;
  251.     //Graph *G = new Graph(n);
  252.     //gParentVect = (int*)malloc(n * sizeof(int));
  253.     //G->genGraph(m);
  254.     //G->print();
  255.     //G->BFS();
  256.     //cout << '\n';
  257.     //prettyPrint(gParentVect, n);
  258.  
  259.     //V fixed
  260.     Graph *G = new Graph(100);
  261.     for (int n = 1000; n < 4500; n += 100)
  262.     {
  263.         cout << n / 100 + 1 << "/" << 45 << '\n';
  264.         BFS_Count = 0;
  265.         G->genGraph(n);
  266.         G->BFS();
  267.  
  268.         G->clear();
  269.         profile.countOperation("V fixed", n, BFS_Count);
  270.     }
  271.  
  272.     //E fixed
  273.     for (int n = 100; n < 200; n += 10)
  274.     {
  275.         cout << n / 10 + 1 << "/" << 20 << '\n';
  276.         BFS_Count = 0;
  277.  
  278.         Graph *G = new Graph(n);
  279.         G->genGraph(4500);
  280.         G->BFS();
  281.  
  282.         G->clear();
  283.         delete G;
  284.         profile.countOperation("E fixed", n, BFS_Count);
  285.     }
  286.  
  287.     profile.showReport();
  288.  
  289.     system("pause");
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement