import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.*;

public class EdgesToNet
{
	//Command line arguments
	private static String edgeFile = "";
	private static String outputFile = "";

	private static Hashtable ids = new Hashtable(500000);

	public static void main(String[] argv) throws FileNotFoundException, IOException
	{

	  // Get command line arguments
          if (argv != null) {
                int len = argv.length;
                if (len == 2) {
                    edgeFile = argv[0];
                    outputFile = argv[1];
                }
	        else printUsage();
           }

       BufferedReader pReader = new BufferedReader(new FileReader(edgeFile));
       String pLine = null;
       pLine = pReader.readLine();
       int c=1;
       while (pLine!=null)
       {
       		String[] strs = split(pLine,'\t');
		if (strs.length<2) System.out.println(pLine);
       		if (!ids.containsKey(strs[0]))
       		{
	           	ids.put(strs[0], new Integer(c++));
       		}
       		if (!ids.containsKey(strs[1]))
       		{
	           	ids.put(strs[1], new Integer(c++));
       		}
       		pLine = pReader.readLine();
       }
       pReader.close();

	//Output file
    FileOutputStream outFile=new FileOutputStream(outputFile);
    DataOutputStream outData=new DataOutputStream(outFile);
    Enumeration idsen;

	outData.writeBytes("*Network "+edgeFile+"\n");

	outData.writeBytes("*vertices "+ids.size()+"\n");

	for (idsen = ids.keys();idsen.hasMoreElements();)
	{
		String id = (String)idsen.nextElement();
		int index = ((Integer)(ids.get(id))).intValue();
		outData.writeBytes(index+" \""+id+"\" ellipse\n");
	}

	// Undirected .net
	//outData.writeBytes("*edges\n");
	// Directed .net
	outData.writeBytes("*arcs\n");

    pReader = new BufferedReader(new FileReader(edgeFile));
    pLine = pReader.readLine();
    while (pLine!=null)
    {
    		String[] strs = split(pLine,'\t');

		  	outData.writeBytes(((Integer)(ids.get(strs[0]))).intValue()+" "+((Integer)(ids.get(strs[1]))).intValue()+" 1\n");

       		pLine = pReader.readLine();
    }
    pReader.close();


	outData.close();
}

	private static void printUsage()
	{
		System.out.println("Usage: java EdgesToNet <edge list file> <output pajek file>");
		System.exit(1);
	}

	public static String[] split(String str, char delim)
    {
                // begin split
                Vector strsVec = new Vector(0,1);
                String tmp = str;
                while (tmp.indexOf(delim)!=-1)
                {
                		if (tmp.substring(0,tmp.indexOf(delim)).length()>0)
                        	strsVec.addElement(new String(tmp.substring(0,tmp.indexOf(delim))));
                        tmp = tmp.substring(tmp.indexOf(delim)+1,tmp.length());
                }
                if (tmp.length()>0) strsVec.addElement(new String(tmp));
                String[] strs = new String[strsVec.capacity()];
                for (int s = 0; s < strsVec.capacity(); s++)
                        strs[s] = (String)strsVec.elementAt(s);
                // end of split
                return strs;
    }
}
