Skip to content

Commit 83cd0e0

Browse files
committed
modified
1 parent e869fff commit 83cd0e0

File tree

6 files changed

+617
-5
lines changed

6 files changed

+617
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
wNBw6FwlEeaEFQ4KWsLmjw
2+
Graph Coloring
3+
fmYLC, ./data/gc_50_3, solver.py, Coloring Problem 1
4+
IkKpq, ./data/gc_70_7, solver.py, Coloring Problem 2
5+
pZOjO, ./data/gc_100_5, solver.py, Coloring Problem 3
6+
XDQ31, ./data/gc_250_9, solver.py, Coloring Problem 4
7+
w7hAO, ./data/gc_500_1, solver.py, Coloring Problem 5
8+
tthbm, ./data/gc_1000_5, solver.py, Coloring Problem 6

discrete-optimization/coloring/gc.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define ms(x,v) memset((x),(v),sizeof(x))
5+
6+
typedef long long LL;
7+
8+
struct Node{
9+
int num,weight;
10+
11+
bool operator<(const Node& o)const{
12+
return weight < o.weight;
13+
}
14+
};
15+
16+
struct BitSet{
17+
LL * col;
18+
const static int base = 64;
19+
BitSet(int n){
20+
col = new LL[(n+base-1)/base * base];
21+
}
22+
pair<int,int> pos(int idx){
23+
int arr_idx = (idx + base -1)/base;
24+
int bit = idx % base;
25+
return make_pair(arr_idx,bit);
26+
}
27+
int get(int idx){
28+
pair<int,int> p = pos(idx);
29+
return (col[p.first]>>p.second) & 1;
30+
}
31+
void set(int idx){
32+
pair<int,int> p = pos(idx);
33+
col[p.first]|= 1LL << p.second;
34+
}
35+
};
36+
int n,e;
37+
vector<vector<int> > G;
38+
int find_min_col(int v,const vector<int> & col){
39+
BitSet c(n);
40+
for(int i : G[v])
41+
if(col[i]!= -1)c.set(col[i]);
42+
for(int cc = 0 ; cc < n ; ++cc)
43+
if(!c.get(cc))return cc;
44+
}
45+
46+
int greedy_algorithm(vector<int> & col){
47+
int r = rand() % n;
48+
for(int i=0 ; i < col.size() ; ++i)col[i] =-1;
49+
col[r] =0;
50+
priority_queue<Node> Q;
51+
Q.push(Node{r,(int)G[r].size()});
52+
53+
while(!Q.empty()){
54+
Node now = Q.top();Q.pop();
55+
for(auto v : G[now.num]){
56+
if(col[v] ==-1){
57+
col[v] = find_min_col(v,col);
58+
Q.push(Node{v,(int)G[v].size()});
59+
}
60+
}
61+
}
62+
return (* max_element(col.begin(),col.end())) +1;
63+
return 0;
64+
}
65+
66+
void init(int n){
67+
G.resize(n);
68+
}
69+
70+
int solve(vector<int>&color){
71+
init(n);
72+
return greedy_algorithm(color);
73+
}
74+
75+
int main(){
76+
// ios::sync_with_stdio(0);
77+
// cin.tie(0);
78+
srand (time(NULL));
79+
80+
// cout << "test ok\n";
81+
cin >> n >> e;
82+
init(n);
83+
for(int _ =0 ; _ < e ; ++_){
84+
int u,v;
85+
cin >> u >> v;
86+
G[u].push_back(v);
87+
G[v].push_back(u);
88+
}
89+
90+
int times = 5;
91+
int ans = n;
92+
vector<int> col(n);
93+
94+
vector<int> tmp(n);
95+
while(times --){
96+
int now_ans = solve(tmp);
97+
if(ans > now_ans){
98+
ans = now_ans;
99+
col = tmp;
100+
}
101+
}
102+
103+
// int ans = solve();
104+
cout << ans << " " << "0\n";
105+
for(int i : col)
106+
cout << i << " ";
107+
108+
return 0;
109+
}
170 KB
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
import sys
6+
def solve_it(input_data):
7+
# Modify this code to run your optimization algorithm
8+
9+
# parse the input
10+
# print("creat tmp file")
11+
tmp_file_name = 'tmp.data'
12+
tmp_file = open(tmp_file_name, 'w')
13+
tmp_file.write(input_data)
14+
tmp_file.close()
15+
16+
sys.path.append('..')
17+
import helper
18+
19+
ans = helper.cpp_runner('gc.cpp',tmp_file_name)
20+
os.remove(tmp_file_name)
21+
22+
return ans
23+
24+
import sys
25+
26+
sys.path.append('..')
27+
import helper
28+
if __name__ == '__main__':
29+
import sys
30+
if len(sys.argv) > 1:
31+
file_location = sys.argv[1].strip()
32+
# with open(file_location, 'r') as input_data_file:
33+
# input_data = input_data_file.read()
34+
print(helper.cpp_runner('gc.cpp',file_location))
35+
else:
36+
print('This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/gc_4_1)')
37+

0 commit comments

Comments
 (0)