Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1083 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 444 |

@SHOAIBVIJAPURE | 430 |

@codeantik | 429 |

@shalinibhataniya1097 | 400 |

@ShamaKhan1 | 392 |

@neverevergiveup | 372 |

@amrutakashikar2 | 355 |

@murarry3625 | 350 |

@mahlawatep | 349 |

Complete Leaderboard |

Given an undirected graph and an integer **M**. The task is to determine if the graph can be colored with at most M colors such that no two adjacent vertices of the graph are colored with the same color. Here coloring of a graph means the assignment of colors to all vertices. Print 1 if it is possible to colour vertices and 0 otherwise.

**Example 1:**

**Input:
**N = 4
M = 3
E = 5
Edges[] = {(1,2),(2,3),(3,4),(4,1),(1,3)}
**Output: **1**
Explanation: **It is possible to colour the
given graph using 3 colours.

**Example 2:**

**Input:
**N = 3
M = 2
E = 3
Edges[] = {(1,2),(2,3),(1,3)}
**Output: **0

**Your Task:**

Your task is to complete the function **graphColoring()** which takes the 2d-array graph[], the number of colours and the number of nodes as inputs and returns **true** if answer exists otherwise **false**. 1 is printed if the returned value is **true, **0 otherwise. The printing is done by the driver's code.

**Note**: In the given 2d-array graph[], if there is an edge between vertex X and vertex Y graph[] will contain 1 at graph[X-1][Y-1], else 0. In 2d-array graph[ ], nodes are 0-based indexed, i.e. from 0 to N-1.

**Expected Time Complexity:** O(M^{N}).

**Expected Auxiliary ****Space:** O(N).

**Constraints:**

1 <= N <= 20

1 <= E <= (N*(N-1))/2

1 <= M <= N

**Note: **The given inputs are 1-base indexed but you have to consider the graph given in the adjacency matrix/list as 0-base indexed.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
M-Coloring Problem

...