-
Notifications
You must be signed in to change notification settings - Fork 1
/
max_cut_brute_force.cpp
76 lines (74 loc) · 1.75 KB
/
max_cut_brute_force.cpp
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
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 0;
cin >> n;
int w[n][n];
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) w[i][j] = 0;
for (int i=0; i<n; ++i) {
int x = 0;
int y = 0;
for (int j=0; j<n; ++j) {
cin >> x >> y;
if ((x==0)&&(y==0)) break;
else {
w[x-1][y-1] = 1;
w[y-1][x-1] = 1;
}
}
if ((x==0)&&(y==0)) break;
}
cout << "The weight matrix is:\n";
for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) cout << w[i][j];
cout << "\n";
}
int ans[n];
int loop[n];
for (int i=0; i<n; ++i) {
ans[i]=0;
loop[i]=0;
}
int max = 0;
for (int i=0; i<pow(2,n); ++i) {
int temp = 0;
for (int x=0; x<n; ++x) {
for (int y=0; y<n; ++y) {
if (loop[x]!=loop[y]) temp+=w[x][y];
else continue;
}
}
if (temp>max) {
max = temp;
for (int j=0; j<n; ++j) ans[j]=loop[j];
}
int carry = 0;
if (loop[n-1]==0) {
loop[n-1]++;
continue;
}
if (loop[n-1]==1) {
loop[n-1]--;
carry++;
}
for (int j=n-2; j>=0; --j) {
if (carry==0) break;
if (carry==1) {
if (loop[j]==0) {
loop[j]++;
carry--;
break;
}
if (loop[j]==1) {
loop[j]--;
}
}
}
}
cout << "x:\n";
for (int i=0; i<n; ++i) cout << ans[i];
cout << "\n";
cout << "Max cut is:" << max/2;
}