Advertisement
Zipton

Untitled

Dec 15th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. /*
  2. Given an undirected graph, max_clique() returns the size of the maximum clique,
  3. that is, the largest subset of nodes such that all pairs of nodes in the subset
  4. are connected by an edge. max_clique_weighted() additionally uses a global array
  5. w[] specifying a weight value for each node, returning the clique in the graph
  6. that has maximum total weight.
  7. Both functions apply to a global, pre-populated adjacency matrix adj[] which
  8. must satisfy the condition that adj[u][v] is true if and only if adj[v][u] is
  9. true, for all pairs of nodes u and v respectively between 0 (inclusive) and the
  10. total number of nodes (exclusive) as passed in the function argument. Note that
  11. max_clique_weighted() is an efficient implementation using bitmasks of unsigned
  12. 64-bit integers, thus requiring the number of nodes to be less than 64.
  13. Time Complexity:
  14. - O(3^(n/3)) per call to max_clique() and max_clique_weighted(), where n
  15. is the number of nodes.
  16. Space Complexity:
  17. - O(n^2) for storage of the graph, where n is the number of nodes.
  18. - O(n) auxiliary stack space for max_clique() and max_clique_weighted().
  19. */
  20.  
  21. #include "stdafx.h"
  22. #include <algorithm>
  23. #include <bitset>
  24. #include <vector>
  25. #include <string>
  26. #include <fstream>
  27. #include <iostream>
  28. #include <time.h>
  29. #include <cassert>
  30.  
  31. using namespace std;
  32. const int MAXN = 10;
  33. bool** adj;
  34. int k,u;
  35.  
  36. /* Максимальная клика
  37. ПРОЦЕДУРА extend (candidates, not):
  38. ПОКА candidates НЕ пусто И not НЕ содержит вершины, СОЕДИНЕННОЙ СО ВСЕМИ вершинами из candidates,
  39. ВЫПОЛНЯТЬ:
  40. 1 Выбираем вершину v из candidates и добавляем её в compsub
  41. 2 Формируем new_candidates и new_not, удаляя из candidates и not вершины, не СОЕДИНЕННЫЕ с v
  42. 3 ЕСЛИ new_candidates и new_not пусты
  43. 4 ТО compsub – клика
  44. 5 ИНАЧЕ рекурсивно вызываем extend (new_candidates, new_not)
  45. 6 Удаляем v из compsub и candidates, и помещаем в not
  46.  
  47. Максимальное по включению независимое множество вершин
  48. 1 Условие в основном цикле: not не должно содержать ни одной вершины, НЕ СОЕДИНЕННОЙ НИ С ОДНОЙ из вершин во множестве candidates
  49. 2 Для формирования new_candidates и new_not, необходимо удалять из candidates и not вершины, СОЕДИНЕННЫЕ с выбранной вершиной.
  50.  
  51. */
  52. int rec(int nodes, std::bitset<MAXN> &curr, std::bitset<MAXN> &pool, std::bitset<MAXN> &excl) {
  53.     cout << u << " current " << curr<<" pool "<<pool<<" exclusion "<<excl; 
  54.     if (pool.none() && excl.none()) {//ЕСЛИ new_candidates и new_not пусты, ТО compsub – клика
  55.         return curr.count();//величина локального множества
  56.     }
  57.     int ans=0;
  58.     u = 0;
  59.     for (int v = 0; v < nodes; v++) {
  60.         if ((pool[v] && !excl[v])) {
  61.             u = v;
  62.         }
  63.     }
  64.     for (int v = 0; v < nodes; v++) {
  65.         if ((!pool[v] || adj[u][v])) {
  66.             continue;
  67.         }
  68.         std::bitset<MAXN> ncurr, npool, nexcl;
  69.         for (int i = 0; i < nodes; i++) {
  70.             ncurr[i] = curr[i];
  71.         }
  72.         ncurr[v] = true;       
  73.         for (int j = 0; j < nodes; j++) {
  74.             npool[j] = pool[j] && !adj[v][j];//Формируем new_candidates и new_not, удаляя из candidates и not вершины, не СОЕДИНЕННЫЕ с v
  75.             //nexcl[j] = excl[j] && adj[v][j];//Для формирования new_candidates и new_not, необходимо удалять из candidates и not вершины, СОЕДИНЕННЫЕ с выбранной вершиной.
  76.         }
  77.         npool[v] = false;
  78.         cout << " ncurr " << ncurr << " npool " << npool << " nexcl " << nexcl<<endl;
  79.         cout << endl;
  80.         ans = std::max(ans, rec(nodes, ncurr, npool, nexcl));
  81.         pool[v] = false;//для ускорения
  82.         excl[v] = true;
  83.     }
  84.     return ans;
  85. }
  86.  
  87. int max_clique(int nodes) {
  88.     std::bitset<MAXN> curr, excl, pool;
  89.     pool.flip();
  90.     return rec(nodes, curr, pool, excl);
  91. }
  92.  
  93. void add_edge_true(int u, int v) {
  94.     adj[u][v] = true;
  95.     adj[v][u] = true;
  96. }
  97.  
  98. void add_edge_false(int u, int v) {
  99.     adj[u][v] = false;
  100.     adj[v][u] = false;
  101. }
  102.  
  103. void fileread(string word) {//Считывание матрицы из файла
  104.     int l;
  105.     ifstream in(word);
  106.     if (in.is_open())
  107.     {
  108.         in >> k;
  109.         adj = new bool*[k];
  110.         for (int i = 0; i < k; i++)
  111.             adj[i] = new bool[k];
  112.         for (int i = 0; i < k; i++)
  113.             for (int j = 0; j < k; j++)
  114.             {
  115.                 in >> l;
  116.                 if (l != 0)
  117.                 {              
  118.                     add_edge_true(i, j);
  119.                 }
  120.                 else{
  121.                     add_edge_false(i, j);
  122.                 }
  123.             }
  124.         in.close();
  125.         cout << "File readed" << endl;
  126.     }
  127.     else
  128.     {
  129.         //Если открытие файла прошло не успешно
  130.         cout << "File not found" << endl;
  131.     }  
  132. }
  133.  
  134. int main() {
  135.         string file;
  136.     cout << "Vvedite imya fila" << endl;
  137.     cin >> file;
  138.     fileread(file);
  139.     float fTimeStart = clock() / (float)CLOCKS_PER_SEC;
  140.     cout <<endl<<"Chislo vershin "<< max_clique(k) << endl;
  141.     float fTimeStop = clock() / (float)CLOCKS_PER_SEC;
  142.     long double time = (fTimeStop - fTimeStart);
  143.     printf("Dlitelnost %f secund \n", time);
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement