1. All M^V Color Configurations Enumeration
Intuition
Generate all MV possible color assignments using recursion. Check each complete configuration for edge conflicts.
Algorithm
- Recursively assign every possible color 0 to M - 1 to each vertex from 0 to V - 1.
- When all V vertices have a color assigned:
- Check every edge (u, v): if color[u] == color[v], configuration is invalid.
- If all edges valid, return the configuration.
- If no configuration satisfies the condition, return false.
#include <iostream>#include <vector> using namespace std; bool isValid(const vector<vector<int>>& edges, const vector<int>& color) { for (const auto& e : edges) { if (color[e[0]] == color[e[1]]) return false; } return true;} bool generateAll(int u, int V, int M, const vector<vector<int>>& edges, vector<int>& color) { if (u == V) { return isValid(edges, color); } for (int c = 0; c < M; ++c) { color[u] = c; if (generateAll(u + 1, V, M, edges, color)) return true; } return false;} int main() { int V = 4, M = 3; vector<vector<int>> edges = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {2, 3}}; vector<int> color(V, -1); if (generateAll(0, V, M, edges, color)) { cout << "Color assignment: "; for (int c : color) cout << c << " "; cout << endl; } else { cout << "No valid coloring found" << endl; } return 0;}Time & Space Complexity
- Time complexity:
O(M^V * E) - Space complexity:
O(V)