1

I'm actually trying to implement the floyd's warshall algorithm but I met a hard-point. I got a segmentation fault while i'm trying to find the shortest-way in my matrix of sequence. I follow this tutorial: floyd's warshall algorithm

Everything work well except at the line 124:

chemin.insert(it,sequenceTest[v1-1][temp-1]);

where i get the segfault (You can skip the first part except if you want to know how I implement the algorithm butt the problem is in the second part):

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int i,j,k;
int sequenceTest [4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int tempSequenceTest [4][4];
int distanceTest [4][4] = {{0,2,4,100000},{2,0,1,5},{4,1,0,3},{100000,5,3,0}};
int tempDistanceTest[4][4];

for(i=0;i<4;i++)
{
    for(j=0;j<4;j++)
    {
        cout<<sequenceTest[i][j]<<" ";
    }
    cout<< endl;
}

cout<< endl;

for(i=0;i<4;i++)
{
    for(j=0;j<4;j++)
    {
        cout << distanceTest[i][j] << " ";
    }
    cout<< endl;
}

cout<< endl;

for(k=0;k<3;k++)
{
    cout <<endl;
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            cout << "i: " << i << " j: " << j << endl;
            if(i == k)
            {
                tempSequenceTest[i][j] = sequenceTest[i][j];
                tempDistanceTest[i][j] = distanceTest[i][j];
                cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
            }
            if(j == k)
            {
                tempSequenceTest[i][j] = sequenceTest[i][j];
                tempDistanceTest[i][j] = distanceTest[i][j];
                cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
            }
            cout<< endl;
        }
    }

    for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                //cout<< "i: " << i << " j: " << j << " k:" << k << endl;
                if(i!=k && j!=k)
                {
                    if(distanceTest[i][j]> distanceTest[i][k] + distanceTest[k][j])
                    {
                        tempDistanceTest[i][j] = distanceTest[i][k] + distanceTest[k][j];
                        cout << distanceTest[i][j] << " > " << distanceTest[i][k] << " + " << distanceTest[k][j] << " = " <<  tempDistanceTest[i][j] << " " << i << j << endl;
                        tempSequenceTest[i][j] = k+1;
                    }
                    else
                    {
                        tempDistanceTest[i][j] = distanceTest[i][j];
                        tempSequenceTest[i][j] = sequenceTest[i][j];
                    }
                }
            }
        }
        cout<< endl;
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                distanceTest[i][j] = tempDistanceTest[i][j];
                sequenceTest[i][j] = tempSequenceTest[i][j];
            }
        }
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                cout<<tempSequenceTest[i][j]<<" ";
            }
            cout<< endl;
        }

        cout<< endl;

        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                cout << tempDistanceTest[i][j] << " ";
            }
            cout<< endl;
        }
}

int v1, v2;
v1 = 1;
v2 = 4;
vector <int> chemin;
vector <int>::iterator it;
chemin.push_back(v1);
chemin.push_back(v2);
int temp = v2;
cout << sequenceTest[0][2];
while(sequenceTest[v1-1][temp-1] != v2)
{
    it = chemin.begin() + 1;
    cout << "V1: " << v1 << " V2: " << v2 << " Temp: " << temp << endl;
    chemin.insert(it,sequenceTest[v1-1][temp-1]);
    for(i=0;i<chemin.size();i++)
    {
        cout << chemin[i];
    }
    cout << endl;
    temp = sequenceTest[v1-1][temp-1];
}
}

Thanks for your attention and for taking the time to help me!

mel
  • 2,730
  • 8
  • 35
  • 70

1 Answers1

2
chemin.insert(it,sequenceTest[v1-1][temp-1]);

This invalidates the iterator it, but you keep reusing it in the iterations that follow. If you want to continue inserting at the same position, capture the return value.

it = chemin.insert(it,sequenceTest[v1-1][temp-1]);
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • I tried to add: it = chemin.insert(it,sequenceTest[v1-1][temp-1]); just after chemin.insert(it,sequenceTest[v1-1][temp-1]); it's kept segfault and I tried befor I fall in an infinite loop – mel Jan 12 '15 at 20:27
  • @mel: What do you mean "just after"? You mean you have both lines, one after the other? This fix is a replacement, not an addition. – Benjamin Lindley Jan 12 '15 at 20:30
  • I edit my post but i think my problem doesn't come from insert anymore – mel Jan 12 '15 at 20:32
  • @mel: Why did you edit your post? What happened when you applied my fix. That is, replacing `chemin.insert(it,sequenceTest[v1-1][temp-1]);` with `it = chemin.insert(it,sequenceTest[v1-1][temp-1]);` – Benjamin Lindley Jan 12 '15 at 20:36
  • With your fix i don't get anymore segfault but I met an infinite loop which come from my while loop – mel Jan 12 '15 at 20:44
  • @mel: I take it by your acceptance of my answer that you realize your new problem is a completely separate one, which belongs in a separate post (if you can't figure out what is causing it on your own). – Benjamin Lindley Jan 12 '15 at 20:48