-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
137 lines (102 loc) · 2.67 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
//
// Created by naraujo on 20/08/17.
//
#include "Graph.hpp"
Graph::Graph(){
}
//Insert
bool Graph::insertNode() {
//Creates Edges for New Node
std::vector<int> newNodeEdges;
for (int i = 0; i < nodes.size(); i++){
edges[i].push_back(0);
newNodeEdges.push_back(0);
}
newNodeEdges.push_back(1);
//Insert Node
nodes.push_back(1);
edges.push_back(newNodeEdges);
return true;
}
bool Graph::insertEdge(int n1, int n2) {
//Safety Checks
if (n1 > edges.size() || n2 > edges[n1].size())
return false;
//Checks if already exists
if (edges[n1][n2] > 0)
return false;
else {
edges[n1][n2] = 1;
return true;
}
}
//Delete
bool Graph::deleteNode(int n) {
if (n > nodes.size() || n < 0)
return false;
//Delete Edges From Other Nodes
for (int i = 0; i < edges.size(); i++){
edges[i].erase(edges[i].begin() + n);
}
//Delete Whole Vector From Vector
edges.erase(edges.begin() + n);
//Delete Node
nodes.erase(nodes.begin() + n);
return true;
}
bool Graph::deleteEdge(int n1, int n2) {
//Safety Checks
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2)
return false;
if (edges[n1][n2] < 1)
return false;
edges[n1][n2] = 0;
return true;
}
//Costs
bool Graph::increaseEdge(int n1, int n2) {
//Safety Checks
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2)
return false;
//Cant insert
if (edges[n1][n2] < 1)
return false;
else
edges[n1][n2]++;
return true;
}
bool Graph::decreaseEdge(int n1, int n2) {
//Safety Checks
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2)
return false;
//Cant delete
if (edges[n1][n2] <= 1)
return false;
edges[n1][n2]--;
return true;
}
//Getters
bool Graph::getEdge(int n1, int n2, int& out){
//Safety Checks
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0)
return false;
out = edges[n1][n2];
return true;
}
bool Graph::getNode(int n, int& out){
if (n > nodes.size() || n < 0)
return false;
out = nodes[n];
return true;
}
void Graph::printEdges() {
std::cout << "Printing Edges..." << std::endl;
std::cout << "-------------- START --------------" << std::endl;
for (int i = 0; i < edges.size(); i++){
std::cout << "| ";
for (int j = 0; j < edges[i].size(); j++)
std::cout << "(" << edges[i][j] << ") | ";
std::cout << std::endl;
}
std::cout << "--------------- END ---------------" << std::endl;
}