

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Demonstrate the Implementation of 4-Color Problem
This is a C++ Program to Demonstrate the Implementation of 4-Color Problem.
Algorithm
Begin Develop function issafe() to check if the current color assignment is safe for vertex v i.e. checks whether the edge exists or not. If it exists, then next check whether the color to be filled in the new vertex is already used by its adjacent vertices. End Begin Function graphColoringtil(bool graph[V][V], int m, int col[], int v) solve 4 coloring problem: Here, g[V][V] = It is a 2D array where V is the number of vertices in graph m = maximum number of colors that can be used. col[] = an color array that should have numbers from 1 to m. if v == V return true For c = 1 to m if (isSafe(v, g, col, c)) col[v] = c if (graphColoringtil (g, k, col, v+1) == true) return true col[v] = 0 return false End Begin function graphColor(): It mainly uses graphColoringUtil() to solve the problem. It returns false if the m colors cannot be assigned, otherwise return true. End
Example
#include <iostream> #include <cstdio> #define V 5 using namespace std; bool isSafe (int v, bool graph[V][V], int col[], int C) { for (int i = 0; i < V; i++) if (graph[v][i] && C == col[i]) return false; return true; } bool graphColoringtil(bool g[V][V], int k, int col[], int v) { if (v == V) //If all vertices are assigned a color then return true; for (int c = 1; c <= k; c++) { //Consider this vertex v and try different colors if (isSafe(v, g, col, c)) { //Check if assignment of color c to v is fine col[v] = c; if (graphColoringtil (g, k, col, v+1) == true) //recur to assign colors to rest of the vertices return true; col[v] = 0; //If assigning color c doesn't lead to a solution then remove it } } return false; } void solution(int color[]) { cout<<"The assigned colors are: \n"; for (int i = 0; i < V; i++) cout<<color[i]; cout<<"\n"; } bool graphColor(bool graph[V][V], int k) { int *color = new int[V]; //initialize all colors value as 0 for (int i = 0; i < V; i++) color[i] = 0; if (graphColoringtil(graph, k, color, 0) == false) { cout<<"Solution does not exist"; return false; } solution(color); return true; } int main() { bool g[V][V] = { {0, 0, 1, 0,1}, {1, 1, 1, 0,0}, {1, 1, 0, 0,1}, {0, 1, 1, 0,0} }; int k= 4; graphColor(g, k); return 0; }
Output
The assigned colors are: 1 2 3 1 1
- Related Questions & Answers
- Demonstrate a basic implementation of ‘tf.keras.layers.Dense’ in Python
- Bootstrap 4 .flex-*-row class implementation
- Bootstrap 4 .flex-*-fill class implementation
- Bootstrap 4 .flex-*-wrap class implementation
- Bootstrap 4 .d-flex class implementation
- C++ program to demonstrate function of macros
- Bootstrap 4 .flex-*-row-reverse class implementation
- Bootstrap 4 .flex-column-reverse class implementation
- C++ program to demonstrate exception handling
- C program to demonstrate usage of variable-length arrays
- C program to demonstrate fork() and pipe()
- C++ program to demonstrate multi-level inheritance
- Add a grey background color to the Bootstrap 4 card
- Add a light grey background color to the Bootstrap 4 card
- C++ Program to Solve the Fractional Knapsack Problem
Advertisements