mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-10 22:18:06 +08:00
make he code neat and clean without global variables
This commit is contained in:
@@ -1,18 +1,36 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief brief one line description here
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
* Number of vertices in the graph
|
||||
// created a namespace to ensure all globals and functions are
|
||||
// restricted to this file
|
||||
namespace {
|
||||
/** A utility function to print solution
|
||||
* @param color description
|
||||
* @param V number of vertices in the graph
|
||||
*/
|
||||
#define V 4
|
||||
void printSolution(const int *color, int V) {
|
||||
std::cout << "Following are the assigned colors\n";
|
||||
for (int i = 0; i < V; i++) {
|
||||
std::cout << color[i];
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Prototypes
|
||||
/** A utility function to check if the current color assignment is safe for
|
||||
* vertex v
|
||||
* @tparam V number of vertices in the graph
|
||||
* @param v description
|
||||
* @param graph description
|
||||
* @param color description
|
||||
* @param c description
|
||||
* @returns `true` if ....
|
||||
* @returns `false` if ....
|
||||
*/
|
||||
void printSolution(int color[]);
|
||||
|
||||
/* A utility function to check if the current color assignment
|
||||
is safe for vertex v */
|
||||
bool isSafe(int v, bool graph[V][V], int color[], int c) {
|
||||
template <int V>
|
||||
bool isSafe(int v, const bool graph[V][V], const int *color, int c) {
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (graph[v][i] && c == color[i]) {
|
||||
return false;
|
||||
@@ -21,12 +39,20 @@ bool isSafe(int v, bool graph[V][V], int color[], int c) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* A recursive utility function to solve m coloring problem */
|
||||
void graphColoring(bool graph[V][V], int m, int color[], int v) {
|
||||
/* A recursive utility function to solve m coloring problem
|
||||
* @tparam V number of vertices in the graph
|
||||
* @param graph description
|
||||
* @param m description
|
||||
* @param [in,out] color description // used in,out to notify in documentation
|
||||
* that this parameter gets modified by the function
|
||||
* @param v description
|
||||
*/
|
||||
template <int V>
|
||||
void graphColoring(bool graph[V][V], int m, int *color, int v) {
|
||||
// base case:
|
||||
// If all vertices are assigned a color then return true
|
||||
if (v == V) {
|
||||
printSolution(color);
|
||||
printSolution(color, V);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -44,16 +70,7 @@ void graphColoring(bool graph[V][V], int m, int color[], int v) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* A utility function to print solution */
|
||||
void printSolution(int color[]) {
|
||||
std::cout << "Following are the assigned colors\n";
|
||||
for (int i = 0; i < V; i++) {
|
||||
std::cout << color[i];
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
/**
|
||||
* Main function
|
||||
@@ -66,6 +83,7 @@ int main() {
|
||||
// | / |
|
||||
// (0)---(1)
|
||||
|
||||
const int V = 4; // number of vertices in the graph
|
||||
bool graph[V][V] = {
|
||||
{0, 1, 1, 1},
|
||||
{1, 0, 1, 0},
|
||||
|
||||
Reference in New Issue
Block a user