Search code examples
c++algorithmdijkstrapath-finding

How create a if else loop in C++ instead of multiple nesting?


I create a C++ program to use Dijkstra algorithm. To be more precise in calculations, i create a function that verify if a path exists between 2 values of my dijkstra matrix.

My function :

void existence_chemin(unsigned int tab[50][50], unsigned int i, unsigned int j, unsigned int rang) {
unsigned int n = 1;
unsigned int r, s, ta; /* indices courants */
unsigned int puissance_tab[50][50];
initialisation_matrice_0(puissance_tab);
unsigned int puissance_tab2[50][50];
initialisation_matrice_0(puissance_tab2);
unsigned int puissance_tab3[50][50];
initialisation_matrice_0(puissance_tab3);

if (tab[i - 1][j - 1] != 0) {
    cout << endl << " Chemin direct de poids "<< tab[i - 1][j - 1] <<" existant entre les sommets " << i << " et " << j << "." << endl;
    //afficher_matrice(tab, rang);
    n++;
    dijkstra(tab, i - 1, j - 1, rang);
}
else {    // (tab[i - 1][j - 1] == 0)
    cout << endl << " Chemin direct entre les sommets " << i << " et " << j << " inexistant.\n Poursuite des recherches en cours ..." << endl;
        unsigned int p, q, r;
        for (p = 0; p < rang; p++)
        {
            for (q = 0; q < rang; q++)
            {
                for (r = 0; r < rang; r++)
                {
                    puissance_tab[p][q] += tab[p][r] * tab[r][q];
                }
            }
        }


    if (puissance_tab[i - 1][j - 1] != 0) {
        cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
        //cout << "\n\nAffichage de la matrice a l'ordre " << t - 1;
        //afficher_matrice(puissance_tab, rang);
        dijkstra(tab, i - 1, j - 1,rang);
    }
    else {
        unsigned int s, t, u;
        for (s = 0; s < rang; s++)
        {
            for (t = 0; t < rang; t++)
            {
                for (u = 0; u < rang; u++)
                {
                    puissance_tab2[s][t] += puissance_tab[s][u] * tab[u][t];
                }
            }
        }

        if (puissance_tab2[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            //cout << "\n\nAffichage de la matrice a l'ordre " << t - 1;
            //afficher_matrice(puissance_tab, rang);
            dijkstra(tab, i - 1, j - 1, rang);
        }
        else {
            unsigned int aaa, ttt, uuu;
            for (aaa = 0; aaa < rang; aaa++)
            {
                for (ttt = 0; ttt < rang; ttt++)
                {
                    for (uuu = 0; uuu < rang; uuu++)
                    {
                        puissance_tab3[aaa][ttt] += puissance_tab2[aaa][uuu] * puissance_tab[uuu][ttt];
                    }
                }
            }

            if (puissance_tab2[i - 1][j - 1] != 0) {
                cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
                //cout << "\n\nAffichage de la matrice a l'ordre " << t - 1;
                //afficher_matrice(puissance_tab, rang);
                dijkstra(tab, i - 1, j - 1, rang);
            }
            else{
                cout << " \nCONCLUSION :\n Aucun chemin n'existe donc entre les sommets " << i << " et " << j << " sur ce graphe." << endl;
            }
        }
    }
}
cout << endl;
choix(tab,rang);
}

I have done many if/else nesting to verify if a path exists for distances of 1, 2 and 3. But if the path is bigger than 3, it return that there is no path between 2 matrix values. That's because i nested if/else. I achieve to create a loop, can you help me to correct this problem ?

The whole code is here, on GitHub.


Solution

  • A cursory look at your code (even before trying to understand it) reveals that you compute puissance_tab3, and ignore it. That's the danger of copy-paste programming.

    Now, assuming that you mean puissance_tab3, first thing to do is to unnest it. Rather than spelling out an else clause, immediately return from if:

    void existence_chemin(unsigned int tab[50][50], unsigned int i, unsigned int j, unsigned int rang) {
        unsigned int n = 1;
        unsigned int r, s, ta; /* indices courants */
        unsigned int puissance_tab[50][50];
        initialisation_matrice_0(puissance_tab);
        unsigned int puissance_tab2[50][50];
        initialisation_matrice_0(puissance_tab2);
        unsigned int puissance_tab3[50][50];
        initialisation_matrice_0(puissance_tab3);
    
        if (tab[i - 1][j - 1] != 0) {
            cout << endl << " Chemin direct de poids "<< tab[i - 1][j - 1] <<" existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1, rang);
            return;
        }
        cout << endl << " Chemin direct entre les sommets " << i << " et " << j << " inexistant.\n Poursuite des recherches en cours ..." << endl;
        unsigned int p, q, r;
        for (p = 0; p < rang; p++) {
            for (q = 0; q < rang; q++) {
                for (r = 0; r < rang; r++) {
                    puissance_tab[p][q] += tab[p][r] * tab[r][q];
                }   
            }   
        }   
    
        if (puissance_tab[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1,rang);
            return;
        }   
        unsigned int s, t, u;
        for (s = 0; s < rang; s++) {
            for (t = 0; t < rang; t++) {
                for (u = 0; u < rang; u++) {
                    puissance_tab2[s][t] += puissance_tab[s][u] * tab[u][t];
                }   
            }   
        }   
    
        if (puissance_tab2[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1, rang);
            return;
        }   
        unsigned int aaa, ttt, uuu;
        for (aaa = 0; aaa < rang; aaa++) {
            for (ttt = 0; ttt < rang; ttt++) {
                for (uuu = 0; uuu < rang; uuu++) {
                    puissance_tab3[aaa][ttt] += puissance_tab2[aaa][uuu] * puissance_tab[uuu][ttt];
                }
            }
        }
    
        if (puissance_tab3[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1, rang);
            return;
        }
        cout << " \nCONCLUSION :\n Aucun chemin n'existe donc entre les sommets " << i << " et " << j << " sur ce graphe." << endl;
    }
    

    The code already looks much more manageable. Next step is to factor out matric multiplication:

    void existence_chemin(unsigned int tab[50][50], unsigned int i, unsigned int j, unsigned int rang) {
        unsigned int puissance_tab[50][50];
        initialisation_matrice_0(puissance_tab);
        unsigned int puissance_tab2[50][50];
        initialisation_matrice_0(puissance_tab2);
        unsigned int puissance_tab3[50][50];
        initialisation_matrice_0(puissance_tab3);
    
        if (tab[i - 1][j - 1] != 0) {
            cout << endl << " Chemin direct de poids "<< tab[i - 1][j - 1] <<" existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1, rang);
            return;
        }
        cout << endl << " Chemin direct entre les sommets " << i << " et " << j << " inexistant.\n Poursuite des recherches en cours ..." << endl; 
    
        matrix_multiply(tab, tab, puissance_tab, rang);
        if (puissance_tab[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1,rang);
            return;
        }
    
        matrix_multiply(puissance_tab, tab, puissance_tab2, rang);
        if (puissance_tab2[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1, rang);
            return;
        }
    
        matrix_multiply(puissance_tab2, puissance_tab, puissance_tab3, rang);
        if (puissance_tab3[i - 1][j - 1] != 0) {
            cout << " \nCONCLUSION :\n Chemin existant entre les sommets " << i << " et " << j << "." << endl;
            dijkstra(tab, i - 1, j - 1, rang);
            return;
        }
        cout << " \nCONCLUSION :\n Aucun chemin n'existe donc entre les sommets " << i << " et " << j << " sur ce graphe." << endl;
    }
    

    Now we are almost there. You can see the pattern which begs to be a loop. The only thing remaining is to manage those matrices. I hope you can do it.

    A word of warning though. I have a SWAG that you need successive powers of tab. You compute something entirely different. Indeed,

    puissance_tab = tab * tab = tab ^ 2
    puissance_tab2 = puissance_tab * tab = tab ^ 3
    puissance_tab3 = puissance_tab2 * puissance_tab = tab ^ 5
    

    are not successive powers.