Search
 
SCRIPT & CODE EXAMPLE
 

CPP

graph colouring backtracking

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 :: ue4 log 
Cpp :: C++ Ranged Based for Loop 
Cpp :: how to define global array in c++ in a scope 
Cpp :: windows install cppcheck 
Cpp :: Swap given nodes in a Doubly Linked List without modifying data 
Cpp :: Magical Doors codechef solution in c++ 
Cpp :: codeforces Pangram in c++ 
Cpp :: how to find the mean and standard deviation of trqiing dataset in pytorch 
Cpp :: 1603. Design Parking System leetcode solution in c++ 
Cpp :: nested loop c++ program example 
Cpp :: 2dvector c++ 
Cpp :: c++ else 
Cpp :: multiply two arbitrary integers a and b (a greater than b) 
Cpp :: compile c++ MPI Program 
Cpp :: sieve of eratosthenes c++ 
Cpp :: c++ auto loop 
Cpp :: how to convert n space separated integers in c++ 
Cpp :: return function in cpp 
Cpp :: c++ define constant 
Cpp :: imgui menu bar 
Cpp :: what does for do in c++ 
C :: c colour text 
C :: how to set a pointer to an offset in c 
C :: sstf program in c 
C :: how to print helloq world in c 
C :: random number c 
C :: how to add two numbers in c programming 
C :: linear search program in c 
C :: string input in c 
C :: xor swap 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =