Advertisement
Zipton

bron kerbosch algorithm

Dec 16th, 2017
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <algorithm>
  3. #include <bitset>
  4. #include <vector>
  5. #include <string>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <time.h>
  9. #include <cassert>
  10.  
  11. using namespace std;
  12. const int MAXN = 100;
  13. bool** adj;
  14. int k, u;
  15.  
  16. /* Максимальная клика
  17. ПРОЦЕДУРА extend (candidates, not):
  18. ПОКА candidates НЕ пусто И not НЕ содержит вершины, СОЕДИНЕННОЙ СО ВСЕМИ вершинами из candidates,
  19. ВЫПОЛНЯТЬ:
  20. 1 Выбираем вершину v из candidates и добавляем её в compsub
  21. 2 Формируем new_candidates и new_not, удаляя из candidates и not вершины, не СОЕДИНЕННЫЕ с v
  22. 3 ЕСЛИ new_candidates и new_not пусты
  23. 4 ТО compsub – клика
  24. 5 ИНАЧЕ рекурсивно вызываем extend (new_candidates, new_not)
  25. 6 Удаляем v из compsub и candidates, и помещаем в not
  26.  
  27. Максимальное по включению независимое множество вершин
  28. 1 Условие в основном цикле: not не должно содержать ни одной вершины, НЕ СОЕДИНЕННОЙ НИ С ОДНОЙ из вершин во множестве candidates
  29. 2 Для формирования new_candidates и new_not, необходимо удалять из candidates и not вершины, СОЕДИНЕННЫЕ с выбранной вершиной.
  30.  
  31. */
  32.  
  33.  inline int rec(int nodes, std::bitset<MAXN> &curr, std::bitset<MAXN> &pool, std::bitset<MAXN> &excl) {
  34.     //cout << u << " current " << curr << " pool " << pool << " exclusion " << excl;
  35.     if (pool.none() && excl.none()) {//ЕСЛИ new_candidates и new_not пусты, ТО compsub – клика
  36.         return curr.count();//величина локального множества
  37.     }
  38.     int ans = 0;
  39.     u = 0;
  40.     for (int v = 0; v < nodes; v++) {
  41.         if ((pool[v])) {
  42.             u = v;
  43.         }
  44.     }
  45.     for (int v = 0; v < nodes; v++) {
  46.         if ((!pool[v] || adj[u][v])) {
  47.             continue;
  48.         }
  49.         std::bitset<MAXN> ncurr, npool, nexcl;
  50.         for (int i = 0; i < nodes; i++) {
  51.             ncurr[i] = curr[i];
  52.         }
  53.         ncurr[v] = true;
  54.         for (int j = 0; j < nodes; j++) {
  55.             npool[j] = pool[j] && adj[v][j];//Формируем new_candidates и new_not, удаляя из candidates и not вершины, не СОЕДИНЕННЫЕ с v
  56.             nexcl[j] = excl[j] && adj[v][j];//Для формирования new_candidates и new_not, необходимо удалять из candidates и not вершины, СОЕДИНЕННЫЕ с выбранной вершиной.
  57.         }
  58.         //cout << " ncurr " << ncurr << " npool " << npool << " nexcl " << nexcl << endl;
  59.         //cout << endl;
  60.         ans = std::max(ans, rec(nodes, ncurr, npool, nexcl));
  61.         pool[v] = false;
  62.         excl[v] = true;
  63.     }
  64.     return ans;
  65. }
  66.  
  67. inline int max_ind_set(int nodes) {
  68.     std::bitset<MAXN> curr, excl, pool;
  69.     pool.flip();
  70.     return rec(nodes, curr, pool, excl);
  71. }
  72.  
  73. void add_edge_true(int u, int v) {
  74.         adj[u][v] = false;
  75.         adj[v][u] = false; 
  76. }
  77.  
  78. void add_edge_false(int u, int v) {
  79.     if (u == v) {
  80.         adj[u][v] = false;
  81.         adj[v][u] = false;
  82.     }
  83.     else {
  84.         adj[u][v] = true;
  85.         adj[v][u] = true;
  86.     }
  87. }
  88.  
  89. void fileread(string word) {//Считывание матрицы из файла
  90.     int l;
  91.     ifstream in(word);
  92.     if (in.is_open())
  93.     {
  94.         in >> k;
  95.         adj = new bool*[k];
  96.         for (int i = 0; i < k; i++)
  97.             adj[i] = new bool[k];
  98.         for (int i = 0; i < k; i++)
  99.             for (int j = 0; j < k; j++)
  100.             {
  101.                 in >> l;
  102.                 if (l != 0)
  103.                 {
  104.                     add_edge_true(i, j);
  105.                 }
  106.                 else {
  107.                     add_edge_false(i, j);
  108.                 }
  109.             }
  110.         in.close();
  111.         cout << "File readed" << endl;
  112.     }
  113.     else
  114.     {
  115.         //Если открытие файла прошло не успешно
  116.         cout << "File not found" << endl;
  117.     }
  118. }
  119.  
  120. int main() {
  121.     string file;
  122.     cout << "Vvedite imya fila" << endl;
  123.     cin >> file;
  124.     fileread(file);
  125.     float fTimeStart = clock() / (float)CLOCKS_PER_SEC;
  126.     cout << endl << "Chislo vershin " << max_ind_set(k) << endl;
  127.     float fTimeStop = clock() / (float)CLOCKS_PER_SEC;
  128.     long double time = (fTimeStop - fTimeStart);
  129.     printf("Dlitelnost %f secund \n", time);
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement