Kirsty Williams
Kirsty Williams

Reputation: 340

All Pairs Shortest Paths Udaya Kumar Redd Algorithm

Using the algorithm on Page 4 of the following paper, I am attempting to compute an all pairs shortest paths matrix.

http://www.waset.org/journals/ijcms/v3/v3-5-43.pdf

I am confused by lines 7 and 8 (of the algorithm) and subsequently, 11 and 12, because assigning s1 the value of j and using both these it the comparison on line 8 seems ambiguous to me. I am wondering if perhaps I am reading the indentation wrong. I am new to algorithms so please be patient with me.

    while(flag != false){
        for(int i=0; i<n;i++){
            aMin = Integer.MAX_VALUE;
            bMin = Integer.MAX_VALUE;

            for(int j=0; j<n;j++){
                // Line 7 of the algorithm
                if((distanceMatrix[i][j] < aMin) && (j != i) && (BR[i][j] == false)){
                    aMin = distanceMatrix[i][j];
                    BR[i][j] = true;
                    a[i] = j;
                    int s1 = j; // End of line 7
                    // Line 8 of the algorithm
                    if(distanceMatrix[i][j] < distanceMatrix[i][s1])
                        BR[i][s1] = false; // End of line 8
                }

            }

            for(int j=0; j<n; j++){
                // Line 11 of the algorithm
                if((distanceMatrix[i][j] < bMin) && (j != i) && (j != a[i]) && (BR[i][j] == false)){
                    bMin = distanceMatrix[i][j];
                    BR[i][j] = true;
                    b[i] = j;
                    int s2 = j; // end of line 11

                    // Line 12 of the algorithm
                    if(distanceMatrix[i][j] < distanceMatrix[i][s2])
                        BR[i][s2] = false; // end of line 12
                }
            }
        }

        for(int i=0; i <n; i++){
            for(int j=0; j<n; j++){
                int t1 = distanceMatrix[i][a[i]] + distanceMatrix[a[i]][j];
                int t2 = distanceMatrix[i][b[i]] + distanceMatrix[b[i]][j];
                distanceMatrix[i][j] = Math.min(distanceMatrix[i][j], Math.min(t1,t2));
                distanceMatrix[j][i] = distanceMatrix[i][j];
            }
        }

        flag = true;
        for(int i=0; i<n;i++){
            // Line 19 of the algorithm
            for(int j=0; j<n; j++){
                temp[i][j] = distanceMatrix[i][j]; // end of line 19
                // Line 20 of the algorithm
                if (distanceMatrix[i][j] == temp[i][j])
                    flag  = false; // end of line 20
            }
        }
    }

Additionally, I understand by looking at line 19 and 20 in the algorithm, it will get stuck in a loop because flag will always be true.

Upvotes: 1

Views: 381

Answers (1)

IVlad
IVlad

Reputation: 43487

I think it should be like this:

4) for i ← 1 to n do amin← ∞ ; bmin ← ∞
5)   for j ← 1 to n do
6)     if D[i, j] < amin and j = i and BR[i, j] = 0 then
7)       amin ← D[i, j] ; BR[i, j] ← 1 ; a[i] ← j ; s1 ← j
8)     if D[i, j] < D[i, s1] then BR[i, s1] ← 0

So as you can see, s1 might not always be j: it will always be <= j however.

So the second if is not inside the first. Otherwise, it doesn't make sense, because s1 == j always and you cannot have D[i, j] < D[i, j].

Upvotes: 1

Related Questions