I am trying to create Dijkstra's algorithm for a weighted graph, and have a flaw, I believe, in the implementation of the basic algorithm. It only visits the nearest nodes, checks the edges, but nothing else. Right now, I'm afraid I'm too close to this to see what's going on. Can anybody point me in the right direction?
Input file:
0
0 1 2
0 2 4
1 2 1
1 3 5
2 4 3
3 4 1
Output:
Enter the name of the file: a.txt
The shortest distance from 0 to all the nodes is
0 : 2
1 : 6
2 : -76543210
3 : -76543210
4 : -76543210
5 : -76543210
6 : -76543210
Code
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <vector>
#include <iterator>
using namespace std;
struct Edge
{
int end; //this is the node that the edge points to.
int weight; //this is the weight of the node, very important!
int origin; //finally, this is the node which we are pointing to, just in case it's needed.
Edge(int start, int dest, int lbs): origin(start), end(dest), weight(lbs){ };
bool operator < (const Edge& a) const
{
return weight < a.weight;
}
};
const int INFINITY = -76543210;
int filelength(string filename)
{
ifstream file;
string str;
int lines=0;
file.open(filename);
while (getline(file, str))
{
lines++;
}
file.close();
return lines;
}
void dij(int x, const vector<vector<Edge>> &list, vector<int> &path, vector<int> &visited)
{
priority_queue<Edge> Q;
int a = list.size();
Edge origin = Edge(list[x][0].origin, list[x][0].end, list[x][0].weight);
path.clear(); //clean out any pre-existing junk
path.resize(a, INFINITY);
visited.resize(a, -1);
Q.push(origin);
path.clear();
path.assign(a, INFINITY);
while (!Q.empty())
{
Edge temp = Edge(Q.top().origin, Q.top().end, Q.top().weight);//we need to preserve the top value so we can use it later
Q.pop();//removing the top
//cout<<"eee";
int begin = temp.origin;
for(std::size_t i=0;i < list[begin].size();++i )
{
if(list[begin][i].weight != 0)
{
if(path[i] < (path[begin] + list[begin][i].weight)) //this is the problem
{
if(path[i] == INFINITY)
{path[i] = 0;}
path[i] = path[begin] + list[begin][i].weight;
}
if(!visited[i])
{
Edge newEdge(list[begin][i].origin, list[begin][i].end, path[i]);
Q.push(newEdge);
visited[i] = true;
}
}
}
}//closure of the while loop
cout << "The shortest distance from " << x << " to all the nodes is" << endl;
for(int i=0;i < list.size();i++ )
{
cout << i << " : " << path[i] << endl;
}
cout << endl << endl;
}
int main()
{
vector<vector<Edge>> list;
vector<int> path;
vector<int> visited;
std::vector<int>::size_type sz = path.size();
int e = 0; //This is the total size of the vector. It gets determined once
string filename;
printf("Enter the name of the file: ");
cin>>filename;
ifstream infile;
infile.open(filename);
int v = 0;
int x;
if(!infile.is_open())
{
cout << "Error opening file";
//cin.get();
exit(EXIT_FAILURE);
}
infile>>x;
Edge original(x, x, 0);
original.origin = original.end;
e = filelength(filename);
list.resize(e);
int a,b;
while(infile>>v>>a>>b)
{
list[v].push_back(Edge(v,a,b));
}
//the first is the origin point, the second is the n-1th term. for example 00 is the first listed value of the node 0
//cout<<list[0][0].weight;
for (unsigned i=0; i<sz; i++)
{
path[i]=i;
}
infile.close();
dij(x, list, path, visited);
cin>>x;
return 0;
}