![]() |
VOOZH | about |
Given a graph with N vertices and E edges. The edges are given as U[] and V[] such that for each index i, U[i] is connected to V[i]. The task is to find the minimum number of colors needed to color the given graph.
Examples
Input: N = 5, M = 6, U[] = { 1, 2, 3, 1, 2, 3 }, V[] = { 3, 3, 4, 4, 5, 5 };
Output: 3
Explanation:
For the above graph node 1, 3, and 5 cannot have the same color. Hence the count is 3.
Approach:
We will keep two array count[] and colors[]. The array count[] will store the count of edges for each node and colors[] will store the colors of each node. Initialize count for every vertex to 0 and color for every vertex to 1.
Steps:
Below is the implementation of the above approach:
3
Time Complexity: O(N+E), where N = number of vertices and E = Number of edges