|
| 1 | +// A Java program to implement greedy algorithm for graph coloring |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +import java.util.LinkedList; |
| 5 | + |
| 6 | +// This class represents an undirected graph using adjacency list |
| 7 | +class Graph |
| 8 | +{ |
| 9 | + private int V; // No. of vertices |
| 10 | + private LinkedList<Integer> adj[]; //Adjacency List |
| 11 | + |
| 12 | + //Constructor |
| 13 | + Graph(int v) |
| 14 | + { |
| 15 | + V = v; |
| 16 | + adj = new LinkedList[v]; |
| 17 | + for (int i=0; i<v; ++i) |
| 18 | + adj[i] = new LinkedList(); |
| 19 | + } |
| 20 | + |
| 21 | + //Function to add an edge into the graph |
| 22 | + void addEdge(int v,int w) |
| 23 | + { |
| 24 | + adj[v].add(w); |
| 25 | + adj[w].add(v); //Graph is undirected |
| 26 | + } |
| 27 | + |
| 28 | + // Assigns colors (starting from 0) to all vertices and |
| 29 | + // prints the assignment of colors |
| 30 | + void greedyColoring() |
| 31 | + { |
| 32 | + int result[] = new int[V]; |
| 33 | + |
| 34 | + // Initialize all vertices as unassigned |
| 35 | + Arrays.fill(result, -1); |
| 36 | + |
| 37 | + // Assign the first color to first vertex |
| 38 | + result[0] = 0; |
| 39 | + |
| 40 | + // A temporary array to store the available colors. False |
| 41 | + // value of available[cr] would mean that the color cr is |
| 42 | + // assigned to one of its adjacent vertices |
| 43 | + boolean available[] = new boolean[V]; |
| 44 | + |
| 45 | + // Initially, all colors are available |
| 46 | + Arrays.fill(available, true); |
| 47 | + |
| 48 | + // Assign colors to remaining V-1 vertices |
| 49 | + for (int u = 1; u < V; u++) |
| 50 | + { |
| 51 | + // Process all adjacent vertices and flag their colors |
| 52 | + // as unavailable |
| 53 | + Iterator<Integer> it = adj[u].iterator() ; |
| 54 | + while (it.hasNext()) |
| 55 | + { |
| 56 | + int i = it.next(); |
| 57 | + if (result[i] != -1) |
| 58 | + available[result[i]] = false; |
| 59 | + } |
| 60 | + |
| 61 | + // Find the first available color |
| 62 | + int cr; |
| 63 | + for (cr = 0; cr < V; cr++){ |
| 64 | + if (available[cr]) |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + result[u] = cr; // Assign the found color |
| 69 | + |
| 70 | + // Reset the values back to true for the next iteration |
| 71 | + Arrays.fill(available, true); |
| 72 | + } |
| 73 | + |
| 74 | + // print the result |
| 75 | + for (int u = 0; u < V; u++) |
| 76 | + System.out.println("Vertex " + u + " ---> Color " |
| 77 | + + result[u]); |
| 78 | + } |
| 79 | + |
| 80 | + // Driver method |
| 81 | + public static void main(String args[]) |
| 82 | + { |
| 83 | + Graph g1 = new Graph(5); |
| 84 | + g1.addEdge(0, 1); |
| 85 | + g1.addEdge(0, 2); |
| 86 | + g1.addEdge(1, 2); |
| 87 | + g1.addEdge(1, 3); |
| 88 | + g1.addEdge(2, 3); |
| 89 | + g1.addEdge(3, 4); |
| 90 | + System.out.println("Coloring of graph 1"); |
| 91 | + g1.greedyColoring(); |
| 92 | + |
| 93 | + System.out.println(); |
| 94 | + Graph g2 = new Graph(5); |
| 95 | + g2.addEdge(0, 1); |
| 96 | + g2.addEdge(0, 2); |
| 97 | + g2.addEdge(1, 2); |
| 98 | + g2.addEdge(1, 4); |
| 99 | + g2.addEdge(2, 4); |
| 100 | + g2.addEdge(4, 3); |
| 101 | + System.out.println("Coloring of graph 2 "); |
| 102 | + g2.greedyColoring(); |
| 103 | + } |
| 104 | +} |
| 105 | +// This code is contributed by Aakash Hasija |
0 commit comments