I created this code for the Department of Redundancy Department algorithm , It gets data from file and it checks each set if there is a redondant FD(Functional Dependency ) it works fine but it has a 26 Cyclomatic Complexity is there any way to reduce it even when i tried to refactor the if conditions into methods it didn't work Here you find the Explanation of the algorithm
public struct node
{
public string left;
public string right;
}
public static bool CheckRedondance(List<node> listNodes)
{
bool resultat = false;
for (int i = 0; i < listNodes.Count; i++) //pour parcourir les DF du groupe
{
for (int j = i + 1; j < listNodes.Count; j++) //pour parcourir le reste du groupe
{
if (listNodes[i].left == listNodes[j].left) // pour trouver la partie gauche egale
{
for (int k = 0; k < listNodes.Count; k++)
{
if (listNodes[k].left == listNodes[i].right)
{
Console.WriteLine("en commun : " + listNodes[k].left + "->" + listNodes[k].right + " avec " + listNodes[i].left + "->" + listNodes[i].right);
if (listNodes[k].right == listNodes[j].right)
{
Console.WriteLine("redondance dans " + listNodes[j].left + "->" + listNodes[j].right);
resultat = true;
}
}
}
}
}
}
return resultat;
}
public static bool CheckRedondance2(List<node> listNodes)
{
bool resultat = false;
node nouvelleDF;
nouvelleDF.right = "";
nouvelleDF.left = "";
for (int i = 0; i < listNodes.Count; i++)
{
for (int j = i + 1; j < listNodes.Count; j++)
{
if (listNodes[j].left.Contains(listNodes[i].left))
{
if (listNodes[j].left.Length > 1)
{
string 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].right))
{
Console.WriteLine("partie 2 :" + concatG + "->" + concatD);
nouvelleDF.right = listNodes[j].right;
nouvelleDF.left = concatG;
Console.WriteLine("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 connaitre l'existance de la même DF
for (int k = 0; k < listNodes.Count; k++) //recherche de la nouvelle DF dans la liste et trouver la redondance du resultat
{
if ((listNodes[k].right == nouvelleDF.right) && ((listNodes[k].left == nouvelleDF.left)))
{
nbIterations = nbIterations + 1;
if (nbIterations == 1)
{
Console.WriteLine("redondance dans " + listNodes[k].left + "->" + listNodes[k].right);
resultat = true;
}
}
}
}
}
}
}
}
return resultat;
}
static void Main()
{
var lines = File.ReadAllLines(@"C:/input.txt"); //lecture du fichier input
int i = 0; //la ligne du fichier
int nb = 0; //la longueur de chaque groupe (et sera ecrasée à chaque fois)
List<node> listNodes = new List<node>(); //les DF de chaque groupe (node est une DF)
int numEnsemble = 0;
while (i < lines.Length)
{
var line = lines[i];
if (!line.Contains("->"))
{
nb = Int32.Parse(line);
if (nb != 0)
{
numEnsemble++;
Console.WriteLine(" ");
Console.WriteLine("Ensemble numero " + numEnsemble);
//Console.WriteLine(nb);
for (int j = i; j <= i + nb; j++) //new groupe
{
var groupLine = lines[j];
if (groupLine.Contains("->"))
{
node localNode; //expl: A->BC
string[] parts = groupLine.Split(new string[] { "->" }, StringSplitOptions.None);
localNode.left = parts[0].Trim(); //expl:A
localNode.right = parts[1].Trim();//expl: BC
Console.WriteLine(localNode.left + "->" + localNode.right);
listNodes.Add(localNode);
}
}
if (!CheckRedondance(listNodes)) //premiere Methode expl: A->BD / BD->C / A->C
{
if (!CheckRedondance2(listNodes)) //2eme Meth: expl: P->RST / PS->T
{
Console.WriteLine("Pas de redondance");
}
}
listNodes.Clear();
i += nb;
}
}
else
{
i++;
}
}
Console.ReadLine();
}