(* ::Package:: *)

BeginPackage["CDPackage`"]
AllDistinctChordDiagramGenerator::usage = "This function generate all distinct chord digrams of given order k. For example, if you type AllDistinctChordDiagramGenerator[3] then it gives all distict chord diagrams of order 3. However to get all distinct chord diagrms of that order, it needs chord diagram of oder k-1. The program read this as input and assume these files are in your current directory. Therefore, for first run, you should run the program starting order k=3 and continue to increase. The output is written into a text file and textfile should be in current folder, where this program resides."
GenerateSimilarDiagramSet::usage = "This function is a internal part of AllDistinctChordDiagramGenerator[kk] function. This function generate all possible similar diagrams and then give similar chord diagram as output(with standard naming). For example, if you enter 223311, this gives a set which consists {112233,122331}. Standard naming mean first number is named as always 1, second distinct number named as always 2, so on."
Remove1TRelations::usage = "This function removes 1T relations from given chord diagram set. However it need first all distinct chord digram of given order as input. Thus first run the, AllDistinctChordDiagramGenerator[kk] function. The result is stored in a textfile and saved in current directory."
GenerateRelationMatrix::usage = "This function need two inputs. kk= Number of chords,  The program need distinctCDwithout1T text files. These files can be generate by running Remove1TRelations[kk] function. The solution is writtrn in to the text file and stored in a current directory. The column of matrix presents cord diagrams from distinctCDwithout1T.txt file. For example, columns of completerelationmatrixofCD3.txt are represent by 121323 and 123123 (the way they appear in the distinctCDwithout1T3.txt file ). The rows are all possible 4t relations for these chord diagrams."
GenerateRows::usage = "This function generate rows of relation matrix using 4T relations. This is use by GenerateRelationMatrix function "
Generate4TRelation::usage = "This function generate a 4t relation for given diagram. This is use by GenerateRows function"
CheckWhethergivenListformIntegerBasis::usage = "This function check whether given set form a integer basis or not. function need kk=number of chords, possiblebasisset=possiblebasisset should be provide in {112233,223344,....} format. Each gauss code should be include in double quotation marks. The program gives number of column in the relation matrix and its Hermitenormalform. The user need to decide whether Hermitenormal form is equivalent to Identity in first n by n matrix, where n=number of columns. If so, then the list form integer basis. Otherwise not." 
GetIndexgivenCD::usage = "This function return standard classification used by this program.For given order, each gausscode without 1t relations indexed as 1,2,3,4,etc. This function return the corrosponding index for given gauss code according to this program. The function need two inputs givencdlist=gausscodes and kk=number of codes. Gauss Codes can be provide as set of text strings. Example GetIndexgivenCD[{123123,121323},3]. Remeber to include each gauss code inside the quotation marks seperated by comma"
Getrationalbasiselement::usage = "Provide the rational basis for given order with dimension. For example Getrationalbasiselement[5] gives the dimension of rational basis and write the basis element in to a text file named RationalBasisforCD5.txt. The text file is in current directory. Textfile contain the gauss codes of rational basis and their standard index(naming) according to this program "
Moduloprimebasisfinder::usage = "This function gives the basis under modulo prime. It need kk=number of chords, moduloprime=primenumber. Example: Moduloprimebasisfinder[5,3] give the basis for chord digrams of order 5 under modulo 3 operations. The result is written in to the textfile Modulo3IntegerBasisforCD5.txt in current directory. Textfile contain the gauss codes of basis elements and their standard index(naming) according to this program"
FindTheBasisElement::usage = "This function calculate the basis element when provide the row reduce relation matrix. This function is for internal use"
CheckWhethergivenListformModuloPrimeBasis::usage = "This function can be used to decide whether given list form modulo prime basis. Here kk=number of chords, primenum=prime number, possiblebasisset=the gauss code set(candidate for the basis). Example CheckWhethergivenListformPrimeBasis[5,11,{1231435425,1231452345,1231453254,1234152345}]. The outputgives modulo prime rank for the primenumber 11. If they are not equal to number of columns, then it does not form a modulo prime basis under that specific prime number . Each Gauss code should be include in double quotation marks."
GetCDgivenIndex::usage = "This gives the standard gausscode when provide the index.  The index is classification used by this program. For example, there are several without 1t relation order 5 gauss code diagrams. This one give which gausscode we treat as first, second, third in that list. Example GetCDgivenIndex[{1,2,3},5]. kk=number of chords"
DrawGaussCode::usage = "This will draw given gauss code. Example DrawGaussCode[123123] draw diagram of 123123. 123123 should be enter inside of double quotation marks"
FindStandardRepofgivenCD::usage="This will give the standard representation used by this program for similar chord diagrms."
InitializeProg::usage = "User should run this program when they run this firsttime to initialize and generate all required files. You can enter kk=maximum number of chrod diagrams you are going to work with. This program create all distinct chrod diagrms without 1T relations upto that order and then create relation matrix upto that order "
Begin["Private`"]

AllDistinctChordDiagramGenerator[kk_] := Module[{str, writestr, A, characterlist, finallist, BB, DD, numberofdistinctdiagrams, clist, currentpath, readdirectorypreviousfile, distinctchorddiagramdirectory},
      	characterlist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
          clist = Take[characterlist, kk];
      
      	currentpath = NotebookDirectory[];
      	readdirectorypreviousfile = StringJoin[ToString[currentpath], "distinctCDoforder", ToString[kk - 1], ".txt"];
      	distinctchorddiagramdirectory = StringJoin[ToString[currentpath], "distinctCDoforder", ToString[kk], ".txt"];
      	Quiet[DeleteFile[distinctchorddiagramdirectory]];
         
          If[kk == 3,
        		writestr = OpenWrite[StringJoin[ToString[currentpath], "distinctCDoforder2.txt"]];
        		Write[writestr, "1212"];
        		Write[writestr, "1122"];
        		Close[writestr];
        	];
      
      	If[kk > 2,
        		str = OpenRead[readdirectorypreviousfile];
        		writestr = OpenWrite[distinctchorddiagramdirectory];
        		A = ToString /@ ReadList[str];
        		Close[str];   
        		Clear[g];
                g[i_] := StringReplaceList[i, StartOfString ~~ x___ ~~ y___ ~~ z___ ~~ EndOfString :> x ~~ clist[[kk]] ~~ y ~~ clist[[kk]] ~~ z];
                DD = Flatten[Map[g, A]];
                Clear[A];
        		Clear[f];
        		f[x_] := GenerateSimilarDiagramSet[x, kk][[1]];
        		finallist = DeleteDuplicates[Map[f, DD]];
        		numberofdistinctdiagrams = Length[finallist];   
        		Clear[DD];   
        		Clear[writingtofile];
        		writingtofile[x_] := Write[writestr, finallist[[x]]];
        		Map[writingtofile, Range[Length[finallist]]];
        		Clear[finallist];
        		Close[writestr];
        	];
      	ClearAll[str, writestr, A, characterlist, finallist, BB, DD, clist, currentpath, readdirectorypreviousfile, distinctchorddiagramdirectory];
      	Print["\n Number of distinctCDof order ", kk, ":", numberofdistinctdiagrams];
      	numberofdistinctdiagrams  
      ];

GenerateSimilarDiagramSet[Diag_ , kk_] := Module[{diag1, n, cd1, cd2, cd3, ruleset, characterlist, clist},
      	characterlist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
         clist = Take[characterlist, kk];
      
         diag1 = Characters[ToString /@ Diag];
         cd1 = Table[RotateLeft[diag1, n], {n, Length[diag1]}];
         cd2 = Map[DeleteDuplicates, cd1];
         Clear[diag1];
         
         Clear[newrule];
         newrule[x_] := Thread[Rule[x, clist]];
         ruleset = Map[newrule, cd2];
         Clear[cd2];
         
         Clear[newset];
         newset[x_] := cd1[[x]] /. ruleset[[x]];
         cd3 = Map[StringJoin, Map[newset, Range[Length[cd1]]]];
         Clear[cd1, ruleset];
         
         cd3 = Sort[DeleteDuplicates[cd3]];
         cd3
      ];
Remove1TRelations[kk_] := Module[{str, writestr, A, without1Tdiagram, numberofCDwithout1T, currentpath, distinctchorddiagramwithout1Tdirectory, readdirectory},
         currentpath = NotebookDirectory[];
         readdirectory = StringJoin[ToString[currentpath], "distinctCDoforder", ToString[kk], ".txt"];
         distinctchorddiagramwithout1Tdirectory = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[kk], ".txt"];
         Quiet[DeleteFile[distinctchorddiagramwithout1Tdirectory]];
      
         str = OpenRead[readdirectory];
         writestr = OpenWrite[distinctchorddiagramwithout1Tdirectory];
         
         A = Map[ToString, ReadList[str]];
         Close[str];
         without1Tdiagram = Select[A, Not@(StringMatchQ[#, x_ ~~ __ ~~ x_] || StringMatchQ[#, ___ ~~ y_ ~~ y_ ~~ ___]) &];
         Clear[A];
         
         numberofCDwithout1T = Length[without1Tdiagram];
         Clear[writingtofile];
         writingtofile[x_] := Write[writestr, {x, without1Tdiagram[[x]]}];
         Map[writingtofile, Range[Length[without1Tdiagram]]];
         Clear[without1Tdiagram];
         
         Close[writestr];
         Print["Number of distinct chord diagramds of order ", kk, " without 1T relations:", numberofCDwithout1T];
         numberofCDwithout1T
      ];
GenerateRelationMatrix[kk_ ] := Module[{str, writestr, A, characterlist, cdsetwithsimilardiag, numberofrowinmatrix, lenA, i, clist, currentpath, readdirectory, matrixdirectory, numberofcolumn, numberofrow},
    	characterlist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
       	clist = Take[characterlist, kk];
      
        currentpath = NotebookDirectory[];  
        readdirectory = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[kk], ".txt"];
       	matrixdirectory = StringJoin[ToString[currentpath], "completerelationmatrixofCD", ToString[kk], ".txt"];     
       	Quiet[DeleteFile[matrixdirectory]];
         
       	str = OpenRead[readdirectory];
       	writestr = OpenWrite[matrixdirectory, PageWidth -> Infinity];
       	A = ReadList[str];
       	lenA = Length[A];
       	Close[str];
      
     	For[i = 1, i < lenA + 1, i++,
        		cdsetwithsimilardiag = GenerateSimilarDiagramSet[ ToString[A[[i]][[2]]], kk];
           		GenerateRows[cdsetwithsimilardiag, clist, A, writestr, kk];
           		ClearAll[cdsetwithsimilardiag];
      	];
       	ClearAll[A];  
       	Close[writestr];
      
      	str = OpenRead[matrixdirectory];
      	A = ReadList[str];
      	Close[str];
      	
      	numberofcolumn = Length[A[[1]]];
       	numberofrow = Length[A];
     	Print["The size of relation matrix for order ", kk, ":", numberofrow, " by ", numberofcolumn];      
    	ClearAll[str, writestr, A, characterlist, cdsetwithsimilardiag, lenA, i, clist, currentpath, readdirectory, matrixdirectory, numberofcolumn, numberofrow];
         
	];
GenerateRows[similardiagrams_, clist_, A_, writestr_, kk_] := Module[{row, ans, i, pos},	
		Clear[rowgenerator];
       	rowgenerator[x_] := Generate4TRelation[x, clist, A, kk];
       	ans = Map[rowgenerator, similardiagrams];
        
      	For[i = 1, i < Length[ans] + 1, i++,
           		pos = Flatten[Position[ans[[i]], _?(# != 0 &), 1, 1]];
           		If[pos != {}, Write[writestr, ans[[i]]]];
           		Clear[pos];
        
      	];         
      	ClearAll[row, ans, i, pos];
	];
Generate4TRelation[diag_, clist_, A_, kk_] := Module[{i, endchord1, endchord2, diag1, diag2, diag3, diag4, fourTdiagramset, x1, x2, x3, temp},
         
 		endchord1 = DeleteDuplicates[Flatten[StringPosition[diag, "1"]]][[2]];
        endchord2 = DeleteDuplicates[Flatten[StringPosition[diag, "2"]]][[2]];
        If[endchord1 < endchord2,
        	diag1 = diag;
            diag2 = StringReplace[diag1, "1" ~~ "2" ~~ x1___ ~~ "1" ~~ x2___ ~~ "2" ~~ x3___ -> "1" ~~ "2" ~~ x1 ~~ "2" ~~ x2 ~~ "1" ~~ x3]; ,
            diag2 = diag;
            diag1 = StringReplace[diag2, "1" ~~ "2" ~~ x1___ ~~ "2" ~~ x2___ ~~ "1" ~~ x3___ -> "1" ~~ "2" ~~ x1 ~~ "1" ~~ x2 ~~ "2" ~~ x3];
       	];
      	diag3 = StringReplace[diag2, "1" ~~ "2" ~~ x1___ ~~ "2" ~~ x2___ ~~ "1" ~~ x3___ -> "1" ~~ x1 ~~ "2" ~~ x2 ~~ "2" ~~ "1" ~~ x3];
       	diag4 = StringReplace[diag3, "1" ~~ x1___ ~~ "2" ~~ x2___ ~~ "2" ~~ "1" ~~ x3___ -> "1" ~~ x1 ~~ "2" ~~ x2 ~~ "1" ~~ "2" ~~ x3];
       	fourTdiagramset = {diag1, diag2, diag3, diag4};
         
       	Clear[g];
       	g[x_] := GenerateSimilarDiagramSet[x, kk][[1]];
       	fourTdiagramset = Map[g, Characters /@ ToString /@ fourTdiagramset];
         
       	Clear[h];
        h[x_] := Flatten[Position[fourTdiagramset, x]];
        temp = h[#2] & @@@ A;
         
        newrule = Thread[Rule[{1, 2, 3, 4}, {1, -1, 1, -1}]];
        temp = temp /. newrule;
         
       	temp = Map[Total, ToExpression /@ temp];
        ClearAll[diag1, diag2, diag3, diag4, g, h, fourTdiagramset];
        temp
         
 	];
CheckWhethergivenListformIntegerBasis[kk_, possiblebasisset_] := Module[{str, B, i, A, numberofrow, numberofcolumn, indexset, hermitenormalform, relationmatrix, currentpath},
      	
   		currentpath = NotebookDirectory[];
        relationmatrix = StringJoin[ToString[currentpath], "completerelationmatrixofCD", ToString[kk], ".txt"];             
      	str = OpenRead[relationmatrix];
      	A = ReadList[str];
      	Close[str];
      
      	numberofcolumn = Length[A[[1]]];
      	numberofrow = Length[possiblebasisset];         
      	indexset = GetIndexgivenCD[possiblebasisset, kk];
      	     
      	B = Table[0, {i, numberofrow}, {j, numberofcolumn}];
      
      	For[i = 1, i < numberofrow + 1, i++,
        	B[[i, indexset[[i]] ]] = 1;        
        ];     
      	B = Join[A, B];
      	ClearAll[A];
      
      	hermitenormalform = HermiteDecomposition[B][[2]][[1 ;; numberofcolumn]];
      
      	ClearAll[str, B, i, A, numberofrow, indexset, relationmatrix, currentpath];
      	If[hermitenormalform == IdentityMatrix[numberofcolumn], Print["The set form integer basis"], Print["The set does not form integer basis"]];
      	      
      	Print["\n\nthis is the upper n by n submatrix of the hermite normal form of matrix where n=numberof columns"];
      	Print[MatrixForm[hermitenormalform]];	
 	];

FindStandardRepofgivenCD[givencd_, kk_] := Module[{},
      	ToString[GenerateSimilarDiagramSet[givencd , kk][[1]]]
 	];

GetIndexgivenCD[givencdlist_, kk_] := Module[{currentpath, distinctchorddiagramwithout1Tdirectory, A, str, pos, standardcdlist, indexlist},
   		currentpath = NotebookDirectory[];
      	distinctchorddiagramwithout1Tdirectory = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[kk], ".txt"];
      
       	Clear[f];
        f[x_] := FindStandardRepofgivenCD[x, kk];
       	standardcdlist = Map[f, givencdlist];
      
       	str = OpenRead[distinctchorddiagramwithout1Tdirectory];
       	A = ReadList[str];
       	Close[str];      
      
      	pos = Flatten[Position[A[[All, 2]], #] & /@ standardcdlist];
       	indexlist = A[[#]][[1]] & /@ pos;
       	ClearAll[currentpath, distinctchorddiagramwithout1Tdirectory, A, str, standardcdlist];
      
       	indexlist
 	];
Moduloprimebasisfinder[kk_, moduloprime_] := Module[{currentpath, str, A, basisdirectory, distinctchorddiagramwithout1Tdirectory, relationmatrixdirectory},
      	
   		currentpath = NotebookDirectory[];
      	basisdirectory = StringJoin[ToString[currentpath], "Modulo", ToString[moduloprime], "IntegerBasisforCD", ToString[kk], ".txt"];
       	distinctchorddiagramwithout1Tdirectory = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[kk], ".txt"];
       	Quiet[DeleteFile[basisdirectory]];
      
       	relationmatrixdirectory = StringJoin[ToString[currentpath], "completerelationmatrixofCD", ToString[kk], ".txt"];
       	str = OpenRead[relationmatrixdirectory];
      
      	Print["start loading relation matrix"]; 
      	A = ReadList[str]; 
      	Close[str];
      	Print["finish loading relation matrix"];
      
      	Print["start computing rowreduce matrix under modulo prime"];
      	A = RowReduce[A, Modulus -> moduloprime];
      	Print["finish row reducing matrix under modulo prime"];
      
      	FindTheBasisElement[A, distinctchorddiagramwithout1Tdirectory, basisdirectory];
      
      	Print["\n\n Modulo ", moduloprime, " basis for order ", kk, " diagrams is in current directory as textfile"];
      	ClearAll[A, str, relationmatrixdirectory];
	];
FindTheBasisElement[A_, without1Tdiagramdirectory_, basisdirectory_] := Module[{numberofcolumn, numberofrow, B, C1, i, str, without1Tdiagrams, writestr, j},
      	numberofcolumn = Length[A[[1]]];
      	numberofrow = Length[A];      
      (*
      This function give position of 1 in row reduce matrix. For example if A={0,1,0,0,0},{0,0,1,1,1},{0,0,0,1,0},{0,0,0,0,0},{0,0,0,0,0}} then this give
       B={{},{1},{2},{2,3},{2}}
      *)
      	Clear[f];
      	f[x_] := Flatten[Position[A[[All, x]], _?(# == 1 &)], 1];
      	B = Map[f, Range[numberofcolumn]];
      	C1 = {};
      
      (*Following  Forloop check piviot elements. For our above example this give C1={{2,1},{3,2},{5,2}}*)
   		For[i = 1, i < Length[B] + 1, i++,
        	If[B[[i]] != {} && Length[B[[i]]] == 1, C1 = AppendTo[C1, {i, Last[B[[i]]]}] ];
       	];
      	Clear[B];
      
      	C1 = DeleteDuplicates[C1, (Last[#1] == Last[#2] &)]; (*This gives C1={{2,1},{3,2}}*)
      	C1 = C1[[All, 1]]; (*This give C1={2,3}. Basically row number. These elements can be wriiten as linear combination of other elements*)
      	C1 = Complement[Range[numberofcolumn], C1]; (*This give the independent variable. Or complement of C1 That is any thing not depend on this list*)    
      
      	str = OpenRead[without1Tdiagramdirectory];
      	without1Tdiagrams = ReadList[str];
      	Close[str];
      
      	writestr = OpenWrite[basisdirectory, PageWidth -> Infinity];
      	For[j = 1, j < Length[C1] + 1, j++,
        	Write[writestr, {C1[[j]], without1Tdiagrams[[ C1[[j]] ]][[2]]}];
        ];
      
      	Close[writestr];
      	ClearAll[numberofcolumn, numberofrow, B, C1, i, str, without1Tdiagrams, writestr, j];  
 	];
Getrationalbasiselement[kk_] := Module[ { readdirectory, rowreducedirectory, without1Tdiagramdirectory, rationalbasisdirectory, currentpath, str, i, j, k, writestr, A, numberofcolumn, numberofrow, dimensionofrationalbasis},
      
      	currentpath = NotebookDirectory[];
      	readdirectory = StringJoin[ToString[currentpath], "completerelationmatrixofCD", ToString[kk], ".txt"];
      	rowreducedirectory = StringJoin[ToString[currentpath], "RowReduceForm", ToString[kk], ".txt"];
      	without1Tdiagramdirectory = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[kk], ".txt"];
      	rationalbasisdirectory = StringJoin[ToString[currentpath], "RationalBasisforCD", ToString[kk], ".txt"];
      	Quiet[DeleteFile[rowreducedirectory]];
      	Quiet[DeleteFile[rationalbasisdirectory]];
      
      
      	Print["Start loading initial reduce relation matrix"];
      	str = OpenRead[readdirectory];
      	A = ReadList[str];
      	Close[str];
      	Print["Finish loading initial redeuce relation matrix"];
      
      	Print["start computing Row reduce form"];
      	A = RowReduce[A];
      	Print["finish computing Row reduce form"];
      	numberofcolumn = Length[A[[1]]];
      	numberofrow = Length[A];
      	dimensionofrationalbasis = numberofcolumn - MatrixRank[A];
      
      	writestr = OpenWrite[rowreducedirectory, PageWidth -> Infinity];
      	For[k = 1, k < numberofrow + 1, k++,
        	Write[writestr, A[[k]]];
        ];
      	Close[writestr];
      	FindTheBasisElement[A, without1Tdiagramdirectory, rationalbasisdirectory];
      	ClearAll[readdirectory, rowreducedirectory, without1Tdiagramdirectory, rationalbasisdirectory, currentpath, str, i, j, k, writestr, A, numberofcolumn, numberofrow];
      
      	Print["\n\nRational basis for order ", kk, "is in current directory as textfile"];
      	Print["Dimension of the basis : ", dimensionofrationalbasis];
      	dimensionofrationalbasis
	];
CheckWhethergivenListformModuloPrimeBasis[kk_, primenum_, possiblebasisset_] := Module[{currentpath, str, B, moduloprimerank, rationalrank, i, A, numberofrow, numberofcolumn, indexset, relationmatrixdirectory},
      
   		currentpath = NotebookDirectory[];
      	relationmatrixdirectory = StringJoin[ToString[currentpath], "completerelationmatrixofCD", ToString[kk], ".txt"];
      	str = OpenRead[relationmatrixdirectory];
      	A = ReadList[str];
      	Close[str];
      
      	numberofcolumn = Length[A[[1]]];
      	numberofrow = Length[possiblebasisset];     
      	indexset = GetIndexgivenCD[possiblebasisset, kk];    
      
      	B = Table[0, {i, numberofrow}, {j, numberofcolumn}];
      
      	For[i = 1, i < numberofrow + 1, i++,
        	B[[i, indexset[[i]] ]] = 1;        
        ];     
      	B = Join[A, B];
      	ClearAll[A];    
      	
      	moduloprimerank = MatrixRank[B, Modulus -> primenum];
      	If[moduloprimerank == numberofcolumn, Print["This list form modulo prime basis"], Print["This list does not form modulo prime basis"] ];
      
      	ClearAll[str, B, i, A, numberofrow, indexset, currentpath, relationmatrixdirectory];
     	Print["The rank of the matrix under modulo operations: ", moduloprimerank];
	];
GetCDgivenIndex[givenindex_, kk_] := Module[{currentpath, distinctchorddiagramwithout1Tdirectory, str, A, cdlist, pos},
   		currentpath = NotebookDirectory[];
       	distinctchorddiagramwithout1Tdirectory = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[kk], ".txt"];      
         
      	str = OpenRead[distinctchorddiagramwithout1Tdirectory];
 		A = ReadList[str];
      	Close[str];
      
     	pos = Flatten[Position[A[[All, 1]], #] & /@ givenindex];      
 		cdlist = A[[#]][[2]] & /@ pos;
 		
      	ClearAll[currentpath, distinctchorddiagramwithout1Tdirectory, str, pos];      
     	cdlist
  	];
DrawGaussCode[gausscode_] := Module[{a, b, c2, c1, r, pts, image1, currentpath, imagedirectory, str},      
     	a = Characters[gausscode];
      	b = DeleteDuplicates[a];
      	c2 = Flatten[Position[a, #, 1][[2]] & /@ b];
      	c1 = Flatten[Position[a, #, 1][[1]] & /@ b];
      	r = 2;(*radius*)
      	pts = Table[{r*Cos[t], r*Sin[t]}, {t, 0, 2 Pi, 2 Pi/Length[a]}];
      	
      	image1 = Graphics[{Thickness[0.03], Circle[{0, 0}, r], Line[{pts[[c1[[#]]]], pts[[c2[[#]]]]}] & /@ Range[Length[b]]} ];
      
    	currentpath = NotebookDirectory[];
     	imagedirectory = StringJoin[ToString[currentpath], "cd", ToString[Length[b]], "_", ToString[gausscode], ".jpg"];
      
       	Export[imagedirectory, image1]
   	];
InitializeProg[kk_] := Module[{i, currentpath, a, b, c, start},
  		Print["Initializing the program......\n"];
      	currentpath = NotebookDirectory[];
      	start = kk + 2;
      	For[i = 3, i < kk + 1, i++,
        	a = StringJoin[ToString[currentpath], "distinctCDoforder", ToString[i], ".txt"];
        	b = StringJoin[ToString[currentpath], "distinctCDwithout1T", ToString[i], ".txt"];
        	c = StringJoin[ToString[currentpath], "completerelationmatrixofCD", ToString[i], ".txt"];
        	If[! FileExistsQ[a],
          		start = i;
          		i = kk + 2;
          	];
       		If[FileExistsQ[a] && ! FileExistsQ[b] && ! FileExistsQ[c],
          		start = i + 1;
          		Remove1TRelations[i];
          		GenerateRelationMatrix[i];
          		i = kk + 2;
          	];
        	If[FileExistsQ[a] && FileExistsQ[b] && ! FileExistsQ[c],
          		start = i + 1;
          		GenerateRelationMatrix[i];
          		i = kk + 2;
          	];
        
        ];      
      	For[i = start, i < kk + 1, i++,
        	AllDistinctChordDiagramGenerator[i];
        	Remove1TRelations[i];
        	GenerateRelationMatrix[i];
        ];
      	Print["\n Output text files  are in current directory....."];
      	Print["Program initialized to work with chord digrams of order less than or equal to ", kk];
  	];
End[]
EndPackage[]
