Djikstra Algo Ouput Problem

Hi,
I am currently doing a project on Graphs and its almost complete. Whenever i enter a char other than A it gives me errors. I still can't figure out what causes it?

Here's the matrix for the code

0 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 4 3 0 0 0 0 0 0 0 0 0 0 0 0
5 4 0 7 7 0 0 8 0 0 0 0 0 0 0 0
0 3 7 0 0 0 0 11 0 0 16 13 14 0 0 0
0 0 7 0 0 4 0 5 0 0 0 0 0 0 0 0
0 0 0 0 4 0 9 0 0 0 0 0 0 0 0 0
0 0 0 0 0 9 0 0 0 0 0 0 0 12 0 0
0 0 8 11 5 0 0 0 3 0 0 0 0 0 0 0
0 0 0 0 0 0 0 3 0 4 0 0 0 0 0 0
0 0 0 0 0 0 0 0 4 0 0 0 0 3 0 8
0 0 0 16 0 0 0 0 0 0 0 5 0 7 0 4
0 0 0 13 0 0 0 0 0 0 5 0 9 0 4 0
0 0 0 14 0 0 0 0 0 0 0 9 0 0 5 0
0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 7
0 0 0 0 0 0 0 0 0 0 0 4 5 0 0 0
0 0 0 0 0 0 0 0 0 8 4 0 0 7 0 0




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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include<queue>

using namespace std;

// Number of vertices 
// in the graph
#define V 16
// A utility function to find the 
// vertex with minimum distance
// value, from the set of vertices
// not yet included in shortest
// path tree

int minDistance(int dist[], bool sptSet[])
{
	// Initialize min value
	int min = INT_MAX, min_index;
	for (int v = 0; v < V; v++)
	{
		if (sptSet[v] == false && dist[v] <= min)
			min = dist[v], min_index = v;
	}
	return min_index;
}

// Function to print shortest // path from source to j // using parent array
void printPath(int parent[], int j)
{
	// Base Case : If j is source

	if (parent[j] == -1)
		return;

	printPath(parent, parent[j]);
	int src = 0;
	char x;

	if (j == 0)
	{
		x = 'A';
	}
	else if (j == 1)
	{
		x = 'B';
	}
	else if (j == 2)
	{
		x = 'C';
	}
	else if (j == 3)
	{
		x = 'D';
	}
	else if (j == 4)
	{
		x = 'E';
	}
	else if (j == 5)
	{
		x = 'F';
	}
	else if (j == 6)
	{
		x = 'G';
	}
	else if (j == 7)
	{
		x = 'H';
	}
	else if (j == 8)
	{
		x = 'I';
	}
	else if (j == 9)
	{
		x = 'J';
	}
	else if (j == 10)
	{
		x = 'K';
	}
	else if (j == 11)
	{
		x = 'L';
	}
	else if (j == 12)
	{
		x = 'M';
	}
	else if (j == 13)
	{
		x = 'N';
	}
	else if (j == 14)
	{
		x = 'O';
	}
	else if (j == 15)
	{
		x = 'P';
	}
	cout << x;

}

// A utility function to print  // the constructed distance // array

void printSolution(int dist[], int n, int parent[])
{
	int src = 0;
	char x;
	if (src == 0)
	{
		x = 'A';
	}
	else if (src == 1)
	{
		x = 'B';
	}
	else if (src == 2)
	{
		x = 'C';
	}
	else if (src == 3)
	{
		x = 'D';
	}
	else if (src == 4)
	{
		x = 'E';
	}
	else if (src == 5)
	{
		x = 'F';
	}
	else if (src == 6)
	{
		x = 'G';
	}
	else if (src == 7)
	{
		x = 'H';
	}
	else if (src == 8)
	{
		x = 'I';
	}
	else if (src == 9)
	{
		x = 'J';
	}
	else if (src == 10)
	{
		x = 'K';
	}
	else if (src == 11)
	{
		x = 'L';
	}
	else if (src == 12)
	{
		x = 'M';
	}
	else if (src == 13)
	{
		x = 'N';
	}
	else if (src == 14)
	{
		x = 'O';
	}
	else if (src == 15)
	{
		x = 'P';
	}

	cout << "Vertex		Distance			Path \n";

	for (int i = 0; i < V; i++)
	{
		int sr = i;
		char x1;

		if (sr == 0)
		{
			x1 = 'A';
		}
		else if (sr == 1)
		{
			x1 = 'B';
		}
		else if (sr == 2)
		{
			x1 = 'C';
		}
		else if (sr == 3)
		{
			x1 = 'D';
		}
		else if (sr == 4)
		{
			x1 = 'E';
		}
		else if (sr == 5)
		{
			x1 = 'F';
		}
		else if (sr == 6)
		{
			x1 = 'G';
		}
		else if (sr == 7)
		{
			x1 = 'H';
		}
		else if (sr == 8)
		{
			x1 = 'I';
		}
		else if (sr == 9)
		{
			x1 = 'J';
		}
		else if (sr == 10)
		{
			x1 = 'K';
		}
		else if (sr == 11)
		{
			x1 = 'L';
		}
		else if (sr == 12)
		{
			x1 = 'M';
		}
		else if (sr == 13)
		{
			x1 = 'N';
		}
		else if (sr == 14)
		{
			x1 = 'O';
		}
		else if (sr == 15)
		{
			x1 = 'P';
		}

		cout << "\n" << x << "->" << x1 << "		" << dist[i] << "-------------------------------->" << x;
		printPath(parent, i);
		cout << endl;

	}

}

// Funtion that implements Dijkstra's
// single source shortest path
// algorithm for a graph represented
// using adjacency matrix representation

void dijkstra(int graph[V][V], int src)
{
	// The output array. dist[i]  will hold the shortest distance from src to i
	int dist[V];
	// sptSet[i] will true if vertex  i is included / in shortest path tree or shortest distance  from src to i is finalized
	bool sptSet[V];
	// Parent array to store  shortest path tree
	int parent[V];
	// Initialize all distances as   INFINITE and stpSet[] as false

	for (int i = 0; i < V; i++)
	{
		parent[0] = -1;
		dist[i] = INT_MAX;
		sptSet[i] = false;
	}

	// Distance of source vertex  from itself is always 0

	dist[src] = 0;

	// Find shortest path  for all vertices
	for (int count = 0; count < V - 1; count++)
	{
		// Pick the minimum distance vertex from the set of vertices not yet processed.   u is always equal to src  in first iteration.
		int u = minDistance(dist, sptSet);
		// Mark the picked vertex  as processed
		sptSet[u] = true;
		// Update dist value of the   adjacent vertices of the  picked vertex.

		for (int v = 0; v < V; v++)
		{
			// Update dist[v] only if is
			// not in sptSet, there is
			// an edge from u to v, and 
			// total weight of path from
			// src to v through u is smaller  than current value of dist[v]

			if (!sptSet[v] && graph[u][v] && dist[u] + graph[u][v] < dist[v])
			{
				parent[v] = u;
				dist[v] = dist[u] + graph[u][v];
			}
		}

	}
	// print the constructed  distance array
	printSolution(dist, V, parent);
}

// Driver Code
int main()
{	
	cout << "Shown below is the Matrix" << endl;
	cout << endl;
	cout << "------------------------------------------------------" << endl;
	//  Let us create the example
	// graph discussed above
	//  int graph[V][V] =
	//{
	// {0,5,5,0,0,0,0,0,0,0,0,0,0,0,0,0},
	// {5,0,4,3,0,0,0,0,0,0,0,0,0,0,0,0},
	// {5,  4,  0 , 7 , 7 , 0  ,0,  8,  0 , 0 , 0,  0,  0,  0  ,0,  0},
	// {0,  3,  7 , 0  ,0 , 0  ,0,  11, 0 , 0 , 16, 13, 14, 0  ,0,  0},
	// {0,  0,  7  ,0  ,0 , 4  ,0,  5,  0 , 0 , 0 , 0,  0 , 0  ,0,  0},
	// {0,  0,  0 , 0  ,4 , 0  ,9,  0,  0 , 0 , 0 , 0,  0 , 0  ,0,  0},
	// {0,  0,  0  ,0  ,0 , 9  ,0,  0,  0 , 0 , 0 , 0,  0 , 12, 0,  0},
	// {0,  0,  8 , 11 ,5 , 0  ,0,  0,  3 , 0 , 0 , 0,  0 , 0 , 0,  0},
	// {0,  0,  0  ,0  ,0 , 0  ,0,  3,  0 , 4 , 0 , 0,  0 , 0 , 0,  0},
	// {0,  0,  0  ,0  ,0 , 0  ,0,  0,  4 , 0 , 0 , 0,  0 , 3 , 0,  8},
	// {0,  0,  0  ,16 ,0 , 0  ,0,  0,  0 , 0 , 0 , 5,  0 , 7 , 0,  4},
	// {0,  0,  0  ,13 ,0 , 0  ,0,  0,  0 , 0 , 5 , 0,  9 , 0 , 4,  0},
	// {0,  0,  0  ,14, 0 , 0  ,0,  0,  0 , 0 , 0 , 9,  0 , 0 , 5,  0},
	// {0,  0,  0  ,0 , 0 , 0  ,0,  0,  0 , 3 , 0 , 0,  0 , 0 , 0,  7},
	// {0,  0,  0  ,0,  0 , 0  ,0,  0,  0 , 0 ,0  ,4,  5  ,0 , 0 , 0},
	// {0,  0,  0  ,0  ,0 , 0 , 0,  0,  0 , 8 ,4  ,0 , 0  ,7,  0 , 0}
	// };
	int rows = 16;
	int columns = 16;
	int graph[V][V];
	for (int i = 0; i<rows; i++)
	{
		for (int j = 0; j<columns; j++)
		{
			graph[i][j] = NULL;
		}
	}

	ifstream ifile;
	ifile.open("Matrix.txt");
	while (!ifile.eof())
	{
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				ifile >> graph[i][j];
			}
		}
	}
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			cout << graph[i][j] << "  ";
		}
		cout << endl;
	}
	cout << "----------------------------------------------------" << endl;
	int x = 0;
	char source = NULL;
	cout << "Enter a source from A-P: " << endl;
	cin >> source;
	x = source - 65;
	dijkstra(graph, 0);
	system("pause");
	return 0;
}

















Last edited on
You can simplify some of the code:

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
void printPath(int parent[], int j)
{
	// Base Case : If j is source

	if (parent[j] != -1) {
		printPath(parent, parent[j]);

		//const char x = 'A' + j;
		//const int ascii = x - 'A';
		//cout << ascii;
		cout << j;
	}
}

void printSolution(int dist[], int n, int parent[])
{
	const int src = 0;
	const char x = 'A' + src;	// x is always A as src is always 0 !!!

	cout << "Vertex		Distance			Path \n";

	for (int i = 0; i < V; ++i) {
		const char x1 = 'A' + i;

		cout << "\n" << x << "->" << x1 << "		" << dist[src] << "-------------------------------->" << x;
		printPath(parent, src);
		cout << endl;
	}
}

Topic archived. No new replies allowed.