Search
 
SCRIPT & CODE EXAMPLE
 

CPP

graph colouring

class GrpahColouring {
  public:
    int V = 4;
 
  bool isSafeToColor(int v, vector < vector < int >> & graphMatrix, vector < int > color, int c) {
    for (int i = 0; i < V; i++)
      if (graphMatrix[v][i] == 1 && c == color[i])
        return false;
    return true;
  }
 
  bool graphColorUtil(vector < vector < int >> & graphMatrix, int m, vector < int > color, int v) {
    if (v == V)
      return true;
 
    for (int i = 1; i <= m; i++) {
      if (isSafeToColor(v, graphMatrix, color, i)) {
        color[v] = i;
        if (graphColorUtil(graphMatrix, m, color, v + 1))
          return true;
        color[v] = 0;
      }
    }
    return false;
  }
 
  void printColoringSolution(int color[]) {
    cout << ("Color schema for vertices are: ") << endl;
    for (int i = 0; i < V; i++)
      cout << (color[i]) << endl;
  }
  bool graphColoring(vector < vector < int >> & graphMatrix, int m) {
    vector < int > color(V, 0);
 
    if (!graphColorUtil(graphMatrix, m, color, 0)) {
      cout << "Color schema not possible" << endl;
      return false;
    }
 
    printColoringSolution(color);
    return true;
  }
Comment

PREVIOUS NEXT
Code Example
Cpp :: end vs cend in cpp 
Cpp :: why the << operator is friend 
Cpp :: how to scan vector in c++ 
Cpp :: c++ file handiling 
Cpp :: Vaccine Dates codechef solution in c++ 
Cpp :: print all variables separated by comma c++ 
Cpp :: convert hex to decimal arduino 
Cpp :: person parametr cpp 
Cpp :: sort array in descending order c++ 
Cpp :: Mirror Inverse Program in c++ 
Cpp :: apertura file in c++ 
Cpp :: true false operator 
Cpp :: how to open file without override c++ 
Cpp :: strong number in c++ 
Cpp :: Opengl GLFW basic window 
Cpp :: +905344253752 
Cpp :: c++ require keyword 
Cpp :: how to point to next array using pointer c++ 
Cpp :: variadic template constructor matches better than copy constructor 
Cpp :: C++ Converting Celsius to Kelvin 
Cpp :: C++ meaning :: 
Cpp :: sfml hide message 
Cpp :: how to move your chrector in unity 
Cpp :: convert c program to c ++ online 
Cpp :: last element of a set in c++ 
Cpp :: qt unhandled exception handler 
Cpp :: c++ create a vecto 
Cpp :: composition namespaces c++ 
Cpp :: count substrings codechef solution in c++ 
Cpp :: c++ vector merge algorithm 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =