0% found this document useful (0 votes)
13 views3 pages

Graph Coloring - Attempt Review 67 - 240513 - 195251

The document discusses a graph coloring problem to assign colors to graph nodes subject to constraints. It provides code to find the chromatic number for a 4 node graph initialized with values. The code uses backtracking and recursion to try all possible color combinations to find the minimum number of colors needed without conflicts.

Uploaded by

221401094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Graph Coloring - Attempt Review 67 - 240513 - 195251

The document discusses a graph coloring problem to assign colors to graph nodes subject to constraints. It provides code to find the chromatic number for a 4 node graph initialized with values. The code uses backtracking and recursion to try all possible color combinations to find the minimum number of colors needed without conflicts.

Uploaded by

221401094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5/7/24, 12:50 PM Graph Coloring: Attempt review

Dashboard / My courses / CB19541-AAD-2022 / Backtracking / Graph Coloring

Started on Tuesday, 30 April 2024, 12:34 PM


State Finished
Completed on Tuesday, 30 April 2024, 12:39 PM
Time taken 5 mins 17 secs
Marks 1.00/1.00
Grade 10.00 out of 10.00 (100%)
Name PRITHIV G 2022-CSBS-B

https://www.rajalakshmicolleges.net/moodle/mod/quiz/review.php?attempt=770062&cmid=6816 1/3
5/7/24, 12:50 PM Graph Coloring: Attempt review

Question 1

Correct

Mark 1.00 out of 1.00

Graph Coloring problem is to assign colors to certain elements of a graph subject to certain constraints.

Graph nodes with initialization as { { 0,1,0,1},{1,0,1,0},{0,1,0,1},{1,0,1,0}}.Consider the four nodes and find the chromatic number for it.

Sample Output:
Node 1 Assigned with color 0
Node 2 Assigned with color 1
Node 3 Assigned with color 0
Node 4 Assigned with color 1

Chromatic Number to color the four node is : 2

Answer: (penalty regime: 0 %)


1 #include <stdio.h>
2 #include <stdbool.h>
3
4 #define V 4
5 ▼ bool isSafe(int v, int graph[V][V], int color[], int c) {
6 for (int i = 0; i < V; i++)
7 if (graph[v][i] && c == color[i])
8 return false;
9 return true;
10 }
11
12
13 ▼ bool graphColoringUtil(int graph[V][V], int m, int color[], int v) {
14 if (v == V)
15 return true;
16
17 ▼ for (int c = 0; c < m; c++) {
18 ▼ if (isSafe(v, graph, color, c)) {
19 color[v] = c;
20 if (graphColoringUtil(graph, m, color, v + 1))
21 return true;
22 color[v] = -1;
23 }
24 }
25 return false;
26 }
27
28
29 ▼ void graphColoring(int graph[V][V], int m) {
30 int color[V];
31 for (int i = 0; i < V; i++)
32 color[i] = -1;
33
34 ▼ if (!graphColoringUtil(graph, m, color, 0)) {
35 printf("Solution does not exist");
36 return;
37 }
38
39
40
41 for (int i = 0; i < V; i++)
42 printf("Node %d Assigned with color %d\n", i + 1, color[i]);
43
44
45 int chromatic_number = 0;
46 for (int i = 0; i < V; i++)
47 if (color[i] > chromatic_number)
https://www.rajalakshmicolleges.net/moodle/mod/quiz/review.php?attempt=770062&cmid=6816 2/3
5/7/24, 12:50 PM Graph Coloring: Attempt review
( )
48 chromatic_number = color[i];
49 printf("Chromatic Number to color the four node is : %d\n", chromatic_number +
50 }
51
52
53 ▼ int main() {
54 ▼ int graph[V][V] = {
55 {0, 1, 0, 1},
56 {1, 0, 1, 0},
57 {0, 1, 0, 1},
58 {1, 0, 1, 0}
59 };
60 int m = 2;
61 graphColoring(graph, m);
62 return 0;
63 }

Test Expected Got

 1 Node 1 Assigned with color 0 Node 1 Assigned with color 0 


Node 2 Assigned with color 1 Node 2 Assigned with color 1
Node 3 Assigned with color 0 Node 3 Assigned with color 0
Node 4 Assigned with color 1 Node 4 Assigned with color 1
Chromatic Number to color the four node is : 2 Chromatic Number to color the four node is : 2

Passed all tests! 

Correct
Marks for this submission: 1.00/1.00.

◄ Sum of All Subset XOR Totals

Jump to...

https://www.rajalakshmicolleges.net/moodle/mod/quiz/review.php?attempt=770062&cmid=6816 3/3

You might also like