import fi.uef.cs.tra.DiGraph;
import fi.uef.cs.tra.Vertex;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class TRAII_25_t11_12_skeleton {

    public static void main(String[] args) {

        // defaults
        int vertices = 5;
        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+5;    // change here for different graphs

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


        // Random graph
        DiGraph graph = GraphMaker.createDiGraph(vertices, edges, seed);
        System.out.println("\nGraph (numbers are vertices, letters are edges):");
        System.out.println(graph);

        System.out.println("\nFollowers for each vertex:");
        for (Vertex v : graph.vertices()) {
            System.out.println(v + " : " + setOfFollowers(graph, v));
        }

        int maxPaths = 15;
        System.out.println("\nPaths:");
        for (Vertex v1 : graph.vertices()) {
            for (Vertex v2 : graph.vertices()) {
                if (v1 == v2)
                    continue;
                System.out.println("" + v1 + "->" + v2 + " : " + somePath(graph, v1, v2));
                if (maxPaths-- <= 0)
                    break;
            }
        }
        
    } // main()


    /**
     * Set of followers of a vertex v.
     * Followers of v are such vertices w that there exists a path from v to w.
     * @param G input graph
     * @param vertex starting vertex
     * @return set of all vertices to which there exists a path from v
     */
    static Set<Vertex> setOfFollowers(DiGraph G, Vertex vertex) {
        GraphMaker.color(G, DiGraph.WHITE);

        Set<Vertex> result = new HashSet<>();

        // TODO

        return result;
    }


    /**
     * Some path from vertex start to vertex end
     * Versio joka rakentaa polkua rekursiossa edetessä (ja purkaa jollei maalia löydy)
     * @param G tarkasteltava verkko (tarvitaan pohjaväritykseen)
     * @param start polun alkusolmu
     * @param end polun loppusolmu
     * @return lista polun solmuista, tai tyhjä lista jollei polkua ole olemassa
     */
    static List<Vertex> somePath(DiGraph G, Vertex start, Vertex end) {

        GraphMaker.color(G, DiGraph.WHITE);
        List<Vertex> result = new LinkedList<>();

        // TODO

        return result;
    }


    // example / skeleton code

    /**
     * DFS (whithout doing anything)
     *
     * @param G graph to traverse
     */
    static void dfsStart(DiGraph G) {
        for (Vertex v : G.vertices())                // set all vertices white
            v.setColor(DiGraph.WHITE);
        for (Vertex v : G.vertices())                // start from unvisited verteices
            if (v.getColor() == DiGraph.WHITE)
                dfsRecursion(v);
    }

    /**
     * DSF from vertex v
     *
     * @param v vertex in process
     */
    static void dfsRecursion(Vertex v) {
        // here the operation for vertex v (if preorder)
        v.setColor(DiGraph.GRAY);
        for (Vertex w : v.neighbors())                // unvisited neighbors
            if (w.getColor() == DiGraph.WHITE)
                dfsRecursion(w);
        // here the operation for vertex v (if postorder)
    }



}
