import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.ArrayList;
import java.util.regex.Pattern;

import tutorial.MapStringList;

/*
 * This class create the hypergraph of the paper-author relationship: hyper[][]
 * paper is edge(rows) , author is vertex(columns)
 */
public class createGraph {
	
	public static void main(String[] args) {
		
		String[] result2;
		int[][] hyper = new int[360][314];
		Iterator itr;
		int size;
		int a, b;
		
		//hashtable keys are papers , key's values are this paper's authors
		MapStringList coauthor = new MapStringList();
		  //read the second file for author - paper relationship
		  try{
			  // Open the file that is the first 
			  // command line parameter
			  FileInputStream fstream2 = new FileInputStream("coauthor.txt");
			  // Get the object of DataInputStream
			  DataInputStream in2 = new DataInputStream(fstream2);
			  BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
			  String strLine2;
			  //Read File Line By Line
			  while ((strLine2 = br2.readLine()) != null)   
			  {
				  // Print the content on the console
				  //System.out.println (strLine2);
				  
				  // Create a pattern to match breaks
			      Pattern p = Pattern.compile("[\\s]+");
			      // Split input with the pattern
			      result2 = p.split(strLine2);
			      int temp = Integer.parseInt(result2[0]);
				  coauthor.add(result2[1], result2[0]);

			  }
			  //add hashtable : paper ---authors
			  //Close the input stream
			  in2.close();
			  }catch (Exception e){//Catch exception if any
			  System.err.println("Error: " + e.getMessage());
			  }
		  itr = (coauthor.getKeys()).iterator(); 
		  while(itr.hasNext()) {
				//get each key: element
			    Object element = itr.next(); 
			    size = coauthor.getValues(element.toString()).size();
			    //array has the coauthors of current author
			    Object[] array = (coauthor.getValues(element.toString())).toArray();
			    
			    // create the TWO DIMENSIONAL ARRAY of hypergraph
			    for(Object ip : array)
			    {
			    	b = Integer.parseInt(ip.toString());//author:column:vertex
			    	a = Integer.parseInt(element.toString());//paper:row:edge
			    	hyper[a-315][b-1] = 1;
			    	//System.out.print(a);
			    	//System.out.println(b);
			    }
		  }

		  determin c = new determin();
		  for (ArrayList<Integer> i: c.getHyperTriangleIndex(hyper))
		  {
			//  System.out.println(i);
		  }
		  //for (int i: c.getHyperStarIndex(hyper))
		  {
			  //System.out.println(i);
		  }
		  int[][] multiD = {
		    	      { 1,1,0,0,0},
		    	      { 1,0,0,1,0},
		    	      { 0,1,0,1,1},
		    	      {0,1,0,0,0}
		    	      };
		  boolean bb = c.determine(multiD, hyper);
		  System.out.print(bb);

		  /*
		   * test fullPermu();
		   
		  int[] l1 = {};
		  int[] l2 = {1,2,3};
		  Set<ArrayList<Integer>> permut= new HashSet<ArrayList<Integer>>();
		  permut = c.fullPermu(l1, l2);
		  System.out.print(permut);
		  
		   * END
		   */
		  
		  /*
		   * test Permutation();
		   		  
			ArrayList<Integer> testData = new ArrayList<Integer>(Arrays.asList(1, 2, 355, 4, 5, 6));
			ArrayList<ArrayList<Integer>> results = c.Permutation(testData,1);
			for(int i=0; i<results.size(); i++)
				System.out.println(results.get(i));
			  
			   * END
			   */

	}
}