Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ChromaticNumber(Graph G)
- {
- for (int i = 0; i < G.Vertices.size(); ++i)
- {
- //initialize vertex color to 1
- ++G.Vertices[i].Color;
- bool Match = true;
- //loop while there is an adjacent vertex with the same color
- while (Match)
- {
- Match = false;
- for (int j = 0; j < G.Vertices[i].Edges.size(); ++j)
- {
- //get adjacent vertex
- int AdjacentVertex = G.Vertices[i].Edges[j];
- //if an adjacent vertex has the same color, increment the
- //current vertex's color
- if (G.Vertices[i].Color == G.Vertices[AdjacentVertex].Color)
- {
- ++G.Vertices[i].Color;
- Match = true;
- }
- }
- }
- }
- int MaxColor = 0;
- //loop to determine the largest color value
- for (int i = 0; i < G.Vertices.size(); ++i)
- if (G.Vertices[i].Color > MaxColor)
- MaxColor = G.Vertices[i].Color;
- return MaxColor;
- }
Add Comment
Please, Sign In to add comment