package graph;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by Paweł Kopeć on 30.12.16.
 *
 * VertexColoring class provides an interface
 * for assigning colors to graphBase's vertices
 * and dividing them into color classes.
 */
public class VertexColoring extends VertexAssignment {

    public static int NO_COLOR = 0;

    private int[] colors;
    private HashMap<Integer, Integer> colorSizes;

    public VertexColoring(Graph graph) {
        super(graph);
        colors = new int[graph.getVertices()];
        colorSizes = new HashMap<>();
        colorSizes.put(0, graph.getVertices());
    }

    /**
     * Set color of a given vertex.
     *
     * @param index of a vertex to be colored
     * @param color that is to be assigned to a vertex
     */
    public void set(int index, int color) {
        graph.validateVertex(index);

        colorSizes.put(colors[index], colorSizes.get(colors[index]) - 1);
        colors[index] = color;

        if(colorSizes.get(color) == null) {
            colorSizes.put(color, 1);
        }
        else {
            colorSizes.put(color, colorSizes.get(color) + 1);
        }
    }

    /**
     * Get color of a given vertex.
     * Equals zero by default.
     *
     * @param index of a given vertex
     * @return color of a given vertex
     */
    public int get(int index) {
        graph.validateVertex(index);
        return colors[index];
    }

    /**
     * Get number of colors in this graphBase.
     *
     * @return number of colors
     */
    public int getColorsNumber() {
        return colorSizes.size();
    }

    /**
     * Get number of vertices that are assigned given color.
     *
     * @param color of vertices to look for
     * @return number of vertices in this color
     */
    public int getNumberOfColored(int color) {
        return colorSizes.containsKey(color) ? colorSizes.get(color) : 0;
    }

    public boolean isProper() {
        for (int i = 0; i < graph.getVertices(); i++) {
            for (Integer neighbour : graph.getNeighbours(i)) {
                if (colors[i] == colors[neighbour]) {
                    return false;
                }
            }
        }

        return true;
    }

    public int[] getColors() {
        return colors.clone();
    }

    public Graph getGraph() {
        return graph;
    }

    public String toString() {
        return Arrays.toString(colors);
    }
}

@@ -59,16 +59,42 @@
 HashMap;
+%0Aimport java.util.Scanner;
 %0A%0A/**%0A *
@@ -637,32 +637,317 @@
 ices());%0A    %7D%0A%0A
+    public VertexColoring(Graph graph, String s) %7B%0A        this(graph);%0A        scan(s);%0A    %7D%0A%0A    private void scan(String s) %7B%0A        Scanner scanner = new Scanner(s);%0A%0A        for (int i = 0; i %3C graph.getVertices(); i++) %7B%0A            set(i, scanner.nextInt());%0A        %7D%0A    %7D%0A%0A
     /**%0A     * S

add scan()