
<< ~/Graph/Graph.m
<< ~/Graph/IHX.m

BBasis[m_,cols1_List,{}] := BBasis[m,cols1]

BBasis[m_,cols1_List,cols2_List] := BBasis[m,cols1,cols2] = Module[
  {n,diags,rels,basis,c,i,fname,s,cols,rcols1,rcols2,r},
  cols=cols1~Join~cols2;
  rcols1=cols1~Join~First[cols2]~Join~{1};
  rcols2=Rest[cols2];
  If[
    (
      (g=BSeedGraph[m,cols])===Unknown ||
      (rels=BBasis[m,rcols1,rcols2])===Unknown
    ),
    Unknown,
    Do[
      r=rels[[i]];
      relcolor=ColoredUV[Length[cols1]+1];
      floating=ColoredUV[Length[cols1]+2];
      pos=Position[r,_relcolor,{1}];
      posf=Position[r,_floating,{1}];
      rels[[i]]=Sum[
        ?????
        {j,Length[pos]}
      ],
      {i,Length[rels]}
    ];
    {diags,rels}=IHX[g];
    n=Length[diags];
    basis=Complement[
      Range[n],
      SubspaceBasis[rels] //. {v_Plus :> First[v], c_.*BasisVector[i_] :> i}
    ];
    basis=diags[[basis]];
    fname="BBasis/"~StringJoin~ToString[m]~StringJoin~(
      StringJoin @@ (ToString[StringForm["-``",#]]& /@ cols)
    );
    s=OpenWrite[fname];
    WriteString[s,"BBasis[",m,",",cols,"] =\n"];
    Write[s,basis];
    Close[s];
    Run["gzip -f", fname];
    basis
  ]
]
