-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.cpp
244 lines (212 loc) · 8.65 KB
/
graph.cpp
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
/// File: graph.cpp
/// Encapsulates graph representations along with functionality to determine
/// shortest paths, and the minimum spanning tree for the graph.
#include "graph.h"
#include "disjointset.h"
#include <iostream>
#include <iomanip>
using namespace std;
const int INFINITY = 1000000000;
/// Constructor sets the number of vertices in this Graph. Initialises the two dimensional
/// array of weights by setting all values to INFINITY (some value larger than any
/// possible edge weight) except the diagonal of the array where the weight is set to 0.00.
Graph::Graph(unsigned int numVertices) {
this->numVertices = numVertices;
weights = new double*[numVertices];
for(unsigned int row = 0; row < numVertices; ++row) {
weights[row] = new double[numVertices];
for(unsigned int column = 0; column < numVertices; ++column) {
if (row == column) {
weights[row][column] = 0.00;
}
else {
weights[row][column] = INFINITY;
}
}
}
}
/// Destructor
Graph::~Graph() {
for(unsigned int i = 0 ; i < numVertices ; i++) {
delete[] weights[i];
}
delete[] weights;
vertices.clear();
}
/// Adds pointer to Vertex to the collection of vertices for this Graph.
void Graph::addVertex(Vertex* vertex) {
this->vertices.push_back(vertex);
}
/// Accessor returns a pointer to the Vertex with the identifier/index in the parameter.
Vertex* Graph::getVertex(int index) {
return vertices[index];
}
/// Adds pointer to Edge to the edge list for this Graph. Using the source and destination
/// identifiers from the edge, sets the weight of the undirected edge in the adjacency matrix.
void Graph::addEdge(Edge* edge) {
this->edges.push(edge);
double weight = edge->getWeight();
unsigned int source = edge->getSource()->getId();
unsigned int destination = edge->getDestination()->getId();
weights[source][destination] = weight;
weights[destination][source] = weight;
}
/// Uses Kruskal’s algorithm to find the Minimum Spanning Tree (MST) for this Graph. Stores the
/// edges of the MST in the adjacency list of each Vertex. Returns the cost of the minimum spanning tree.
double Graph::minimumSpanningTreeCost() {
double minCost;
unsigned int edgeCount = 0;
DisjointSet mst = DisjointSet(numVertices);
while((!edges.empty()) & (edgeCount < numVertices - 1)) {
Edge* edge = edges.top();
edges.pop();
unsigned int source = edge->getSource()->getId();
unsigned int destination = edge->getDestination()->getId();
if (!mst.sameComponent(source, destination)) {
edgeCount++;
mst.join(source, destination);
Vertex* vertex1 = getVertex(source);
Vertex* vertex2 = getVertex(destination);
vertex2->addAdjacency(source);
vertex1->addAdjacency(destination);
edges.push(edge);
minCost = minCost + edge->getWeight();
}
}
return minCost;
}
/// Determines the shortest path from the source vertex to all other vertices using dijkstras algorithm.
void Graph::dijkstra(unsigned int sourceId) {
priority_queue<Vertex*, vector<Vertex*>, Vertex> vertexQueue;
for (vector<Vertex*>::iterator v = this->vertices.begin(); v != this->vertices.end(); v++) {
Vertex* vertex = *v;
vertex->setDiscovered(false);
vertex->setPredecessorId(sourceId);
vertex->setMinDistance(weights[sourceId][vertex->getId()]);
vertexQueue.push(vertex);
}
while(!vertexQueue.empty()) {
Vertex* u = vertexQueue.top();
vertexQueue.pop();
u->setDiscovered(true);
for (vector<Vertex*>::iterator i = this->vertices.begin(); i != this->vertices.end(); i++) {
Vertex* v = *i;
if ((weights[u->getId()][v->getId()] < INFINITY) & (!v->isDiscovered())) {
if (u->getMinDistance() + weights[u->getId()][v->getId()] <= v->getMinDistance()) {
v->setMinDistance(u->getMinDistance() + weights[u->getId()][v->getId()]);
v->setPredecessorId(u->getId());
vertexQueue.push(v);
}
}
}
}
dijkstraOutput(sourceId);
}
/// Outputs the results from the dijkstras algorithm to the console.
/// Prints the length of the path and the vertex identifiers in the path.
void Graph::dijkstraOutput(unsigned int sourceId) {
for (vector<Vertex*>::iterator v = this->vertices.begin(); v != this->vertices.end(); v++) {
Vertex* vertex = *v;
double distance = vertex->getMinDistance();
unsigned int predecessor = INFINITY;
vector <unsigned int> path;
if ((distance == INFINITY) & (!vertex->getId() == sourceId)) {
cout << "NO PATH from " << sourceId << " to " << fixed << setw(2) << vertex->getId() << endl;
}
else if (!vertex->getId() == sourceId) {
cout << "Distance from 0 to " << fixed << setw(2) << vertex->getId() << " = " << setprecision(2) << fixed << setw(6) << distance << " travelling via ";
while (predecessor != sourceId) {
path.insert(path.begin() ,vertex->getId());
predecessor = vertex->getPredecessorId();
vertex = vertices[predecessor];
if (vertex->getId() == sourceId) {
path.insert(path.begin(), (vertex->getId()));
}
}
for (vector<unsigned int>::iterator p = path.begin(); p != path.end(); p++) {
unsigned int node = *p;
cout << setw(2) << node << " ";
}
cout << endl;
}
}
}
/// Determines the shortest path from the source vertex to all other vertices using only the adjacencies
/// in the minimum spanning tree.
void Graph::bfs(unsigned int sourceId) {
for (vector<Vertex*>::iterator v = this->vertices.begin(); v != this->vertices.end(); v++) {
Vertex* vertex = *v;
vertex->setDiscovered(false);
}
queue<Vertex*> vertexQueue;
this->vertices[sourceId]->setDiscovered(true);
vertexQueue.push(vertices[sourceId]);
while (!vertexQueue.empty()) {
Vertex* currentVertex = vertexQueue.front();
vertexQueue.pop();
set<unsigned int>* adjacencies = currentVertex->getAdjacencies();
for (set<unsigned int>::iterator i = adjacencies->begin(); i != adjacencies->end(); i++) {
unsigned int index = *i;
Vertex* adjacent = this->vertices[index];
if (!adjacent->isDiscovered()) {
adjacent->setPredecessorId(currentVertex->getId());
adjacent->setDiscovered(true);
vertexQueue.push(adjacent);
}
}
}
bfsOutput(sourceId);
}
/// Outputs the results from the bfs to the console.
/// Prints the length of the path and the vertex identifiers in the path.
void Graph::bfsOutput(unsigned int sourceId) {
for (vector<Vertex*>::iterator v = this->vertices.begin(); v != this->vertices.end(); v++) {
Vertex* vertex = *v;
double distance = 0;
unsigned int predecessor = INFINITY;
vector <unsigned int> path;
if ((vertex->getMinDistance() == INFINITY) & (!vertex->getId() == sourceId)) {
cout << "NO PATH from " << sourceId << " to " << fixed << setw(2) << vertex->getId() << endl;
}
else if (!vertex->getId() == sourceId) {
cout << "Distance from 0 to " << fixed << setw(2) << vertex->getId() << " = ";
while (predecessor != sourceId) {
path.insert(path.begin() ,vertex->getId());
predecessor = vertex->getPredecessorId();
distance += weights[vertex->getId()][vertex->getPredecessorId()];
vertex = vertices[predecessor];
if (vertex->getId() == sourceId) {
path.insert(path.begin(), (vertex->getId()));
}
}
cout << setprecision(2) << fixed << setw(6) << distance << " travelling via ";
for (vector<unsigned int>::iterator p = path.begin(); p != path.end(); p++) {
unsigned int node = *p;
cout << setw(2) << node << " ";
}
cout << endl;
}
}
}
/// Outputs the adjacency matrix for the graph.
ostream& operator<<(ostream& out, Graph& graph) {
for(unsigned int row = 0; row < graph.numVertices; ++row) {
for(unsigned int column = 0; column < graph.numVertices; ++column) {
double weight = graph.weights[row][column];
if (column == 0) {
out << setprecision(2) << fixed << setw(6);
}
else {
out << setprecision(2) << fixed << setw(7);
}
if (weight == INFINITY) {
out << "-";
}
else {
out << weight;
}
}
out << endl;
}
return out;
}