-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextremal_mis.cc
More file actions
192 lines (169 loc) · 4.2 KB
/
extremal_mis.cc
File metadata and controls
192 lines (169 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// File: extremal_mis.cc
// Author: David W. Juedes
// Purpose: Finds the extremal graphs related to the number of maximal
// independent sets.
// In particular, this prints out the graphs with the most maximal independent
// sets of each size.
#include <set>
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<int,int> > all_edges(int n) {
vector<pair<int,int> > temp;
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
temp.push_back(make_pair(i,j));
}
}
return temp;
}
vector<set<int> > create_graph(int n, vector<pair<int,int> > &edges) {
vector<set<int> > G1;
G1.resize(n);
for (int i=0;i<edges.size();i++) {
G1[edges[i].first].insert(edges[i].second);
G1[edges[i].second].insert(edges[i].first);
}
return G1;
}
// Six Possible Edges for a graph of size 4.
// Hence, there are 2^6 = 64 possible graphs.
//
//
// Each graph has 2^4 = 16 possible subsets.
// Each of these could or could not be maximal independent sets.
//
bool IS(set<int> &I, vector<set<int> > &G) {
for (set<int>::iterator p = I.begin();
p!=I.end();++p) {
for (set<int>::iterator p1=I.begin();p1!=I.end();++p1) {
if ((*p)!=(*p1)) {
if (G[*p].count(*p1) > 0) return false;
if (G[*p1].count(*p) >0) return false;
}
}
}
return true;
}
bool MIS(set<int> &I, vector<set<int> > &G) {
int n = G.size();
if (!IS(I,G)) return false;
for (int i=0;i<n;i++) {
if (I.count(i) == 0) {
set<int> T;
T=I;
T.insert(i);
if (IS(T,G)) return false;
}
}
return true;
}
//
// Generates all subsets of the set {0..n-1}
//
vector<set<int> > all_subsets(int n) {
vector<set<int> > all_s;
int n2 = 1<<n;
for (int j=0;j<n2;j++) {
set<int> t;
for (int k=0;k<n;k++) {
if ((j&(1<<k))>0) {
t.insert(k);
}
}
all_s.push_back(t);
}
return all_s;
}
//
// Generates all graphs of size n
//
vector<vector<set<int> > > all_graphs(int n) {
vector<vector<set<int> > > all_g;
vector<pair<int,int> > all_e;
all_e = all_edges(n);
int m = all_e.size();
int m2 = 1<<m;
for (int j=0;j<m2;j++) {
vector<pair<int,int> > t1;
for (int k=0;k<all_e.size();k++) {
if ((j&(1<<k))>0) {
t1.push_back(all_e[k]);
}
}
all_g.push_back(create_graph(n,t1));
}
return all_g;
}
void printG(vector<set<int> > &G) {
int n = G.size();
for (int i=0;i<n;i++) {
cout << i <<":";
for (set<int>::iterator p = G[i].begin();
p!=G[i].end();++p) {
cout << " " << *p;
}
cout << endl;
}
}
// Counts the number of maximal independent sets in graphs of size n;
void count_MIS(int n) {
vector<pair<int,int> > all_e;
all_e = all_edges(n);
cout << "Number of possibles edges for a graph of size "<<n<<" = " << all_e.size() << endl;
vector<set<int> > all_s;
all_s = all_subsets(n);
cout << "Number of possible subsets of with at most "<< n << " elements = " << all_s.size() << endl;
//vector<vector<set<int> > > all_g;
//all_g = all_graphs(n);
//cout << "Number of possible graphs of with "<< n << " vertices = " << all_g.size() << endl;
int maxMIS = 0;
vector<set<int> > maxG;
int m = all_e.size();
int m2 = 1<<m;
for (int i=0;i<m2;i++) {
vector<pair<int,int> > GX;
for (int k=0;k<m;k++) {
if ((i&(1<<k))>0) {
GX.push_back(all_e[k]);
}
}
vector<set<int> > G = create_graph(n,GX);
//for (int i=0;i<all_g.size();i++) {
int sumMIS=0;
for (int j=0;j<all_s.size();j++) {
if (MIS(all_s[j],G)) {
sumMIS++;
}
}
if (sumMIS >maxMIS) {
maxG=G;
maxMIS = sumMIS;
}
// cout << "MIS total for graph " << i << " = " << sumMIS << endl;
}
cout << "Maximum number of MISs" << maxMIS << endl;
cout << "Extremal graph = " << endl;
printG(maxG);
}
int main() {
count_MIS(1);
count_MIS(2);
count_MIS(3);
count_MIS(4);
count_MIS(5);
count_MIS(6);
count_MIS(7);
//vector<pair<int,int> > starting_point;
//starting_point = all_edges(4);
//cout << starting_point.size() << endl;
//vector<vector<set<int> > > all_gs;
//all_gs = all_graphs(4);
//cout << all_gs.size() << endl;
//vector<set<int> > all_s;
//all_s = all_subsets(4);
//cout << all_s.size() << endl;
}