import fi.uef.cs.tra.AbstractGraph;
import fi.uef.cs.tra.Graph;
import fi.uef.cs.tra.Vertex;


public class TRAII_25_t16_skeleton {

    public static void main(String[] args) {

        // defaults
        int vertices = 7;
        int edges = 7;

        if (args.length > 0)
            vertices = Integer.parseInt(args[0]);

        if (args.length > 1)
            edges = Integer.parseInt(args[1]);

        int seed = vertices+edges;

        if (args.length > 2)
            seed = Integer.parseInt(args[2]);

        Graph graph;


        System.out.println("\nGraph1: ");
        graph = GraphMaker.createGraph(vertices, edges, seed);
        System.out.print(GraphMaker.toString(graph, 0));

        System.out.print("Connected: ");
        boolean isConn = isConnected(graph);
        System.out.println(isConn + "\n");
        if (! isConn) {   // call task 16
            System.out.println("Augmentation:");
            augmentConnected(graph);
            System.out.print(GraphMaker.toString(graph, 0));
        }

        isConn = isConnected(graph);
        System.out.println("Connected now: " + isConn);

         }

    /**
     * Augment unconnected graph to connected by adding edges between components.
     * If graph was already connected, do nothing.
     * @param G input graph
     */
    static void augmentConnected(Graph G) {

        // TODO

    }   // augmentConnected()



    /**
     * dfs with color
     * @param v starting vertex
     * @param color to use
     */
    static void dfsColor(Vertex v, int color) {
        v.setColor(color);

        for (Vertex w : v.neighbors())
            if (w.getColor() != color)
                dfsColor(w, color);
    }

    /**
     * Color of all vertices
     * @param G graph
     * @param c color to use
     */
    static void color(AbstractGraph G, int c) {
        for (Vertex v : G.vertices())
            v.setColor(c);
    }


    /**
     * Is given graph connected or not?
     * @param G input graph
     * @return true if graph is connected, false otherwise
     */
    static boolean isConnected(Graph G) {
        // all white
        color(G, Graph.WHITE);

        // dfs from some vertex
        Vertex w = G.firstVertex();
        dfsColor(w, Graph.BLACK);

        // are there still white vertices?
        for (Vertex v : G.vertices())
            if (v.getColor() == Graph.WHITE)
                return false;
        return true;
    }



}
