有没有降低算法c#复杂度的方法

有没有降低算法c#复杂度的方法,c#,C#,我为Department of Redundancy Department算法创建了这段代码,它从文件中获取数据,并检查每个集合是否存在冗余FD(函数依赖关系)。它工作正常,但有26圈复杂度。即使我尝试将if条件重构为不起作用的方法,有没有办法降低它 公共结构节点 { 公共字符串左; 公共字符串权; } 公共静态bool checkredandance(列表节点) { bool resultat=false; 对于(int i=0;i”+listNodes[j]。右); resultat=tr

我为Department of Redundancy Department算法创建了这段代码,它从文件中获取数据,并检查每个集合是否存在冗余FD(函数依赖关系)。它工作正常,但有26圈复杂度。即使我尝试将if条件重构为不起作用的方法,有没有办法降低它

公共结构节点
{
公共字符串左;
公共字符串权;
}
公共静态bool checkredandance(列表节点)
{
bool resultat=false;
对于(int i=0;i”+listNodes[k]。右+“avec”+listNodes[i]。左+“->”+listNodes[i]。右);
if(listNodes[k].right==listNodes[j].right)
{
Console.WriteLine(“redandance dans”+listNodes[j]。左+“->”+listNodes[j]。右);
resultat=true;
}
}
}
}
}
}
返回结果;
}
公共静态bool CheckRedondance2(列表节点)
{
bool resultat=false;
node nouvelleDF;
nouvelleDF.right=“”;
nouvelleDF.left=“”;
for(int i=0;i1)
{
字符串concatD,concatG;
concatG=listNodes[i]。left+listNodes[j]。left.Substring(1,listNodes[j]。left.Length-1);
concatD=listNodes[i]。right+listNodes[j]。left.Substring(1,listNodes[j]。left.Length-1);
if(concatD.Contains(listNodes[j].右))
{
控制台写入线(“第2部分:“+concatG+”->“+concatD”);
nouvelleDF.right=listNodes[j]。右;
nouvelleDF.left=混凝土;
控制台写入线(“nouvelle df”+nouvelleDF.left+“->”+nouvelleDF.right);
//concatD=/*listNodes[i]。right;*/listNodes[i]。right+listNodes[j]。left.Substring(1,listNodes[j]。left.Length-1);
int nbIterations=0;//pour connaire l'existance de la même DF
for(int k=0;k”+listNodes[k]。右);
resultat=true;
}
}
}
}
}
}
}
}
返回结果;
}
静态void Main()
{
var lines=File.ReadAllLines(@“C:/input.txt”);//fichier输入讲座
int i=0;//la ligne du fichier
int nb=0;//la longueur de chaque groupe(和其他人一样)
List listNodes=new List();//les DF de chaque groupe(node est une DF)
int numEnsemble=0;
而(i”)
{
nb=Int32.Parse(行);
如果(nb!=0)
{
numEnsemble++;
控制台。写线(“”);
控制台写入线(“集合数字”+数字符号);
//控制台写入线(nb);
对于(int j=i;j BC
string[]parts=groupLine.Split(新字符串[]{“->”},StringSplitOptions.None);
localNode.left=parts[0].Trim();//expl:A
localNode.right=parts[1].Trim();//expl:BC
Console.WriteLine(localNode.left+“->”+localNode.right);
添加(localNode);
}
}
if(!checkredandance(listNodes))//premiere Methode expl:A->BD/BD->C/A->C
{
如果(!CheckRedondance2(listNodes))//2me Meth:expl:P->RST/PS->T
{
控制台。写入线(“备份”);
}
}
Clear();
i+=nb;
}
}
其他的
{
i++;
}
}
Console.ReadLine();
}
1)想法,而不是完整的解决方案:

如何更改文件中的第一个结构

public struct node
{
    public List<string> conns;
}
在一个节点的左侧或右侧有连接,这对您来说是非常重要的吗?如果
public struct node
{
    public List<string> conns;
}
foreach (string element in nodeRight.conns) {
  if (nodeLeft.conns.Contains(element))
  {
     /*...*/
  }
}
public static bool CheckRedondanceEfficient(List<node> listNodes)
{
    var nodeLookup = new Dictionary<string, List<node>>();
    var matchLookup = new Dictionary<string, HashSet<string>>();
    foreach (node node in listNodes)
    {
        if (AddNode(node, nodeLookup, matchLookup))
            return true;
    }
    foreach (node node in listNodes)
    {
        if (matchLookup.TryGetValue(node.left, out HashSet<string> hashLookup) && hashLookup.Contains(node.right))
            return true;
    }
    return false;
}

private static bool AddNode(node node, Dictionary<string, List<node>> nodeLookup, Dictionary<string, HashSet<string>> matchLookup)
{
    if (matchLookup.TryGetValue(node.left, out HashSet<string> hashLookup) && hashLookup.Contains(node.right))
        return true;
    if (nodeLookup.TryGetValue(node.left, out List<node> nodeMatches))
    {
        foreach (node first in nodeMatches)
        {
            AddFirstMatch(first, node, matchLookup);
        }
        nodeMatches.Add(node);
    }
    else
    {
        nodeLookup.Add(node.left, new List<node>()
        {
            node
        });
    }
    return false;
}

private static void AddFirstMatch(node nodeFirst, node nodeSecond, Dictionary<string, HashSet<string>> matchLookup)
{
    HashSet<string> firstMatch;
    if (!matchLookup.TryGetValue(nodeFirst.right, out firstMatch))
    {
        firstMatch = new HashSet<string>();
        matchLookup.Add(nodeFirst.right, firstMatch);
        firstMatch.Add(nodeSecond.right);
    }
    else
        firstMatch.Add(nodeSecond.right);
}