-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.hpp
More file actions
185 lines (164 loc) · 4.39 KB
/
structs.hpp
File metadata and controls
185 lines (164 loc) · 4.39 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
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
/**
* @file structs.hpp
* @author The CS2 TA Team
* @version 1.0
* @date 2018
* @copyright This code is in the public domain.
*
* @brief Structs for representing Nodes, Edges, and Graphs
*/
#pragma once
#include <cmath>
#include <iostream>
using namespace std;
/**
* @brief Struct representing the Nodes of a graph.
*
* The Node struct is a representation of a point in the (Euclidean)
* 2-D plane which is part of a graph. The struct provides functionalities such
* as calculate distance and combining Kruskal trees.
*
* Contains member variables for use in MST and Shortest Path calculations
*/
struct Node
{
/**
* @brief Represents the x-coordinate of the point
*/
int x;
/**
* @brief Represents the y-coordinate of the point
*/
int y;
/**
* @brief node id
*/
int id;
/**
* @brief Initializes the struct
*
* @param id: first argument representing the id of node
* @param x: second argument representing the x-coordinate
* @param y: third argument representing the y-coordinate
*/
Node(int id_, int x_, int y_)
: x(x_)
, y(y_)
, id(id_)
{}
/**
* @brief Calculates distance from this node to another node
*
* @param a: Node to calculate distance to
* @return : Cartesian distance between two nodes in plane
*/
double distance (Node a) {
return sqrt((x - a.x)*(x - a.x) + (y - a.y)*(y - a.y));
}
/**
* @brief Change all nodes in a Kruskal tree to become part of
* another Kruskal tree. Does this recursively by calling the
* function on each of the connected nodes it has a Kruskal edge to
*
* @param new_tree_id: id of tree to change this node and its
* connected nodes to
*/
void kruskalFloodFill(int new_tree_id) {
this->which_tree = new_tree_id;
for (unsigned int i = 0; i < this->kruskal_edges.size(); i++) {
Node *other = kruskal_edges[i];
if (other->which_tree != new_tree_id) {
other->kruskalFloodFill(new_tree_id);
}
}
}
/**
* @brief Adjacency list of all nodes that this node is connected
* through
*/
std::vector<Node *> edges;
// extra variables for algorithms
/**
* @brief Edges that a node is connected to within a Kruskal tree
* Should be a subset of edges vector
*/
std::vector<Node *> kruskal_edges;
/**
* @brief For shortest path computation. Used to later backtrack and
* find path
*/
Node * previous;
/**
* @brief For shortest path computation. Stores the distance from each
* node to the start node at each point in computation
*/
double distance_to_start;
/**
* @brief For Kruskal's algorithm. Stores which of the Kruskal trees
* this node is in (trees are numbered from 0 to NPOINTS - 1)
*/
int which_tree;
};
/**
* @brief Struct representing the Edges of a graph.
*
* The Edge struct is a representation of an edge between two points
* in the 2-D plane which is part of a graph.
*/
struct Edge
{
/**
* @brief Stores references to the two nodes connected by the edge
*/
Node *a;
Node *b;
/**
* @brief Stores id of an edge for uniqueness
*/
int id;
/**
* @brief Weight of an edge. In this case, we use the length of
* the edge as the weight
*/
double weight;
/**
* @brief Initializes the struct
*
* @param id: first argument representing the id of edge
* @param a: second argument representing the first node
* @param b: third argument representing the second node
* @param weight: fourth argument representing weight of edge
*/
Edge(int id_, Node *a_, Node *b_, double weight_)
: a(a_)
, b(b_)
, id(id_)
, weight(weight_)
{}
};
/**
* @brief Struct representing a graph.
*
* The Graph struct is a representation of a graph in the 2-D plane
*/
struct Graph
{
/**
* @brief Nodes within a graph
*/
vector<Node *> nodes;
/**
* @brief Edges within a graph
*/
vector<Edge *> edges;
/**
* @brief Initializes the struct
*
* @param nodes: first argument representing the nodes of a graph
* @param edges: second argument representing the edges of a graph
*/
Graph(vector<Node *> nodes_, vector<Edge *> edges_)
: nodes(nodes_)
, edges(edges_)
{}
};