-
Notifications
You must be signed in to change notification settings - Fork 0
/
Digraph.hpp
132 lines (98 loc) · 3.85 KB
/
Digraph.hpp
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
/*
@file Digraph.cpp
Interface file for directed graph class
*/
#ifndef DIGRAPH_HPP
#define DIGRAPH_HPP
#include <string>
#include <vector>
// keep track of wether a city has been visited or not
enum Status { NOT_VISITED, VISITED };
// Helper class, represents nodes on a graph.
// In our case, each node is a city
class Node {
private:
// name of the city
std::string name;
// whether the city has been visited or not
// NOT_VISITED or VISITED
enum Status status;
public:
// Constructor. Marks node as not visited and assigns them a name
// @param cityName [in] The name of the city
Node(std::string cityName) {
name = cityName;
status = Status::NOT_VISITED;
}
// @stereotype get
enum Status getStatus() const {
return status;
}
// @stereotype set
void setStatus(enum Status st) {
status = st;
}
// @stereotype get
std::string getName() const {
return name;
}
};
// Class representing a directed graph
class Digraph {
private:
// number of vertices (cities) in the graph (map)
int numberOfVertices = 0;
// number of edges (routes) in the graph (map)
int numberOfEdges = 0;
// vector of pointers to nodes (cities)
std::vector<Node*> vertices;
// adjacency matrix for storing the directed graph
// the values are weights(distances)
std::vector<std::vector<int>> distMatrix;
public:
// Returns the number of vertices
// @stereotype get
int getNumberOfVertices() const;
// Returns the number of edges
// @stereotype get
int getNumberOfEdges() const;
// Returns the vertex at index `i`
// @param i [in] The index of the vertex
Node* getVertex(int i) const;
// Adds the city named `cityName` to the directed graph
// @param cityName [in] The name of the city
void addVertex(std::string cityName);
// Resets all edge values to -1, indicating a complete absence of edges
void resetEdges();
// If there is not already an edge that directly connects
// `source` to `destination`, it is added
// @param source The index of the source city
// @param destination The index of the destination city
// @param weight The distance between `source` and `destination`
void addEdge(int source, int destination, int weight);
// If there is an edge that directly connects `source` to
// `destination`, it is deleted
// @param source The index of the source city
// @param destination The index of the destination city
void deleteEdge(int source, int destination);
// Checks to see if an edge exists that directly connects `source` to `destination`
// @param source The index of the source city
// @param destination The index of the destination city
// @return An integer representing the distance between `source` and `destination`
// @retval -1 An edge DOES NOT exist that directly connects `source` to `destination`
int isEdge(int source, int destination) const;
// Compute shortest path distances from `source` to `destination`
// @param `source` The index of the source city.
// @param `destination` The index of the destination city.
// @param `path` A string that holds the path between the cities
// @returns The distance between source and destination
int dijkstra(int source, int destination, std::string& path) const;
// Destructor, Frees all allocated memory
~Digraph();
private:
// Returns the vertex with the minimum distance
// @param dist [in] The vector holding the distances to all the cities from a single-source
// @returns The index of the vertex with the minimum distance
int minVertex(const std::vector<int>& dist) const;
};
#endif // DIGRAPH_HPP