forked from MainakRepositor/500-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8ca187
commit 275d538
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Number of vertices in the graph | ||
#define V 4 | ||
|
||
void printSolution(int color[]); | ||
|
||
// check if the colored | ||
// graph is safe or not | ||
bool isSafe(bool graph[V][V], int color[]) | ||
{ | ||
// check for every edge | ||
for (int i = 0; i < V; i++) | ||
for (int j = i + 1; j < V; j++) | ||
if (graph[i][j] && color[j] == color[i]) | ||
return false; | ||
return true; | ||
} | ||
|
||
/* This function solves the m Coloring | ||
problem using recursion. It returns | ||
false if the m colours cannot be assigned, | ||
otherwise, return true and prints | ||
assignments of colours to all vertices. | ||
Please note that there may be more than | ||
one solutions, this function prints one | ||
of the feasible solutions.*/ | ||
bool graphColoring(bool graph[V][V], int m, int i, | ||
int color[V]) | ||
{ | ||
// if current index reached end | ||
if (i == V) { | ||
|
||
// if coloring is safe | ||
if (isSafe(graph, color)) { | ||
|
||
// Print the solution | ||
printSolution(color); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// Assign each color from 1 to m | ||
for (int j = 1; j <= m; j++) { | ||
color[i] = j; | ||
|
||
// Recur of the rest vertices | ||
if (graphColoring(graph, m, i + 1, color)) | ||
return true; | ||
|
||
color[i] = 0; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/* A utility function to print solution */ | ||
void printSolution(int color[]) | ||
{ | ||
cout << "Solution Exists:" " Following are the assigned colors \n"; | ||
for (int i = 0; i < V; i++) | ||
cout << " " << color[i]; | ||
cout << "\n"; | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
/* Create following graph and | ||
test whether it is 3 colorable | ||
(3)---(2) | ||
| / | | ||
| / | | ||
| / | | ||
(0)---(1) | ||
*/ | ||
bool graph[V][V] = { | ||
{ 0, 1, 1, 1 }, | ||
{ 1, 0, 1, 0 }, | ||
{ 1, 1, 0, 1 }, | ||
{ 1, 0, 1, 0 }, | ||
}; | ||
int m = 3; // Number of colors | ||
|
||
// Initialize all color values as 0. | ||
// This initialization is needed | ||
// correct functioning of isSafe() | ||
int color[V]; | ||
for (int i = 0; i < V; i++) | ||
color[i] = 0; | ||
|
||
if (!graphColoring(graph, m, 0, color)) | ||
cout << "Solution does not exist"; | ||
|
||
return 0; | ||
} | ||
|