Guest User

Untitled

a guest
Nov 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef pair<int, int> pp;
  6. const int s = 9;
  7.  
  8. vector<int> color(vector< vector<int> > graph, int n){
  9. // basic graph coloring algorithm
  10. vector<bool> colors(n, true);
  11. vector<int> result(n,-1);
  12. result[0] = 0;// color the first vertex with color 0
  13. for ( int i=0; i<n;i++){
  14. vector<int>::iterator it;
  15. for(it = graph[i].begin();it!=graph[i].end();it++)
  16. if (result[*it] != -1) // if the vertex is colored with a color then make the color unavailable
  17. colors[result[*it]] = false;// for coloring the current vertex
  18.  
  19. // find the first available color
  20. int clr = 0;
  21. for (; clr<n;clr++)
  22. if (colors[clr])
  23. break;
  24. result[i] = clr;// color the vertex with the first available color
  25. for(it = graph[i].begin();it!=graph[i].end();it++){
  26. // reset the available colors
  27. if (result[*it] != -1)
  28. colors[result[*it]] = true;
  29. }
  30. }
  31. return result;
  32. }
  33.  
  34. vector<int> WPA(vector< set<int> > graph, int n){
  35. // modified implementation of Welsh-Powell algorithm
  36. priority_queue<pp, vector<pp> >pq;
  37. vector<bool> colors(n, true);
  38. vector<int> result(n*n,-1);
  39. for (int i=0; i<graph.size(); i++){
  40. pq.push(make_pair(graph[i].size(), i));
  41. }
  42.  
  43. result[pq.top().second] = 0;// color the first vertex with color 0
  44. pq.pop();
  45. while ( !pq.empty()){
  46. int i = pq.top().second;
  47. //cout << i <<endl;
  48. pq.pop();
  49. //cout << i << " ";
  50. set<int>::iterator it;
  51. for(it = graph[i].begin();it!=graph[i].end();it++)
  52. if (result[*it] != -1) // if the vertex is colored with a color then make the color unavailable
  53. colors[result[*it]] = false;// for coloring the current vertex
  54.  
  55. // find the first available color
  56. int clr = 0;
  57. for (; clr<n;clr++)
  58. if (colors[clr])
  59. break;
  60. result[i] = clr;// color the vertex with the first available color
  61. for(it = graph[i].begin();it!=graph[i].end();it++){
  62. // reset the available colors
  63. if (result[*it] != -1)
  64. colors[result[*it]] = true;
  65. }
  66. }
  67. return result;
  68. }
  69.  
  70. vector< vector<int> > makeGraph(int m, int n){
  71. // make the adjacency list of the graph
  72. vector< vector<int> > graph(n+1);
  73. for(int j = 0; j<m;j++){
  74. int u,v;
  75. cin >> u >> v;
  76. graph[u].push_back(v);
  77. graph[v].push_back(u);
  78. }
  79. return graph;
  80. }
  81.  
  82. vector < set <int> > makeBoard(){
  83. vector< set<int> >graph(s*s);
  84. for (int i=0;i<(s*s);i++){
  85. int r=(i/9);
  86. int c=(i%9);
  87. for(int j=0;j<s;j++){
  88. int v=(j*s)+c;
  89. if (v!=i)
  90. graph[i].insert(v);
  91. v=(i-c+j);
  92. if (v!=i)
  93. graph[i].insert(v);
  94. }
  95. int sr = (r-(r%3));
  96. int sc = (c-(c%3));
  97. for(int l=0;l<3;l++){
  98. int j=((sr+l)*s+sc);
  99. for(int k=0;k<3;k++){
  100. if ((j+k)!=i)
  101. graph[i].insert(j+k);
  102. }
  103. }
  104. }
  105. return graph;
  106. }
  107.  
  108. int main(){
  109. int n,m;
  110. //cin >> n >> m;
  111. //vector< vector<int> > graph = makeGraph(m,n);
  112. vector< set<int> > g = makeBoard();
  113. for(int i=0;i<g.size();i++){
  114. set<int>::iterator it;
  115. for(it = g[i].begin();it!=g[i].end();it++)
  116. cout << *it <<" ";
  117. cout << endl << i <<endl;
  118. }
  119. //vector<int> result = WPA(graph,n);
  120. vector<int> result = WPA(g,s);
  121.  
  122. for ( int i=0;i<(s*s);i++){
  123. cout <<"Vertex: "<< i << " -----> color: " <<result[i] << endl;
  124. }
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment