package javakurs.musterloesung.uebung09.aufgabe1;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import javakurs.musterloesung.uebung08.DirectedGraph;
import javakurs.musterloesung.uebung08.DirectedGraphTest;
import javakurs.musterloesung.uebung08.VertexPropertyMap;

/**
 * {@link TarjanVisitorTest} is a test class for {@link TarjanVisitor}.
 *
 * @author Joerg Steffen, DFKI
 * @version $Id: TarjanVisitorTest.java,v 1.1 2011/01/10 16:29:58 steffen Exp $
 */
public final class TarjanVisitorTest {

  /**
   * This would create a new instance of {@link TarjanVisitorTest}. Not to be
   * used.
   */
  private TarjanVisitorTest() {

    // private constructor to enforce noninstantiability
  }


  /**
   * Use this example to test your Tarjan visitor.
   *
   * @return the example graph or {@code null} if an error appeared
   */
  public static DirectedGraph<String> getExampleGraphTarjan() {

    String graphString =
      "s --> w z\n"
        + "z --> w y\n"
        + "v --> w s\n"
        + "w --> x q\n"
        + "t --> u v\n"
        + "u --> t v\n"
        + "x --> z\n"
        + "q --> x\n"
        + "y --> x\n";

    DirectedGraph<String> graph = null;
    try {
      graph = DirectedGraphTest.readGraph(new StringReader(graphString));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return graph;
  }


  /**
   * This is the main method. It requires no arguments.
   *
   * @param args
   *          the arguments; not used here
   */
  public static void main(String[] args) {

    // get example graph
    DirectedGraph<String> graph = getExampleGraphTarjan();

    // use DFS with Tarjan SCC visitor
    TarjanVisitor<String> visitor = new TarjanVisitor<String>();
    graph.dfs(visitor);

    // print resulting SCCs
    VertexPropertyMap<String> names =
        (VertexPropertyMap<String>)graph.getPropertyMap("names");
    int counter = 0;
    for (List<Integer> comp : visitor.getSCCs()) {
      System.out.print("Component " + ++counter + ": ");
      for (Integer v : comp) {
        System.out.print(" " + names.get(v));
      }
      System.out.println();
    }
  }
}
