Greedy Coloring Algorithm (Recap)
Greedy Coloring Algorithm (Recap)
Here's a clear and simple explanation of your **graph coloring code** using
**backtracking**:
---
---
---
#### 🔹 Step-by-step:
1. Loop through each color `c` from 1 to `m`.
2. Check if it is **safe to assign color `c` to vertex `k`** using `isSafe()`.
3. If safe:
- Assign color `c` to vertex `k`: `x[k] = c`.
- If more vertices remain (`k + 1 < n`), **recurse** to color the next vertex:
`graphColour(k + 1)`.
- If all vertices are colored (`k+1 == n`), **print the solution**.
4. If no color is safe, it **backtracks** and tries a different color.
---
#### 🔹 How:
- For each other vertex `i`, if:
- Vertex `i` is **adjacent** to `k` (`G[k][i] == 1`)
- AND `i` already has the same color `c` (`x[i] == c`)
→ Then it's **not safe**, return `false`.
---
### ✅ **Example:**
If you try to color a triangle graph (3 nodes all connected), this function will:
- Try different colors for each node.
- Reject any assignment where two connected nodes have the same color.
- Eventually find and print a valid coloring.
---
Let me know if you'd like to see this explained with a diagram or implemented in a
specific language (like Python or C++)!