Advertisement
rdrewd

graphcolor.c

Apr 27th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. // A class that represents an undirected graph
  6. class Graph
  7.     {
  8.         int V; // No. of vertices
  9.         list<int> *adj; // A dynamic array of adjacency lists
  10.         public:
  11.             // Constructor and destructor
  12.             Graph(int V) { this->V = V; adj = new list<int>[V]; }
  13.             ~Graph() { delete [] adj; }
  14.  
  15.             // function to add an edge to graph
  16.             void addEdge(int v, int w);
  17.  
  18.             // Prints greedy coloring of the vertices
  19.             void greedyColoring();
  20.      };
  21.  
  22. void Graph::addEdge(int v, int w)
  23.      {
  24.         adj[v].push_back(w);
  25.         adj[w].push_back(v); // Note: the graph is undirected
  26.         }
  27.  
  28. // Assigns colors (starting from 0) to all vertices and prints
  29. // the assignment of colors
  30. void Graph::greedyColoring()
  31.     {
  32.         int result[V];
  33.  
  34.         // Assign the first color to first vertex
  35.         result[0] = 0;
  36.  
  37.         // Initialize remaining V-1 vertices as unassigned
  38.         for (int u = 1; u < V; u++)
  39.             result[u] = -1; // no color is assigned to u
  40.  
  41.         // A temporary array to store the available colors. True
  42.         // value of available[cr] would mean that the color cr is
  43.         // assigned to one of its adjacent vertices
  44.         bool available[V];
  45.         for (int cr = 0; cr < V; cr++)
  46.             available[cr] = false;
  47.  
  48.         // Assign colors to remaining V-1 vertices
  49.         for (int u = 1; u < V; u++)
  50.             {
  51.                 // Process all adjacent vertices and flag their colors
  52.                 // as unavailable
  53.                 list<int>::iterator i;
  54.                 for (i = adj[u].begin(); i != adj[u].end(); ++i)
  55.                     if (result[*i] != -1)
  56.                         available[result[*i]] = true;
  57.  
  58.                 // Find the first available color
  59.                 int cr;
  60.                 for (cr = 0; cr < V; cr++)
  61.                     if (available[cr] == false)
  62.                         break;
  63.  
  64.                 result[u] = cr; // Assign the found color
  65.  
  66.                 // Reset the values back to false for the next iteration
  67.                 for (i = adj[u].begin(); i != adj[u].end(); ++i)
  68.                         if (result[*i] != -1)
  69.                                 available[result[*i]] = false;
  70.             }
  71.  
  72.         // print the result
  73.         for (int u = 0; u < V; u++)
  74.             cout << "Vertex " << u << " ---> Color "
  75.                 << result[u] << endl;
  76.         }
  77.  
  78. // Driver program to test above function
  79. int main()
  80.     {
  81.         Graph g1(5);
  82.         g1.addEdge(0, 1);
  83.         g1.addEdge(0, 2);
  84.         g1.addEdge(1, 2);
  85.         g1.addEdge(1, 3);
  86.         g1.addEdge(2, 3);
  87.         g1.addEdge(3, 4);
  88.         cout << "Coloring of Graph 1 \n";
  89.         g1.greedyColoring();
  90.  
  91.         Graph g2(5);
  92.         g2.addEdge(0, 1);
  93.         g2.addEdge(0, 2);
  94.         g2.addEdge(1, 2);
  95.         g2.addEdge(1, 4);
  96.         g2.addEdge(2, 4);
  97.         g2.addEdge(4, 3);
  98.         cout << "\nColoring of Graph 2 \n";
  99.         g2.greedyColoring();
  100.  
  101.         return 0;
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement