0

I'm trying to implement a Floryd Algorithm in C++

I have this already:

a means the node where the edge starts.

b means the node where the edge ends.

t means the time of the edge.

m means the number of edges.

n means the number of nodes.

typedef pair<int,int> nodo;
vector <nodo> g[100000];

void preguntarFloyd()
{
    g->clear();
    int  m;
    int contador = 0;
    cin m;

    for(int k = 0; k < m ; k++)
    {
        int a, b, t;
        cin >> a >> b >> t;
        g[a].push_back(nodo(b,t));
    }

    for (int k = 0; k < n; k++)
    {
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j <n; j++)
            {
                if(g[i][k].second + g[k][j].second < g[i][j].second )
                {
                    g[i][j].second = g[i][k].second + g[k][j].second;
                }
            }
        }
    }


}

When I try the code, the program crashes saying: "Expression: Vector subscript out of range"

I hope you guys can help me since I haven't been able to work this out!

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • 7
    Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (An then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Mar 07 '13 at 00:57
  • 1
    What is `n`? What inputs are you providing when you run it? – Dave Mar 07 '13 at 01:00
  • where does it happen? what's the index it fails on? what's `n`? – Karoly Horvath Mar 07 '13 at 01:00
  • But I need some help because I don't even know how to implement it! Please help me :( – Nicolás Múnera Mar 07 '13 at 01:01
  • I don't know exactly where it happens because the program just crashes when executing it, and n means the number of nodes! – Nicolás Múnera Mar 07 '13 at 01:03
  • use a debugger or add trace messages (printf/cout/...) – Karoly Horvath Mar 07 '13 at 01:09
  • use `vector::size()` to upper bound the appropriate `for` loops rather than `n`. – Karthik T Mar 07 '13 at 01:37

1 Answers1

1

The two most common ways to represent a graph is:

  • An adjacency-list
  • An adjacency-matrix

You have one data structure, in the initialisation phase you handle it like if it was a an adjacency-list then you try to access it as if it were an adjacency-matrix. Obviously this will never work...

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • This is an awesome answer! How can I access it as if it were an adjacency-list??? – Nicolás Múnera Mar 07 '13 at 01:14
  • You have to check each element in the list and search for that specific edge - eg: if you are looking for (i,j) search in g[i] for an edge to j. if there's no edge, then in that step the route cannot be shortened. – Karoly Horvath Mar 07 '13 at 01:15
  • NOTE: You seem to be a beginner, using the matrix as the common format might make your life easier... in that case you need a special value to denote that there's no connection (0, -1, INT_MAX, etc...) – Karoly Horvath Mar 07 '13 at 01:17