public
Description: An application to find shortest paths in a graph specified by input file. A project for academic purposes. Created for the Algorithms and Data Structures laboratory classes.
Homepage:
Clone URL: git://github.com/pptaszynski/aisdi-graf.git
aisdi-graf / main.cpp
100644 96 lines (86 sloc) 1.84 kb
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
#include "graf.h"
using namespace std;
 
int main(int argc, char* argv[] )
{
if (argc != 2) {
cerr << "Usage: " << endl << argv[0] << " infile" << endl;
return 1;
}
 
ifstream in_file(argv[0] );
if( !in_file) {
cerr << "Error opening " << argv[1] << endl;
return 1;
}
 
Graph graph;
cout << "Reading graph from file..." << endl;
string curr_line;
 
//Reead vertices
//line count
int lcount = 0;
string line;
 
if (!getline(in_file, line).eof() ) {
++lcount;
}
unsigned int vertidx = 0;
while (in_file.gcount() > 0) {
double vx, vy;
istringstream buff(line);
 
buff >> vx;
buff >> vy;
if (buff.fail() )
cerr << "Incorrect line in input file: " << line << endl << "On line :" << lcount << endl;
else {
graph.addVertex(vertidx, vx, vy);
++vertidx;
}
if (!getline(in_file, line).eof() ) {
++lcount;
}
}
 
// Read edges
if (!getline(in_file, line).eof() ) {
++lcount;
}
while (in_file.gcount() > 0) {
unsigned int v, w;
double cost;
istringstream buff(line);
buff >> v;
buff >> w;
buff >> cost;
if (buff.fail() )
cerr << "Incorrect line in input file: " << line << endl << "On line :" << lcount << endl;
else {
graph.addEdge(v, w, cost);
++vertidx;
}
if (!getline(in_file, line).eof() ) {
++lcount;
}
}
 
// Read paths to find
if (!getline(in_file, line).eof() ) {
++lcount;
}
while (in_file.gcount() > 0) {
unsigned int from, to;
istringstream buff(line);
buff >> from;
buff >> to;
if (buff.fail() )
cerr << "Incorrect line in input file: " << line << endl << "On line :" << lcount << endl;
else {
//try {
graph.unweighted(from);
graph.printPath(to);
//}
//catch (const GraphException& e) {
// cerr << e.toString( ) << endl;
//}
}
if (!getline(in_file, line).eof() ) {
++lcount;
}
}
 
return 0;
}