-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
76 lines (69 loc) · 1.65 KB
/
code_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
#define V 4
void printSolution(int color[]);
bool isSafe (int v, bool graph[V][V], int color[], int c) {
for (int i = 0; i < V; i++) {
if (graph[v][i] && c == color[i]) {
return false;
}
}
return true;
}
bool graphColoringUtil(bool graph[V][V], int m, int color[], int v) {
if (v == V) {
return true;
}
for (int c = 1; c <= m; c++) {
if (isSafe(v, graph, color, c)) {
color[v] = c;
if (graphColoringUtil (graph, m, color, v+1) == true) {
return true;
}
color[v] = 0;
}
}
return false;
}
bool graphColoring(bool graph[V][V], int m) {
int *color = new int[V];
for (int i = 0; i < V; i++) {
color[i] = 0;
}
if (graphColoringUtil(graph, m, color, 0) == false) {
printf("\nSolution does not exist\n");
return false;
}
printSolution(color);
return true;
}
void printSolution(int color[]) {
cout << "\nSolution Exists\n";
cout << "--------------------";
cout << "\nVertex\t|\tColor\n";
cout << "--------------------\n";
for (int i = 0; i < V; i++) {
cout << "\t" << (i+1) << "\t|\t" << color[i];
cout << endl;
}
cout << "--------------------\n";
}
int main() {
bool graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0},
};
int m = 3;
graphColoring (graph, m);
return 0;
}