Optional hw assignment. Graph defined by a text file, dijkstra method to run through the graph with a starting vertex number.

We're only allowed to change the dijkstra method, everything else must remain as the professor programmed.

The idea is that we run from the command line like so: programName textFile x, where x is the starting vertex.

Graph definition in a header file:

Code:

class Graph
{
struct Edge
{
int dest; // destination vertex number
int cost; // edge weight
Edge *next;
};
struct Vertex
{
Edge *adj;
bool known;
int dist;
int path;
};
vector<Vertex *> vertices;
vector<int> PQ; //priority queue
int PQsize;
void PQinsert (int v); //glorified enqueue
int PQdeleteMin (); //glorified dequeue
public:
Graph () { PQsize=0; }
int numVertices () { return vertices.size(); }
int dist (int dest) { return vertices[dest]->dist; }
void readFile (char *name); //creates the graph from file
void dijkstra (int start); //the only method I'm allowed to write myself
void printPath (int dest);
};

Based on this, here's what I have for the dijkstra method.

Code:

void Graph::dijkstra (int start)
{
Vertex *startV = vertices[start];
startV->path = INFINITY;
for (int i=start; i < this->numVertices(); i++)
{
vertices[i]->dist = INFINITY; //in the header, INFINITY is defined as INT_MAX
vertices[i]->known = false;
}
startV->dist = 0;
PQinsert(start);
while(PQ.size() > 0)
{
Vertex *v = vertices[this->PQdeleteMin()];
v->known = true;
Edge *e = v->adj;
while (e!=NULL)
{
Vertex *w = vertices[e->dest];
if(w->known==false)
{
int cvw = e->cost;
if(v->dist + cvw < w->dist)
{
w->dist = v->dist + cvw;
w->path = v->path;
PQinsert(w->path);
}
}
e = e->next;
}
}
}

When I run this, it brings up an error about a vector subscript out of range.

Through a series of cout statements to determine how far it gets, I've determined that it runs through all loops at least once, but brings up that error at the second attempt to run this line:

I found this out by using 2 cout statements, one at the beginning of the first while loop and another between different lines of code all the way down, until I saw one less instance of the second statement. After putting that second statement one line under the specified code, I see it only shows up once (the first statement shows up twice, as did this until then).

So, I've determined where the problem occurs, I just need some help fixing it. Will be greatly appreciated.