pptaszynski / aisdi-graf

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.

This URL has Read+Write access

aisdi-graf / graf.h
100644 134 lines (112 sloc) 2.298 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
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
#include <math.h>
#include <limits>
#include <fstream>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <list>
 
using namespace std;
 
/**
* A Class representing a vertex of a graph
*
*/
class Edge;
 
class Vertex
{
public:
double x; // Vertex x coordinate
double y; // Vertex y cooridnate
vector<Edge> adj; // Vector of adjacent vert.
double dist; // to compute dist in finding shortest path alg.
Vertex *prev; // Previous Vertex on the path - to help print it
int scratch; // Var. for alg.
 
/**
* Public constructor.
*
* @param int
* @para int
*
*/
Vertex ( double xco, double yco ) : x(xco), y(yco) {
reset();
}
 
/**
* Method to reset fields for the algorithm
*
*/
void reset() {
dist = numeric_limits<double>::infinity();
prev = NULL;
scratch = 0;
}
};
 
/**
* Class representing an edge in graph
*
*/
 
class Edge
{
public:
// First vertex is the one that has this edge in adj Vector
Vertex *to; // Destination vertex of an edge.
double cost;
 
/**
* Public constructor
*
* @param Vertex*
* @param double
*/
Edge( Vertex *d = 0, double c = 0.0 ) : to(d), cost(c) { }
 
};
 
/**
* class for a priority queue for searching weighted shortest-path algorithm
*
*/
class Path
{
public:
Vertex *to;
double cost;
 
Path(Vertex* t = 0, double c = 0.0) : to(t), cost(c) { }
 
bool operator>(const Path& other) const {
return cost > other.cost;
}
 
bool operator<(const Path& other) const {
return cost < other.cost;
}
};
 
/**
* Graph class
*
* @constructor no parameteres
* @method void addEdge(int v, int w, double costvw)
* @method void printPath(int w)
* @method void unweighted(int s)
* @method void weighted(int s)
*
*/
class Graph
{
public:
Graph() {}
~Graph();
 
Vertex* addVertex (unsigned int key, double x, double y);
void addEdge (int from, int to, double cost);
void printPath (int to) const;
void unweighted (int from);
void weighted (int to);
 
private:
Vertex* getVertex (int v);
void printPath (const Vertex &to) const;
void clearAlg();
 
typedef map<int, Vertex*, less<int> > vertex_map;
 
// Disable operator=
inline const Graph& operator= (const Graph& other) {
return *this;
}
 
vertex_map vmap;
 
};