-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixGraph.cpp
66 lines (55 loc) · 1.35 KB
/
MatrixGraph.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
/**********************************************************
* Mike Marsden
* HW05 - Traveling Salesperson Problem
* MatrixGraph.cpp
**********************************************************/
#include "MatrixGraph.h"
//Constructor
MatrixGraph::MatrixGraph(unsigned num_nodes) {
num_edges = 0;
M.resize(num_nodes);
for (int i = 0; i < M.size(); i++) {
M[i].resize(num_nodes);
}
}
//Destructor
MatrixGraph::~MatrixGraph() {}
//Setter Methods
void MatrixGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight) {
// By going u,v and v,u, it makes it undirected.
M[u][v] = weight;
M[v][u] = weight;
num_edges++;
}
//Accessors
EdgeWeight MatrixGraph::weight(NodeID u, NodeID v) const {
EdgeWeight eW = M[u][v];
return eW;
}
std::list<NWPair> MatrixGraph::getAdj(NodeID u) const {
std::list<NWPair> adjs = std::list<NWPair>();
for (int i = 0; i < M.size(); i++) {
// If this condition is met, there is a weight
// therefore an adjacent point.
if (M[u][i] != 0) {
adjs.push_back(NWPair(i, M[u][i]));
}
}
return adjs;
}
unsigned MatrixGraph::degree(NodeID u) const {
unsigned degree = 0;
for (int i = 0; i < M.size(); i++) {
// Will increment only if a "weight" is found
if (M[u][i] != 0) {
degree++;
}
}
return degree;
}
unsigned MatrixGraph::size() const {
return M.size();
}
unsigned MatrixGraph::numEdges() const {
return num_edges;
}