Hey, I have an assignment due in a few hours, and I can't quite finish it up. I would appreciate if somebody could review the assignment and my code and offer some assistance.
Here is the assignment:
Implement Dijkstra’s shortest-path algorithm. The graph will be given in a text file. We make several assumptions to simplify the implementation. A graph containing n vertexes uses first n capital letters in the alphabet as the names of these vertexes. The file for a graph with n vertexes contains n lines, one for each vertex and the edges starting from that vertex. Each edge is represented by the name of the destination vertex and its length, which is an integer. The names of vertexes and lengths are separated by one or more spaces only. For example, a graph can be represented as:
A B 4 C 2
B D 2 C 3 E 3
C B 1 D 4 E 5
D
E D 1
Where there is a path from A to B of length 4, from A to C of length 2, from B to D of length 2, etc.
Your program will ask the user to input the source node and the destination
node. It will print out the shortest distance between them and the path from the source node to the
destination node. You do not have to implement priority queue for this assignment.
This is what I have so far. I can't figure out how to handle characters, so my program can only read numbers from the text file. It can read three per line, and does so in the following format: [starting node, ending node, length].
Please any prompt help would be awesome!
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
|
#include <iostream>
#include <string>
using namespace std;
#define GRAPHSIZE 2048
#define INFINITY GRAPHSIZE*GRAPHSIZE
#define MAX(a, b) ((a > b) ? (a) : (b))
int e; //The number of edges in the graph
int n; //The number of nodes in the graph
int dist[GRAPHSIZE][GRAPHSIZE];
int d[GRAPHSIZE];
void output() {
int i;
for (i = 1; i <= n; ++i)
cout << i; //just for reference until i finish
cout << endl;
for (i = 1; i <= n; ++i) {
cout << d[i]; //just for reference until i finish
}
cout << endl;;
}
void dijkstra(int x) {
int i, k, small;
int visited[GRAPHSIZE];
for (i = 1; i <= n; ++i) {
d[i] = INFINITY;
visited[i] = 0; //element i hasn't been visited
}
d[x] = 0;
for (k = 1; k <= n; ++k) {
small = -1;
for (i = 1; i <= n; ++i)
if (!visited[i] && ((small == -1) || (d[i] < d[small])))
small = i;
visited[small] = 1;
for (i = 1; i <= n; ++i)
if (dist[small][i])
if (d[small] + dist[small][i] < d[i])
d[i] = d[small] + dist[small][i];
}
}
int main(int argc, char *argv[]) {
int i, j;
int a, b, c;
FILE *fin = fopen("dist.txt", "r"); //open the file
fscanf(fin, "%d", &e);
for (i = 0; i < e; ++i)
for (j = 0; j < e; ++j)
dist[i][j] = 0; //set initial dist to zero
n = -1;
for (i = 0; i < e; ++i) {
fscanf(fin, "%d%d%d", &a, &b, &c);
dist[a][b] = c;
n = MAX(a, MAX(b, n));
}
fclose(fin);
dijkstra(1);
output();
cin >> j;
return 0;
}
|