Dijkstra's Algorithm producing incorrect results

In this implementation of Dijkstra I am trying to read a file in and apply the algorithm to it. While it does run, it does not produce the expected output. It will instead only evaluate the first case and no others. For example this file (the lone 0 is the starting point) produces the following output where 76543210 is my variant of infinity. What is the root of this problem?

File (node, destination, weight)
1
2
3
4
5
6
7
0
0 1 2
0 2 4
1 2 1
1 3 5
2 4 3
3 4 1


Output
1
2
3
4
5
6
7
8
9
10
Enter the name of the file: a.txt
The shortest distance from 0 to all the nodes is
0 : 76543212
1 : 76543210
2 : 76543210
3 : 76543210
4 : 76543210
5 : 76543210






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#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);
	//origin.origin = x;
	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);
	//cout<<path[0];
	//path.assign(a, 0);
	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()-1;++i  )
		{
			//cout<<"fff";
			//cout<<list[begin].size();
			if(list[begin][i].weight != 0)
			{
				//cout<<path[begin];
				//cout<<"ggg";
				//cout<<i<<" "<<begin;
				//cout<<path[i];
				 if(path[i] < (path[begin] +   list[begin][i].weight))	
				 {
					// cout<<"hhh";
                    path[i] = path[begin]  +  list[begin][i].weight;
				 }
				// cout<<"iii";
				 if(!visited[i])
				 {
					 Edge newEdge(list[begin][i].origin, list[begin][i].end, path[i]);
					 Q.push(newEdge);
					 visited[i] = true;
				 }
				 //cout<<"hhh";
			
			}
		}
	}//closure of the while loop
	cout << "The shortest distance from " << x << " to all the nodes is" << endl;
    for(int i=0;i < list.size()-1;i++  )
    {
        cout << i << " : " << path[i] << endl;
    }
    cout << endl << endl;

}

int main()
{
	//Edge original;
	//original.weight = 0;

	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;
	//cout<<filename;

	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/*,c*/;
		//c=0;
		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;
		
	
	//quit();
	return 0;
}

for(std::size_t i=0;i < list[begin].size()-1;++i ) //line 60 ¿why -1?
88
89
90
91
    for(int i=0;i < list.size()-1;i++  ) //again ¿why -1?
    {
        cout << i << " : " << path[i] << endl; //you traverse path, but the condition is on list ¿?
    }


70
71
72
73
				if(path[i] < (path[begin] +   list[begin][i].weight))
				{
					path[i] = path[begin]  +  list[begin][i].weight;
				}
you've got it backwards. It should be If the new path is better, then change it.
Better is smaller
1
2
if( path[begin]+list[begin][i].weight < path[i] )
   path[i] = path[begin] + list[begin][i].weight;



Also, iirc std::priority_queue would make the top to be the greatest value, but you need the smaller here.

Another thing: vector<int> &visited if you want a collection of booleans, then use a collection of booleans vector<bool> visited
You initialize the elements to -1, anything that is not 0 is true. You say that all were already visited.

I don't see the point in asking it as a parameter either
Last edited on
Topic archived. No new replies allowed.